Add check that JSON request body exists when required
[MAILPOET-4135]
This commit is contained in:
@@ -36,7 +36,13 @@ class Request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getBody(): array {
|
public function getBody(): array {
|
||||||
return $this->wpRequest->get_json_params() ?? [];
|
$json = $this->wpRequest->get_json_params();
|
||||||
|
|
||||||
|
/* @phpstan-ignore-next-line hotfix for missing 'null' in WP annotation */
|
||||||
|
if ($json === null) {
|
||||||
|
throw Exceptions::apiNoJsonBody();
|
||||||
|
}
|
||||||
|
return $json;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRawBody(): string {
|
public function getRawBody(): string {
|
||||||
|
@@ -3,11 +3,20 @@
|
|||||||
namespace MailPoet\Automation;
|
namespace MailPoet\Automation;
|
||||||
|
|
||||||
use MailPoet\Automation\Exceptions\InvalidStateException;
|
use MailPoet\Automation\Exceptions\InvalidStateException;
|
||||||
|
use MailPoet\Automation\Exceptions\UnexpectedValueException;
|
||||||
|
|
||||||
class Exceptions {
|
class Exceptions {
|
||||||
|
private const API_NO_JSON_BODY = 'mailpoet_automation_api_no_json_body';
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
throw new InvalidStateException(
|
throw new InvalidStateException(
|
||||||
"This is a static factory class. Use it via 'Exception::someError()' factories."
|
"This is a static factory class. Use it via 'Exception::someError()' factories."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function apiNoJsonBody(): UnexpectedValueException {
|
||||||
|
return UnexpectedValueException::create()
|
||||||
|
->withErrorCode(self::API_NO_JSON_BODY)
|
||||||
|
->withMessage('No JSON body passed.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user