| 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 |
|