1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace M6Web\Bundle\ApiExceptionBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
6
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
7
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
8
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface; |
9
|
|
|
use M6Web\Bundle\ApiExceptionBundle\Manager\ExceptionManager; |
10
|
|
|
use M6Web\Bundle\ApiExceptionBundle\Exception\Interfaces\ExceptionInterface; |
11
|
|
|
use M6Web\Bundle\ApiExceptionBundle\Exception\Interfaces\HttpExceptionInterface; |
12
|
|
|
use M6Web\Bundle\ApiExceptionBundle\Exception\Interfaces\FlattenErrorExceptionInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ExceptionListener |
16
|
|
|
*/ |
17
|
|
|
class ExceptionListener |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var boolean |
21
|
|
|
*/ |
22
|
|
|
protected $stackTrace; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $default; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Kernel |
31
|
|
|
*/ |
32
|
|
|
protected $kernel; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ExceptionManager |
36
|
|
|
*/ |
37
|
|
|
protected $exceptionManager; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var boolean |
41
|
|
|
*/ |
42
|
|
|
protected $matchAll; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor |
46
|
|
|
* |
47
|
|
|
* @param Kernel $kernel |
48
|
|
|
* @param ExceptionManager $exceptionManager |
49
|
|
|
* @param boolean $matchAll |
50
|
|
|
* @param array $default |
51
|
|
|
* @param boolean $stackTrace |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
Kernel $kernel, |
55
|
|
|
ExceptionManager $exceptionManager, |
56
|
|
|
$matchAll, |
57
|
|
|
array $default, |
58
|
|
|
$stackTrace = false |
59
|
|
|
) { |
60
|
1 |
|
$this->kernel = $kernel; |
61
|
1 |
|
$this->exceptionManager = $exceptionManager; |
62
|
1 |
|
$this->matchAll = $matchAll; |
63
|
1 |
|
$this->default = $default; |
64
|
1 |
|
$this->stackTrace = $stackTrace; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Format response exception |
69
|
|
|
* |
70
|
|
|
* @param GetResponseForExceptionEvent $event |
71
|
|
|
*/ |
72
|
|
|
public function onKernelException(GetResponseForExceptionEvent $event) |
73
|
|
|
{ |
74
|
1 |
|
$exception = $event->getException(); |
75
|
|
|
|
76
|
1 |
|
if (!($event->getRequest()) |
77
|
1 |
|
|| ($this->matchAll === false && !$this->isApiException($exception)) |
78
|
1 |
|
) { |
79
|
1 |
|
return; |
80
|
|
|
} |
81
|
1 |
|
|
82
|
1 |
|
$data = []; |
83
|
|
|
|
84
|
1 |
|
if ($this->isApiException($exception)) { |
85
|
1 |
|
$exception = $this->exceptionManager->configure($exception); |
|
|
|
|
86
|
1 |
|
} |
87
|
|
|
|
88
|
1 |
|
$statusCode = $this->getStatusCode($exception); |
|
|
|
|
89
|
|
|
|
90
|
1 |
|
$data['error']['status'] = $statusCode; |
91
|
|
|
|
92
|
1 |
|
if ($code = $exception->getCode()) { |
93
|
1 |
|
$data['error']['code'] = $code; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
$data['error']['message'] = $this->getMessage($exception); |
|
|
|
|
97
|
|
|
|
98
|
1 |
|
if ($this->isFlattenErrorException($exception)) { |
|
|
|
|
99
|
1 |
|
$data['error']['errors'] = $exception->getFlattenErrors(); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
1 |
|
if ($this->stackTrace) { |
103
|
1 |
|
$data['error']['stack_trace'] = $exception->getTrace(); |
|
|
|
|
104
|
|
|
|
105
|
|
|
// Clean stacktrace to avoid circular reference or invalid type |
106
|
1 |
|
array_walk_recursive( |
107
|
1 |
|
$data['error']['stack_trace'], |
108
|
1 |
|
function(&$item) { |
109
|
1 |
|
if (is_object($item)) { |
110
|
|
|
$item = get_class($item); |
111
|
1 |
|
} elseif (is_resource($item)) { |
112
|
|
|
$item = get_resource_type($item); |
113
|
|
|
} |
114
|
1 |
|
} |
115
|
1 |
|
); |
116
|
1 |
|
} |
117
|
|
|
|
118
|
1 |
|
$response = new JsonResponse($data, $statusCode, $this->getHeaders($exception)); |
|
|
|
|
119
|
1 |
|
$event->setResponse($response); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get exception status code |
124
|
|
|
* |
125
|
|
|
* @param \Exception $exception |
126
|
|
|
* |
127
|
|
|
* @return integer |
128
|
|
|
*/ |
129
|
|
|
private function getStatusCode(\Exception $exception) |
130
|
|
|
{ |
131
|
1 |
|
$statusCode = $this->default['status']; |
132
|
|
|
|
133
|
|
|
if ($exception instanceof SymfonyHttpExceptionInterface |
134
|
1 |
|
|| $exception instanceof HttpExceptionInterface |
135
|
1 |
|
) { |
136
|
1 |
|
$statusCode = $exception->getStatusCode(); |
137
|
1 |
|
} |
138
|
|
|
|
139
|
1 |
|
return $statusCode; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get exception message |
144
|
|
|
* |
145
|
|
|
* @param \Exception $exception |
146
|
|
|
* |
147
|
|
|
* @return integer |
148
|
|
|
*/ |
149
|
|
|
private function getMessage(\Exception $exception) |
150
|
|
|
{ |
151
|
1 |
|
$message = $exception->getMessage(); |
152
|
|
|
|
153
|
1 |
|
if ($this->isApiException($exception)) { |
154
|
1 |
|
$message = $exception->getMessageWithVariables(); |
|
|
|
|
155
|
1 |
|
} |
156
|
|
|
|
157
|
1 |
|
return $message; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get exception headers |
162
|
|
|
* |
163
|
|
|
* @param \Exception $exception |
164
|
|
|
* |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
1 |
|
private function getHeaders(\Exception $exception) |
168
|
|
|
{ |
169
|
1 |
|
$headers = $this->default['headers']; |
170
|
|
|
|
171
|
|
|
if ($exception instanceof SymfonyHttpExceptionInterface |
172
|
1 |
|
|| $exception instanceof HttpExceptionInterface |
173
|
1 |
|
) { |
174
|
1 |
|
$headers = $exception->getHeaders(); |
175
|
1 |
|
} |
176
|
|
|
|
177
|
1 |
|
return $headers; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Is api exception |
182
|
|
|
* |
183
|
|
|
* @param \Exception $exception |
184
|
|
|
* |
185
|
|
|
* @return boolean |
186
|
|
|
*/ |
187
|
|
|
private function isApiException(\Exception $exception) |
188
|
|
|
{ |
189
|
1 |
|
if ($exception instanceof ExceptionInterface) { |
190
|
1 |
|
return true; |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Is flatten error exception |
198
|
|
|
* |
199
|
|
|
* @param \Exception $exception |
200
|
|
|
* |
201
|
|
|
* @return boolean |
202
|
|
|
*/ |
203
|
|
|
private function isFlattenErrorException(\Exception $exception) |
204
|
|
|
{ |
205
|
1 |
|
if ($exception instanceof FlattenErrorExceptionInterface) { |
206
|
1 |
|
return true; |
207
|
|
|
} |
208
|
|
|
|
209
|
1 |
|
return false; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: