Engine
4 weeks ago
Integrations
4 weeks ago
Validator
1 year ago
class-bootstrap.php
2 months ago
class-container.php
7 months ago
class-email-css-inliner.php
7 months ago
class-email-editor-container.php
3 months ago
class-package.php
1 year ago
exceptions.php
1 year ago
exceptions.php
124 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | // phpcs:ignoreFile PSR1.Classes.ClassDeclaration |
| 4 | namespace Automattic\WooCommerce\EmailEditor; |
| 5 | |
| 6 | /** |
| 7 | * Provides information for converting exceptions to HTTP responses. |
| 8 | */ |
| 9 | interface HttpAwareException { |
| 10 | public function getHttpStatusCode(): int; |
| 11 | } |
| 12 | |
| 13 | |
| 14 | /** |
| 15 | * Frames all exceptions ("$e instanceof Automattic\WooCommerce\EmailEditor\Exception"). |
| 16 | */ |
| 17 | abstract class Exception extends \Exception { |
| 18 | /** @var string[] */ |
| 19 | private $errors = []; |
| 20 | |
| 21 | final public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null) { |
| 22 | parent::__construct($message, $code, $previous); |
| 23 | } |
| 24 | |
| 25 | /** @return static */ |
| 26 | public static function create(?\Throwable $previous = null) { |
| 27 | return new static('', 0, $previous); |
| 28 | } |
| 29 | |
| 30 | /** @return static */ |
| 31 | public function withMessage(string $message) { |
| 32 | $this->message = $message; |
| 33 | return $this; |
| 34 | } |
| 35 | |
| 36 | /** @return static */ |
| 37 | public function withCode(int $code) { |
| 38 | $this->code = $code; |
| 39 | return $this; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param string[] $errors |
| 44 | * @return static |
| 45 | */ |
| 46 | public function withErrors(array $errors) { |
| 47 | $this->errors = $errors; |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | /** @return static */ |
| 52 | public function withError(string $id, string $error) { |
| 53 | $this->errors[$id] = $error; |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return string[] |
| 59 | */ |
| 60 | public function getErrors(): array { |
| 61 | return $this->errors; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * USE: Generic runtime error. When possible, use a more specific exception instead. |
| 68 | * API: 500 Server Error (not HTTP-aware) |
| 69 | */ |
| 70 | class RuntimeException extends Exception {} |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * USE: When wrong data VALUE is received. |
| 75 | * API: 400 Bad Request |
| 76 | */ |
| 77 | class UnexpectedValueException extends RuntimeException implements HttpAwareException { |
| 78 | public function getHttpStatusCode(): int { |
| 79 | return 400; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * USE: When an action is forbidden for given actor (although generally valid). |
| 86 | * API: 403 Forbidden |
| 87 | */ |
| 88 | class AccessDeniedException extends UnexpectedValueException implements HttpAwareException { |
| 89 | public function getHttpStatusCode(): int { |
| 90 | return 403; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * USE: When the main resource we're interested in doesn't exist. |
| 97 | * API: 404 Not Found |
| 98 | */ |
| 99 | class NotFoundException extends UnexpectedValueException implements HttpAwareException { |
| 100 | public function getHttpStatusCode(): int { |
| 101 | return 404; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * USE: When the main action produces conflict (i.e. duplicate key). |
| 108 | * API: 409 Conflict |
| 109 | */ |
| 110 | class ConflictException extends UnexpectedValueException implements HttpAwareException { |
| 111 | public function getHttpStatusCode(): int { |
| 112 | return 409; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * USE: An application state that should not occur. Can be subclassed for feature-specific exceptions. |
| 119 | * API: 500 Server Error (not HTTP-aware) |
| 120 | */ |
| 121 | class InvalidStateException extends RuntimeException {} |
| 122 | |
| 123 | class NewsletterProcessingException extends Exception {} |
| 124 |