AccessDeniedException.php
4 years ago
ConflictException.php
4 years ago
Exception.php
1 year ago
InvalidStateException.php
4 years ago
NotFoundException.php
4 years ago
RuntimeException.php
4 years ago
UnexpectedValueException.php
4 years ago
index.php
3 years ago
Exception.php
81 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Exceptions; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Exception as PhpException; |
| 9 | use MailPoet\API\REST\Exception as RestException; |
| 10 | use Throwable; |
| 11 | |
| 12 | /** |
| 13 | * Frames all MailPoet Automation exceptions ("$e instanceof MailPoet\Automation\Exception"). |
| 14 | */ |
| 15 | abstract class Exception extends PhpException implements RestException { |
| 16 | /** @var int */ |
| 17 | protected $statusCode = 500; |
| 18 | |
| 19 | /** @var string */ |
| 20 | protected $errorCode; |
| 21 | |
| 22 | /** @var string[] */ |
| 23 | protected $errors = []; |
| 24 | |
| 25 | final public function __construct( |
| 26 | ?string $message = null, |
| 27 | ?string $errorCode = null, |
| 28 | ?Throwable $previous = null |
| 29 | ) { |
| 30 | parent::__construct($message ?? __('Unknown error.', 'mailpoet'), 0, $previous); |
| 31 | $this->errorCode = $errorCode ?? 'mailpoet_automation_unknown_error'; |
| 32 | } |
| 33 | |
| 34 | /** @return static */ |
| 35 | public static function create(?Throwable $previous = null) { |
| 36 | return new static(null, null, $previous); |
| 37 | } |
| 38 | |
| 39 | /** @return static */ |
| 40 | public function withStatusCode(int $statusCode) { |
| 41 | $this->statusCode = $statusCode; |
| 42 | return $this; |
| 43 | } |
| 44 | |
| 45 | /** @return static */ |
| 46 | public function withError(string $id, string $error) { |
| 47 | $this->errors[$id] = $error; |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | /** @return static */ |
| 52 | public function withErrorCode(string $errorCode) { |
| 53 | $this->errorCode = $errorCode; |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | /** @return static */ |
| 58 | public function withMessage(string $message) { |
| 59 | $this->message = $message; |
| 60 | return $this; |
| 61 | } |
| 62 | |
| 63 | /** @return static */ |
| 64 | public function withErrors(array $errors) { |
| 65 | $this->errors = $errors; |
| 66 | return $this; |
| 67 | } |
| 68 | |
| 69 | public function getStatusCode(): int { |
| 70 | return $this->statusCode; |
| 71 | } |
| 72 | |
| 73 | public function getErrorCode(): string { |
| 74 | return $this->errorCode; |
| 75 | } |
| 76 | |
| 77 | public function getErrors(): array { |
| 78 | return $this->errors; |
| 79 | } |
| 80 | } |
| 81 |