PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 3.59.2
MailPoet – Newsletters, Email Marketing, and Automation v3.59.2
5.38.0 5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 All 542 releases
mailpoet / lib / exceptions.php
exceptions.php
109 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 public static function create(\Throwable $previous = null): self {
29 return new static('', 0, $previous);
30 }
31
32 public function withMessage(string $message): self {
33 $this->message = $message;
34 return $this;
35 }
36
37 public function withCode(int $code): self {
38 $this->code = $code;
39 return $this;
40 }
41
42 public function withErrors(array $errors): self {
43 $this->errors = $errors;
44 return $this;
45 }
46
47 public function getErrors(): array {
48 return $this->errors;
49 }
50 }
51
52
53 /**
54 * USE: Generic runtime error. When possible, use a more specific exception instead.
55 * API: 500 Server Error (not HTTP-aware)
56 */
57 class RuntimeException extends Exception {}
58
59
60 /**
61 * USE: When wrong data VALUE is received.
62 * API: 400 Bad Request
63 */
64 class UnexpectedValueException extends RuntimeException implements HttpAwareException {
65 public function getHttpStatusCode(): int {
66 return 400;
67 }
68 }
69
70
71 /**
72 * USE: When an action is forbidden for given actor (although generally valid).
73 * API: 403 Forbidden
74 */
75 class AccessDeniedException extends UnexpectedValueException implements HttpAwareException {
76 public function getHttpStatusCode(): int {
77 return 403;
78 }
79 }
80
81
82 /**
83 * USE: When the main resource we're interested in doesn't exist.
84 * API: 404 Not Found
85 */
86 class NotFoundException extends UnexpectedValueException implements HttpAwareException {
87 public function getHttpStatusCode(): int {
88 return 404;
89 }
90 }
91
92
93 /**
94 * USE: When the main action produces conflict (i.e. duplicate key).
95 * API: 409 Conflict
96 */
97 class ConflictException extends UnexpectedValueException implements HttpAwareException {
98 public function getHttpStatusCode(): int {
99 return 409;
100 }
101 }
102
103
104 /**
105 * USE: An application state that should not occur. Can be subclassed for feature-specific exceptions.
106 * API: 500 Server Error (not HTTP-aware)
107 */
108 class InvalidStateException extends RuntimeException {}
109