|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* http-exception (https://github.com/juliangut/http-exception). |
|
5
|
|
|
* HTTP aware exceptions. |
|
6
|
|
|
* |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @link https://github.com/juliangut/http-exception |
|
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Jgut\HttpException; |
|
15
|
|
|
|
|
16
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
|
17
|
|
|
use PascalDeVink\ShortUuid\ShortUuid; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* HTTP exception class. |
|
21
|
|
|
*/ |
|
22
|
|
|
class HttpException extends \DomainException |
|
23
|
|
|
{ |
|
24
|
|
|
const STATUS_CODE_LIMIT = 600; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Unique error identifier. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $identifier; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Exception description. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $description; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* HTTP status code. |
|
42
|
|
|
* |
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $statusCode; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* HTTP Exception constructor. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $message |
|
51
|
|
|
* @param string $description |
|
52
|
|
|
* @param int $code |
|
53
|
|
|
* @param int $statusCode |
|
54
|
|
|
* @param \Throwable|null $previous |
|
55
|
|
|
* |
|
56
|
|
|
* @throws \RuntimeException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct( |
|
59
|
|
|
string $message, |
|
60
|
|
|
string $description, |
|
61
|
|
|
int $code, |
|
62
|
|
|
int $statusCode, |
|
63
|
|
|
\Throwable $previous = null |
|
64
|
|
|
) { |
|
65
|
|
|
if ($statusCode < StatusCodeInterface::STATUS_BAD_REQUEST || $statusCode >= static::STATUS_CODE_LIMIT) { |
|
66
|
|
|
throw new \RuntimeException(\sprintf('%s is not a valid HTTP error status code', $statusCode)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
parent::__construct(\trim($message), $code, $previous); |
|
70
|
|
|
|
|
71
|
|
|
$this->identifier = ShortUuid::uuid4(); |
|
72
|
|
|
$this->description = \trim($description); |
|
73
|
|
|
$this->statusCode = $statusCode; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get exception unique identifier. |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getIdentifier(): string |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->identifier; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get exception description. |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getDescription(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->description; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get HTTP status code. |
|
98
|
|
|
* |
|
99
|
|
|
* @return int |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getStatusCode(): int |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->statusCode; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|