PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 3.90.2
MailPoet – Newsletters, Email Marketing, and Automation v3.90.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 in MailPoet – Newsletters, Email Marketing, and Automation 3.90.2, at lib/exceptions.php

113 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 /** @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 public function getErrors(): array {
52 return $this->errors;
53 }
54 }
55
56
57 /**
58 * USE: Generic runtime error. When possible, use a more specific exception instead.
59 * API: 500 Server Error (not HTTP-aware)
60 */
61 class RuntimeException extends Exception {}
62
63
64 /**
65 * USE: When wrong data VALUE is received.
66 * API: 400 Bad Request
67 */
68 class UnexpectedValueException extends RuntimeException implements HttpAwareException {
69 public function getHttpStatusCode(): int {
70 return 400;
71 }
72 }
73
74
75 /**
76 * USE: When an action is forbidden for given actor (although generally valid).
77 * API: 403 Forbidden
78 */
79 class AccessDeniedException extends UnexpectedValueException implements HttpAwareException {
80 public function getHttpStatusCode(): int {
81 return 403;
82 }
83 }
84
85
86 /**
87 * USE: When the main resource we're interested in doesn't exist.
88 * API: 404 Not Found
89 */
90 class NotFoundException extends UnexpectedValueException implements HttpAwareException {
91 public function getHttpStatusCode(): int {
92 return 404;
93 }
94 }
95
96
97 /**
98 * USE: When the main action produces conflict (i.e. duplicate key).
99 * API: 409 Conflict
100 */
101 class ConflictException extends UnexpectedValueException implements HttpAwareException {
102 public function getHttpStatusCode(): int {
103 return 409;
104 }
105 }
106
107
108 /**
109 * USE: An application state that should not occur. Can be subclassed for feature-specific exceptions.
110 * API: 500 Server Error (not HTTP-aware)
111 */
112 class InvalidStateException extends RuntimeException {}
113