Skip to content

Commit

Permalink
feat: add exceptions (#2)
Browse files Browse the repository at this point in the history
* feat: add exceptions

* fix: add strict types

* chore: apply changes

* fix: array result

* chore: apply changes

* chore: apply changes

---------

Signed-off-by: Lyrisbee <Lyrisbee@users.noreply.github.com>
  • Loading branch information
Lyrisbee authored Dec 12, 2023
1 parent f5f73bf commit d803442
Show file tree
Hide file tree
Showing 20 changed files with 289 additions and 102 deletions.
15 changes: 15 additions & 0 deletions src/Exceptions/BadRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Exceptions;

use Storipress\WordPress\Objects\WordPressError;

class BadRequestException extends WordPressException
{
public function __construct(WordPressError $error)
{
parent::__construct($error, 400);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Storipress\WordPress\Exceptions;

use Exception as BaseException;

abstract class Exception extends BaseException
class CannotCreateException extends WordPressException
{
//
}
10 changes: 10 additions & 0 deletions src/Exceptions/CannotUpdateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Exceptions;

class CannotUpdateException extends WordPressException
{
//
}
10 changes: 10 additions & 0 deletions src/Exceptions/DuplicateTermSlugException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Exceptions;

class DuplicateTermSlugException extends WordPressException
{
//
}
15 changes: 15 additions & 0 deletions src/Exceptions/ForbiddenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Exceptions;

use Storipress\WordPress\Objects\WordPressError;

class ForbiddenException extends WordPressException
{
public function __construct(WordPressError $error)
{
parent::__construct($error, 403);
}
}
9 changes: 7 additions & 2 deletions src/Exceptions/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

namespace Storipress\WordPress\Exceptions;

class NotFoundException extends HttpException
use Storipress\WordPress\Objects\WordPressError;

class NotFoundException extends WordPressException
{
//
public function __construct(WordPressError $error)
{
parent::__construct($error, 404);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Storipress\WordPress\Exceptions;

class HttpUnknownError extends HttpException
class TermExistsException extends WordPressException
{
//
}
15 changes: 15 additions & 0 deletions src/Exceptions/UnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Exceptions;

use Storipress\WordPress\Objects\WordPressError;

class UnauthorizedException extends WordPressException
{
public function __construct(WordPressError $error)
{
parent::__construct($error, 401);
}
}
9 changes: 7 additions & 2 deletions src/Exceptions/UnexpectedValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

namespace Storipress\WordPress\Exceptions;

class UnexpectedValueException extends Exception
use Storipress\WordPress\Objects\WordPressError;

class UnexpectedValueException extends WordPressException
{
//
public function __construct(WordPressError $error)
{
parent::__construct($error, 500);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Storipress\WordPress\Exceptions;

abstract class HttpException extends Exception
class UnknownException extends WordPressException
{
//
}
28 changes: 28 additions & 0 deletions src/Exceptions/WordPressException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Exceptions;

use Exception as BaseException;
use Storipress\WordPress\Objects\WordPressError;

abstract class WordPressException extends BaseException
{
/**
* Duplicate term id
*/
public ?int $term_id;

public function __construct(public WordPressError $error, int $code)
{
$this->term_id = $error->data->term_id ?? null;

parent::__construct($error->message, $code);
}

public function getTermId(): ?int
{
return $this->term_id;
}
}
16 changes: 16 additions & 0 deletions src/Objects/WordPressError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Objects;

use stdClass;

class WordPressError extends WordPressObject
{
public string $code;

public string $message;

public stdClass $data;
}
26 changes: 10 additions & 16 deletions src/Requests/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,15 +14,14 @@ class Category extends Request
*
* @return array<int, CategoryObject>
*
* @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();
}

return array_map(
Expand All @@ -37,15 +35,14 @@ public function list(): array
*
* @param array<string, mixed> $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();
}

return CategoryObject::from($data);
Expand All @@ -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
{
Expand All @@ -66,7 +62,7 @@ public function retrieve(int $categoryId, string $context = 'view'): CategoryObj
]);

if (is_array($data)) {
throw new UnexpectedValueException();
throw $this->unexpectedValueException();
}

return CategoryObject::from($data);
Expand All @@ -77,8 +73,7 @@ public function retrieve(int $categoryId, string $context = 'view'): CategoryObj
*
* @param array<string, mixed> $arguments
*
* @throws HttpException
* @throws UnexpectedValueException
* @throws WordPressException
*/
public function update(int $categoryId, array $arguments): CategoryObject
{
Expand All @@ -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();
}

return CategoryObject::from($data);
Expand All @@ -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
{
Expand Down
13 changes: 5 additions & 8 deletions src/Requests/GeneralRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Storipress\WordPress\Requests;

use stdClass;
use Storipress\WordPress\Exceptions\WordPressException;

class GeneralRequest extends Request
{
Expand All @@ -13,8 +14,7 @@ class GeneralRequest extends Request
* @param array<mixed> $arguments
* @return stdClass|array<int, stdClass>
*
* @throws \Storipress\WordPress\Exceptions\HttpException
* @throws \Storipress\WordPress\Exceptions\UnexpectedValueException
* @throws WordPressException
*/
public function get(string $path, array $arguments): stdClass|array
{
Expand All @@ -26,8 +26,7 @@ public function get(string $path, array $arguments): stdClass|array
* @param array<mixed> $arguments
* @return stdClass|array<int, stdClass>
*
* @throws \Storipress\WordPress\Exceptions\HttpException
* @throws \Storipress\WordPress\Exceptions\UnexpectedValueException
* @throws WordPressException
*/
public function post(string $path, array $arguments): stdClass|array
{
Expand All @@ -39,8 +38,7 @@ public function post(string $path, array $arguments): stdClass|array
* @param array<mixed> $arguments
* @return stdClass|array<int, stdClass>
*
* @throws \Storipress\WordPress\Exceptions\HttpException
* @throws \Storipress\WordPress\Exceptions\UnexpectedValueException
* @throws WordPressException
*/
public function patch(string $path, array $arguments): stdClass|array
{
Expand All @@ -51,8 +49,7 @@ public function patch(string $path, array $arguments): stdClass|array
* @param non-empty-string $path
* @param array<mixed> $arguments
*
* @throws \Storipress\WordPress\Exceptions\HttpException
* @throws \Storipress\WordPress\Exceptions\UnexpectedValueException
* @throws WordPressException
*/
public function delete(string $path, array $arguments): bool
{
Expand Down
Loading

0 comments on commit d803442

Please sign in to comment.