fixed ErrorResponse call in API::setupPublic()

- removed empty constructors in updated endpoints
- added missing keys to Error class (unauthorized, forbidden)
This commit is contained in:
Jonathan Labreuille
2016-08-03 16:04:45 +02:00
parent c5b376bd21
commit 1c6fca7f83
4 changed files with 18 additions and 13 deletions

View File

@ -37,7 +37,9 @@ class API {
function setupAdmin() {
if($this->checkToken() === false) {
(new ErrorResponse(
array('unauthorized' => __('This request is not authorized.')),
array(
Error::UNAUTHORIZED => __('You need to specify a valid API token.')
),
array(),
Response::STATUS_UNAUTHORIZED
))->send();
@ -45,7 +47,9 @@ class API {
if($this->checkPermissions() === false) {
(new ErrorResponse(
array('forbidden' => __('You do not have the required permissions.')),
array(
Error::FORBIDDEN => __('You do not have the required permissions.')
),
array(),
Response::STATUS_FORBIDDEN
))->send();
@ -56,10 +60,13 @@ class API {
function setupPublic() {
if($this->checkToken() === false) {
$response = new ErrorResponse(array(
'unauthorized' => __('This request is not authorized.')
), Response::STATUS_UNAUTHORIZED);
$response->send();
(new ErrorResponse(
array(
Error::UNAUTHORIZED => __('You need to specify a valid API token.')
),
array(),
Response::STATUS_UNAUTHORIZED
))->send();
}
$this->processRoute();
@ -101,7 +108,9 @@ class API {
wp_send_json($response);
}
} catch(\Exception $e) {
(new ErrorResponse(array($e->getMessage())))->send();
(new ErrorResponse(
array($e->getCode() => $e->getMessage())
))->send();
}
}

View File

@ -7,9 +7,6 @@ use \MailPoet\Models\Setting;
if(!defined('ABSPATH')) exit;
class Settings extends APIEndpoint {
function __construct() {
}
function get() {
return $this->successResponse(Setting::getAll());
}

View File

@ -6,9 +6,6 @@ use \MailPoet\Config\Activator;
if(!defined('ABSPATH')) exit;
class Setup extends APIEndpoint {
function __construct() {
}
function reset() {
try {
$activator = new Activator();

View File

@ -6,6 +6,8 @@ if(!defined('ABSPATH')) exit;
final class Error {
const UNKNOWN = 'unknown';
const BAD_REQUEST = 'bad_request';
const UNAUTHORIZED = 'unauthorized';
const FORBIDDEN = 'forbidden';
private function __construct() {