Add check that JSON request body exists when required

[MAILPOET-4135]
This commit is contained in:
Jan Jakes
2022-02-16 11:00:39 +01:00
committed by Veljko V
parent 8c7516eaba
commit 51d6b541e5
2 changed files with 16 additions and 1 deletions

View File

@@ -36,7 +36,13 @@ class Request {
}
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 {

View File

@@ -3,11 +3,20 @@
namespace MailPoet\Automation;
use MailPoet\Automation\Exceptions\InvalidStateException;
use MailPoet\Automation\Exceptions\UnexpectedValueException;
class Exceptions {
private const API_NO_JSON_BODY = 'mailpoet_automation_api_no_json_body';
public function __construct() {
throw new InvalidStateException(
"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.');
}
}