|
1 | 1 | <?php |
2 | | -declare(strict_types = 1); |
| 2 | +declare(strict_types=1); |
3 | 3 |
|
4 | 4 | namespace Middlewares; |
5 | 5 |
|
6 | | -use Exception; |
7 | | -use RuntimeException; |
8 | | -use Throwable; |
| 6 | +use Middlewares\Utils\HttpErrorException as BaseException; |
9 | 7 |
|
10 | | -class HttpErrorException extends Exception |
| 8 | +class HttpErrorException extends BaseException |
11 | 9 | { |
12 | | - private static $phrases = [ |
13 | | - // CLIENT ERROR |
14 | | - 400 => 'Bad Request', |
15 | | - 401 => 'Unauthorized', |
16 | | - 402 => 'Payment Required', |
17 | | - 403 => 'Forbidden', |
18 | | - 404 => 'Not Found', |
19 | | - 405 => 'Method Not Allowed', |
20 | | - 406 => 'Not Acceptable', |
21 | | - 407 => 'Proxy Authentication Required', |
22 | | - 408 => 'Request Time-out', |
23 | | - 409 => 'Conflict', |
24 | | - 410 => 'Gone', |
25 | | - 411 => 'Length Required', |
26 | | - 412 => 'Precondition Failed', |
27 | | - 413 => 'Request Entity Too Large', |
28 | | - 414 => 'Request-URI Too Large', |
29 | | - 415 => 'Unsupported Media Type', |
30 | | - 416 => 'Requested range not satisfiable', |
31 | | - 417 => 'Expectation Failed', |
32 | | - 418 => 'I\'m a teapot', |
33 | | - 421 => 'Misdirected Request', |
34 | | - 422 => 'Unprocessable Entity', |
35 | | - 423 => 'Locked', |
36 | | - 424 => 'Failed Dependency', |
37 | | - 425 => 'Unordered Collection', |
38 | | - 426 => 'Upgrade Required', |
39 | | - 428 => 'Precondition Required', |
40 | | - 429 => 'Too Many Requests', |
41 | | - 431 => 'Request Header Fields Too Large', |
42 | | - 444 => 'Connection Closed Without Response', |
43 | | - 451 => 'Unavailable For Legal Reasons', |
44 | | - // SERVER ERROR |
45 | | - 499 => 'Client Closed Request', |
46 | | - 500 => 'Internal Server Error', |
47 | | - 501 => 'Not Implemented', |
48 | | - 502 => 'Bad Gateway', |
49 | | - 503 => 'Service Unavailable', |
50 | | - 504 => 'Gateway Time-out', |
51 | | - 505 => 'HTTP Version not supported', |
52 | | - 506 => 'Variant Also Negotiates', |
53 | | - 507 => 'Insufficient Storage', |
54 | | - 508 => 'Loop Detected', |
55 | | - 510 => 'Not Extended', |
56 | | - 511 => 'Network Authentication Required', |
57 | | - 599 => 'Network Connect Timeout Error', |
58 | | - ]; |
59 | | - |
60 | | - private $context = []; |
61 | | - |
62 | | - /** |
63 | | - * Create and returns a new instance |
64 | | - * |
65 | | - * @param int $code A valid http error code |
66 | | - * @param array $context |
67 | | - * @param Throwable|null $previous |
68 | | - * |
69 | | - * @return static |
70 | | - */ |
71 | | - public static function create(int $code = 500, array $context = [], Throwable $previous = null): self |
72 | | - { |
73 | | - if (!isset(self::$phrases[$code])) { |
74 | | - throw new RuntimeException("Http error not valid ({$code})"); |
75 | | - } |
76 | | - |
77 | | - $exception = new static(self::$phrases[$code], $code, $previous); |
78 | | - $exception->setContext($context); |
79 | | - |
80 | | - return $exception; |
81 | | - } |
82 | | - |
83 | | - /** |
84 | | - * Add data context used in the error handler |
85 | | - */ |
86 | | - public function setContext(array $context) |
87 | | - { |
88 | | - $this->context = $context; |
89 | | - } |
90 | | - |
91 | | - /** |
92 | | - * Return the data context |
93 | | - */ |
94 | | - public function getContext(): array |
95 | | - { |
96 | | - return $this->context; |
97 | | - } |
98 | 10 | } |
0 commit comments