diff --git a/src/Exceptions/BadRequestException.php b/src/Exceptions/BadRequestException.php index e03faa1..a8d47b1 100644 --- a/src/Exceptions/BadRequestException.php +++ b/src/Exceptions/BadRequestException.php @@ -4,7 +4,12 @@ namespace Storipress\WordPress\Exceptions; -class BadRequestException extends HttpException +use Storipress\WordPress\Objects\ErrorException; + +class BadRequestException extends WordPressException { - // + public function __construct(ErrorException $error) + { + parent::__construct($error, 400); + } } diff --git a/src/Exceptions/Exception.php b/src/Exceptions/CannotCreateException.php similarity index 52% rename from src/Exceptions/Exception.php rename to src/Exceptions/CannotCreateException.php index d2e69af..3f1404c 100644 --- a/src/Exceptions/Exception.php +++ b/src/Exceptions/CannotCreateException.php @@ -4,9 +4,7 @@ namespace Storipress\WordPress\Exceptions; -use Exception as BaseException; - -abstract class Exception extends BaseException +class CannotCreateException extends WordPressException { // } diff --git a/src/Exceptions/CannotUpdateException.php b/src/Exceptions/CannotUpdateException.php new file mode 100644 index 0000000..3385bc9 --- /dev/null +++ b/src/Exceptions/CannotUpdateException.php @@ -0,0 +1,10 @@ +raw_message = $error->raw_message; - - $this->message_code = $error->code; - - parent::__construct($error->message, $code, $previous); - } - - public function getRawMessage(): string - { - return $this->raw_message; - } - - public function getMessageCode(): string - { - return $this->message_code; - } -} diff --git a/src/Exceptions/Rest/TermExistsException.php b/src/Exceptions/Rest/TermExistsException.php deleted file mode 100644 index 6e3488e..0000000 --- a/src/Exceptions/Rest/TermExistsException.php +++ /dev/null @@ -1,28 +0,0 @@ -term_id = $error->data['term_id'] ?? null; - - parent::__construct($error, $code, $previous); - } - - public function getTermId(): mixed - { - return $this->term_id; - } -} diff --git a/src/Exceptions/Rest/UnknownException.php b/src/Exceptions/Rest/UnknownException.php deleted file mode 100644 index f5b0898..0000000 --- a/src/Exceptions/Rest/UnknownException.php +++ /dev/null @@ -1,10 +0,0 @@ -term_id = $error->data->term_id ?? null; + + parent::__construct($error->message, $code); + } + + public function getTermId(): ?int + { + return $this->term_id; + } +} diff --git a/src/Objects/ErrorException.php b/src/Objects/ErrorException.php index fc2958c..d0c9be8 100644 --- a/src/Objects/ErrorException.php +++ b/src/Objects/ErrorException.php @@ -12,7 +12,5 @@ class ErrorException extends WordPressObject public string $message; - public string $raw_message; - public stdClass $data; } diff --git a/src/Requests/Category.php b/src/Requests/Category.php index 834b48b..87bf3b4 100644 --- a/src/Requests/Category.php +++ b/src/Requests/Category.php @@ -4,8 +4,7 @@ namespace Storipress\WordPress\Requests; -use Storipress\WordPress\Exceptions\HttpException; -use Storipress\WordPress\Exceptions\UnexpectedValueException; +use Storipress\WordPress\Exceptions\WordPressException; use Storipress\WordPress\Objects\Category as CategoryObject; class Category extends Request @@ -15,15 +14,14 @@ class Category extends Request * * @return array * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function list(): array { $data = $this->request('get', '/categories'); if (!is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return array_map( @@ -37,15 +35,14 @@ public function list(): array * * @param array $arguments * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function create(array $arguments): CategoryObject { $data = $this->request('post', '/categories', $arguments); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return CategoryObject::from($data); @@ -54,8 +51,7 @@ public function create(array $arguments): CategoryObject /** * https://developer.wordpress.org/rest-api/reference/categories/#retrieve-a-category * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function retrieve(int $categoryId, string $context = 'view'): CategoryObject { @@ -66,7 +62,7 @@ public function retrieve(int $categoryId, string $context = 'view'): CategoryObj ]); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return CategoryObject::from($data); @@ -77,8 +73,7 @@ public function retrieve(int $categoryId, string $context = 'view'): CategoryObj * * @param array $arguments * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function update(int $categoryId, array $arguments): CategoryObject { @@ -87,7 +82,7 @@ public function update(int $categoryId, array $arguments): CategoryObject $data = $this->request('patch', $uri, $arguments); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return CategoryObject::from($data); @@ -96,8 +91,7 @@ public function update(int $categoryId, array $arguments): CategoryObject /** * https://developer.wordpress.org/rest-api/reference/categories/#delete-a-category * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function delete(int $categoryId): bool { diff --git a/src/Requests/GeneralRequest.php b/src/Requests/GeneralRequest.php index c84a2fe..14ebc19 100644 --- a/src/Requests/GeneralRequest.php +++ b/src/Requests/GeneralRequest.php @@ -5,6 +5,7 @@ namespace Storipress\WordPress\Requests; use stdClass; +use Storipress\WordPress\Exceptions\WordPressException; class GeneralRequest extends Request { @@ -13,8 +14,7 @@ class GeneralRequest extends Request * @param array $arguments * @return stdClass|array * - * @throws \Storipress\WordPress\Exceptions\HttpException - * @throws \Storipress\WordPress\Exceptions\UnexpectedValueException + * @throws WordPressException */ public function get(string $path, array $arguments): stdClass|array { @@ -26,8 +26,7 @@ public function get(string $path, array $arguments): stdClass|array * @param array $arguments * @return stdClass|array * - * @throws \Storipress\WordPress\Exceptions\HttpException - * @throws \Storipress\WordPress\Exceptions\UnexpectedValueException + * @throws WordPressException */ public function post(string $path, array $arguments): stdClass|array { @@ -39,8 +38,7 @@ public function post(string $path, array $arguments): stdClass|array * @param array $arguments * @return stdClass|array * - * @throws \Storipress\WordPress\Exceptions\HttpException - * @throws \Storipress\WordPress\Exceptions\UnexpectedValueException + * @throws WordPressException */ public function patch(string $path, array $arguments): stdClass|array { @@ -51,8 +49,7 @@ public function patch(string $path, array $arguments): stdClass|array * @param non-empty-string $path * @param array $arguments * - * @throws \Storipress\WordPress\Exceptions\HttpException - * @throws \Storipress\WordPress\Exceptions\UnexpectedValueException + * @throws WordPressException */ public function delete(string $path, array $arguments): bool { diff --git a/src/Requests/Post.php b/src/Requests/Post.php index a2f7209..becd9bf 100644 --- a/src/Requests/Post.php +++ b/src/Requests/Post.php @@ -4,8 +4,7 @@ namespace Storipress\WordPress\Requests; -use Storipress\WordPress\Exceptions\HttpException; -use Storipress\WordPress\Exceptions\UnexpectedValueException; +use Storipress\WordPress\Exceptions\WordPressException; use Storipress\WordPress\Objects\Post as PostObject; class Post extends Request @@ -15,15 +14,14 @@ class Post extends Request * * @return array * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function list(): array { $data = $this->request('get', '/posts'); if (!is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return array_map( @@ -37,15 +35,14 @@ public function list(): array * * @param array $arguments * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function create(array $arguments): PostObject { $data = $this->request('post', '/posts', $arguments); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return PostObject::from($data); @@ -54,8 +51,7 @@ public function create(array $arguments): PostObject /** * https://developer.wordpress.org/rest-api/reference/posts/#retrieve-a-post * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function retrieve(int $postId, string $context = 'view'): PostObject { @@ -66,7 +62,7 @@ public function retrieve(int $postId, string $context = 'view'): PostObject ]); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return PostObject::from($data); @@ -77,8 +73,7 @@ public function retrieve(int $postId, string $context = 'view'): PostObject * * @param array $arguments * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function update(int $postId, array $arguments): PostObject { @@ -87,7 +82,7 @@ public function update(int $postId, array $arguments): PostObject $data = $this->request('patch', $uri, $arguments); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return PostObject::from($data); @@ -96,8 +91,7 @@ public function update(int $postId, array $arguments): PostObject /** * https://developer.wordpress.org/rest-api/reference/posts/#delete-a-post * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function delete(int $postId, bool $force = false): bool { diff --git a/src/Requests/Request.php b/src/Requests/Request.php index 0489a49..0619b63 100644 --- a/src/Requests/Request.php +++ b/src/Requests/Request.php @@ -9,17 +9,16 @@ use JsonSchema\Validator; use stdClass; use Storipress\WordPress\Exceptions\BadRequestException; +use Storipress\WordPress\Exceptions\CannotCreateException; +use Storipress\WordPress\Exceptions\CannotUpdateException; +use Storipress\WordPress\Exceptions\DuplicateTermSlugException; use Storipress\WordPress\Exceptions\ForbiddenException; -use Storipress\WordPress\Exceptions\HttpException; -use Storipress\WordPress\Exceptions\HttpUnknownError; use Storipress\WordPress\Exceptions\NotFoundException; -use Storipress\WordPress\Exceptions\Rest\CannotCreateException; -use Storipress\WordPress\Exceptions\Rest\CannotUpdateException; -use Storipress\WordPress\Exceptions\Rest\DuplicateTermSlugException; -use Storipress\WordPress\Exceptions\Rest\TermExistsException; -use Storipress\WordPress\Exceptions\Rest\UnknownException; +use Storipress\WordPress\Exceptions\TermExistsException; use Storipress\WordPress\Exceptions\UnauthorizedException; use Storipress\WordPress\Exceptions\UnexpectedValueException; +use Storipress\WordPress\Exceptions\UnknownException; +use Storipress\WordPress\Exceptions\WordPressException; use Storipress\WordPress\Objects\ErrorException; use Storipress\WordPress\WordPress; @@ -39,8 +38,7 @@ public function __construct( * @param array $options * @return ($method is 'delete' ? bool : stdClass|array) * - * @throws UnexpectedValueException - * @throws HttpException + * @throws UnexpectedValueException|WordPressException */ protected function request( string $method, @@ -59,14 +57,14 @@ protected function request( $response = $http->{$method}($this->getUrl($path), $options); if (!($response instanceof Response)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response value.'); } $payload = $response->object(); // @phpstan-ignore-next-line if (!($payload instanceof stdClass) && !is_array($payload)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } if (!$response->successful()) { @@ -74,7 +72,6 @@ protected function request( $payload, $response->body(), $response->status(), - $response->headers(), ); } @@ -95,11 +92,9 @@ public function getUrl(string $path): string } /** - * @param array> $headers - * - * @throws HttpException + * @throws WordPressException */ - protected function error(stdClass $payload, string $message, int $status, array $headers): void + protected function error(stdClass $payload, string $message, int $status): void { if ($this->validate($payload)) { $error = ErrorException::from($payload); @@ -113,12 +108,18 @@ protected function error(stdClass $payload, string $message, int $status, array }; } + $error = ErrorException::from((object) [ + 'code' => (string) $status, + 'message' => $message, + 'data' => (object) [], + ]); + throw match ($status) { - 400 => new BadRequestException($message, $status), - 401 => new UnauthorizedException($message, $status), - 403 => new ForbiddenException($message, $status), - 404 => new NotFoundException($message, $status), - default => new HttpUnknownError($message, $status), + 400 => new BadRequestException($error), + 401 => new UnauthorizedException($error), + 403 => new ForbiddenException($error), + 404 => new NotFoundException($error), + default => new UnknownException($error, $status), }; } @@ -140,4 +141,13 @@ protected function validate(stdClass $data): bool return $validator->isValid(); } + + protected function unexpectedValueException(string $message): UnexpectedValueException + { + return new UnexpectedValueException(ErrorException::from((object) [ + 'message' => $message, + 'code' => '500', + 'data' => (object) [], + ])); + } } diff --git a/src/Requests/Tag.php b/src/Requests/Tag.php index c6e9870..265cf22 100644 --- a/src/Requests/Tag.php +++ b/src/Requests/Tag.php @@ -4,8 +4,7 @@ namespace Storipress\WordPress\Requests; -use Storipress\WordPress\Exceptions\HttpException; -use Storipress\WordPress\Exceptions\UnexpectedValueException; +use Storipress\WordPress\Exceptions\WordPressException; use Storipress\WordPress\Objects\Tag as TagObject; class Tag extends Request @@ -15,15 +14,14 @@ class Tag extends Request * * @return array * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function list(): array { $data = $this->request('get', '/tags'); if (!is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return array_map( @@ -37,15 +35,14 @@ public function list(): array * * @param array $arguments * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function create(array $arguments): TagObject { $data = $this->request('post', '/tags', $arguments); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return TagObject::from($data); @@ -54,8 +51,7 @@ public function create(array $arguments): TagObject /** * https://developer.wordpress.org/rest-api/reference/tags/#retrieve-a-tag * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function retrieve(int $tagId, string $context = 'view'): TagObject { @@ -66,7 +62,7 @@ public function retrieve(int $tagId, string $context = 'view'): TagObject ]); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return TagObject::from($data); @@ -77,8 +73,7 @@ public function retrieve(int $tagId, string $context = 'view'): TagObject * * @param array $arguments * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function update(int $tagId, array $arguments): TagObject { @@ -87,7 +82,7 @@ public function update(int $tagId, array $arguments): TagObject $data = $this->request('patch', $uri, $arguments); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return TagObject::from($data); @@ -96,8 +91,7 @@ public function update(int $tagId, array $arguments): TagObject /** * https://developer.wordpress.org/rest-api/reference/tags/#delete-a-tag * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function delete(int $tagId): bool { diff --git a/src/Requests/User.php b/src/Requests/User.php index df27873..a60aceb 100644 --- a/src/Requests/User.php +++ b/src/Requests/User.php @@ -4,8 +4,7 @@ namespace Storipress\WordPress\Requests; -use Storipress\WordPress\Exceptions\HttpException; -use Storipress\WordPress\Exceptions\UnexpectedValueException; +use Storipress\WordPress\Exceptions\WordPressException; use Storipress\WordPress\Objects\User as UserObject; class User extends Request @@ -15,15 +14,14 @@ class User extends Request * * @return array * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function list(): array { $data = $this->request('get', '/users'); if (!is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return array_map( @@ -37,15 +35,14 @@ public function list(): array * * @param array $arguments * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function create(array $arguments): UserObject { $data = $this->request('post', '/users', $arguments); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return UserObject::from($data); @@ -54,8 +51,7 @@ public function create(array $arguments): UserObject /** * https://developer.wordpress.org/rest-api/reference/users/#retrieve-a-user * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function retrieve(int $userId, string $context = 'view'): UserObject { @@ -66,7 +62,7 @@ public function retrieve(int $userId, string $context = 'view'): UserObject ]); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return UserObject::from($data); @@ -77,8 +73,7 @@ public function retrieve(int $userId, string $context = 'view'): UserObject * * @param array $arguments * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function update(int $userId, array $arguments): UserObject { @@ -87,7 +82,7 @@ public function update(int $userId, array $arguments): UserObject $data = $this->request('patch', $uri, $arguments); if (is_array($data)) { - throw new UnexpectedValueException(); + throw $this->unexpectedValueException('Unexpected response format.'); } return UserObject::from($data); @@ -96,8 +91,7 @@ public function update(int $userId, array $arguments): UserObject /** * https://developer.wordpress.org/rest-api/reference/users/#delete-a-user * - * @throws HttpException - * @throws UnexpectedValueException + * @throws WordPressException */ public function delete(int $userId, int $reassign): bool { diff --git a/tests/ArchitectureTest.php b/tests/ArchitectureTest.php index e01127c..8138af6 100644 --- a/tests/ArchitectureTest.php +++ b/tests/ArchitectureTest.php @@ -1,6 +1,6 @@ expect('Storipress\WordPress\Exceptions') ->classes() - ->toExtend(Exception::class); + ->toExtend(WordPressException::class);