API
1 week ago
Abilities
1 month ago
AdminPages
1 week ago
Analytics
2 weeks ago
AutomaticEmails
2 months ago
Automation
3 weeks ago
Cache
2 weeks ago
Captcha
2 weeks ago
Config
5 days ago
Cron
3 days ago
CustomFields
1 month ago
DI
5 days ago
Doctrine
1 month ago
EmailEditor
3 days ago
Entities
5 days ago
Features
1 month ago
Form
1 week ago
Homepage
2 years ago
Listing
1 month ago
Logging
2 weeks ago
Mailer
2 weeks ago
Migrations
5 days ago
Migrator
2 weeks ago
Newsletter
3 days ago
NewsletterTemplates
2 months ago
PostEditorBlocks
1 week ago
Referrals
3 years ago
Router
5 days ago
Segments
2 weeks ago
Services
5 days ago
Settings
5 days ago
Statistics
3 days ago
Subscribers
3 days ago
Subscription
5 days ago
SystemReport
2 months ago
Tags
1 month ago
Tasks
2 months ago
Twig
2 months ago
Util
2 weeks ago
Validator
8 months ago
WP
1 week ago
WPCOM
5 months ago
WooCommerce
2 weeks ago
exceptions.php
1 year ago
index.php
3 years ago
exceptions.php
121 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | // phpcs:ignoreFile PSR1.Classes.ClassDeclaration |
| 4 | namespace MailPoet; |
| 5 | |
| 6 | if (!defined('ABSPATH')) exit; |
| 7 | |
| 8 | |
| 9 | /** |
| 10 | * Provides information for converting exceptions to HTTP responses. |
| 11 | */ |
| 12 | interface HttpAwareException { |
| 13 | public function getHttpStatusCode(): int; |
| 14 | } |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * Frames all MailPoet exceptions ("$e instanceof MailPoet\Exception"). |
| 19 | */ |
| 20 | abstract class Exception extends \Exception { |
| 21 | /** @var string[] */ |
| 22 | private $errors = []; |
| 23 | |
| 24 | final public function __construct($message = '', $code = 0, ?\Throwable $previous = null) { |
| 25 | parent::__construct($message, $code, $previous); |
| 26 | } |
| 27 | |
| 28 | /** @return static */ |
| 29 | public static function create(?\Throwable $previous = null) { |
| 30 | return new static('', 0, $previous); |
| 31 | } |
| 32 | |
| 33 | /** @return static */ |
| 34 | public function withMessage(string $message) { |
| 35 | $this->message = $message; |
| 36 | return $this; |
| 37 | } |
| 38 | |
| 39 | /** @return static */ |
| 40 | public function withCode(int $code) { |
| 41 | $this->code = $code; |
| 42 | return $this; |
| 43 | } |
| 44 | |
| 45 | /** @return static */ |
| 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 | public function getErrors(): array { |
| 58 | return $this->errors; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * USE: Generic runtime error. When possible, use a more specific exception instead. |
| 65 | * API: 500 Server Error (not HTTP-aware) |
| 66 | */ |
| 67 | class RuntimeException extends Exception {} |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * USE: When wrong data VALUE is received. |
| 72 | * API: 400 Bad Request |
| 73 | */ |
| 74 | class UnexpectedValueException extends RuntimeException implements HttpAwareException { |
| 75 | public function getHttpStatusCode(): int { |
| 76 | return 400; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * USE: When an action is forbidden for given actor (although generally valid). |
| 83 | * API: 403 Forbidden |
| 84 | */ |
| 85 | class AccessDeniedException extends UnexpectedValueException implements HttpAwareException { |
| 86 | public function getHttpStatusCode(): int { |
| 87 | return 403; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * USE: When the main resource we're interested in doesn't exist. |
| 94 | * API: 404 Not Found |
| 95 | */ |
| 96 | class NotFoundException extends UnexpectedValueException implements HttpAwareException { |
| 97 | public function getHttpStatusCode(): int { |
| 98 | return 404; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * USE: When the main action produces conflict (i.e. duplicate key). |
| 105 | * API: 409 Conflict |
| 106 | */ |
| 107 | class ConflictException extends UnexpectedValueException implements HttpAwareException { |
| 108 | public function getHttpStatusCode(): int { |
| 109 | return 409; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * USE: An application state that should not occur. Can be subclassed for feature-specific exceptions. |
| 116 | * API: 500 Server Error (not HTTP-aware) |
| 117 | */ |
| 118 | class InvalidStateException extends RuntimeException {} |
| 119 | |
| 120 | class NewsletterProcessingException extends Exception {} |
| 121 |