|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Response\Error; |
|
10
|
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Exception\InvalidParametersException; |
|
12
|
|
|
use Psr\Log\LogLevel; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
17
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author John Kleijn <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class HttpError |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $logRef; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
private $statusCode; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $severity; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $message; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Request |
|
46
|
|
|
*/ |
|
47
|
|
|
private $request; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var \Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
private $exception; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* HttpError constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Request $request |
|
58
|
|
|
* @param \Exception $exception |
|
59
|
|
|
* @param LogRefBuilder $logRefBuilder |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct(Request $request, \Exception $exception, LogRefBuilder $logRefBuilder) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
$this->request = $request; |
|
64
|
|
|
$this->exception = $exception; |
|
65
|
|
|
$this->logRef = $logRefBuilder->create($request, $exception); |
|
66
|
|
|
|
|
67
|
|
|
$code = $exception->getCode(); |
|
68
|
|
|
|
|
69
|
|
|
if ($exception instanceof InvalidParametersException) { |
|
70
|
|
|
$this->severity = LogLevel::NOTICE; |
|
71
|
|
|
$this->statusCode = Response::HTTP_BAD_REQUEST; |
|
72
|
|
|
$this->message = "Validation failed"; |
|
73
|
|
|
|
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($exception instanceof NotFoundHttpException) { |
|
78
|
|
|
$this->statusCode = Response::HTTP_NOT_FOUND; |
|
79
|
|
|
$this->severity = LogLevel::INFO; |
|
80
|
|
|
} else { |
|
81
|
|
|
if ($exception instanceof MethodNotAllowedHttpException) { |
|
82
|
|
|
$this->statusCode = Response::HTTP_METHOD_NOT_ALLOWED; |
|
83
|
|
|
$this->severity = LogLevel::WARNING; |
|
84
|
|
|
} elseif ($exception instanceof AuthenticationException) { |
|
85
|
|
|
$this->statusCode = Response::HTTP_UNAUTHORIZED; |
|
86
|
|
|
$this->severity = LogLevel::WARNING; |
|
87
|
|
|
} else { |
|
88
|
|
|
if (strlen((string)$code) !== 3) { |
|
89
|
|
|
$this->statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
|
90
|
|
|
$this->severity = LogLevel::CRITICAL; |
|
91
|
|
|
} else { |
|
92
|
|
|
$class = (int)substr((string)$code, 0, 1); |
|
93
|
|
|
switch ($class) { |
|
94
|
|
|
case 4: |
|
95
|
|
|
$this->statusCode = Response::HTTP_BAD_REQUEST; |
|
96
|
|
|
$this->severity = LogLevel::NOTICE; |
|
97
|
|
|
break; |
|
98
|
|
|
case 5: |
|
99
|
|
|
$this->statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
|
100
|
|
|
$this->severity = LogLevel::ERROR; |
|
101
|
|
|
break; |
|
102
|
|
|
default: |
|
103
|
|
|
$this->statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
|
104
|
|
|
$this->severity = LogLevel::CRITICAL; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
$this->message = Response::$statusTexts[$this->statusCode]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return Request |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getRequest(): Request |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->request; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return \Exception |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getException(): \Exception |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->exception; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getLogRef(): string |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->logRef; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getMessage(): string |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->message; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getSeverity(): string |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->severity; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return int |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getStatusCode(): int |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->statusCode; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|