1 | <?php |
||
16 | class KernelExceptionListener |
||
17 | { |
||
18 | /** |
||
19 | * @var string[] |
||
20 | */ |
||
21 | private $paths; |
||
22 | |||
23 | /** |
||
24 | * @var bool |
||
25 | */ |
||
26 | private $debug = false; |
||
27 | |||
28 | 96 | public function __construct(array $paths) |
|
32 | |||
33 | 14 | public function onKernelException(GetResponseForExceptionEvent $event) |
|
34 | { |
||
35 | 14 | $exception = $event->getException(); |
|
36 | 14 | $request = $event->getRequest(); |
|
37 | 14 | if (!$this->isInterceptionPath($request)) { |
|
38 | 8 | return; |
|
39 | } |
||
40 | |||
41 | 6 | $data = null; |
|
42 | 6 | if ($this->debug) { |
|
43 | $data = [ |
||
44 | 6 | 'class' => get_class($exception), |
|
45 | 6 | 'message' => $exception->getMessage(), |
|
46 | 6 | 'trace' => $exception->getTrace() |
|
47 | ]; |
||
48 | } |
||
49 | |||
50 | 6 | $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
|
51 | |||
52 | 6 | if ($exception instanceof InsufficientAuthenticationException) { |
|
53 | $statusCode = Response::HTTP_UNAUTHORIZED; |
||
54 | } |
||
55 | |||
56 | 6 | if ($exception instanceof BadCredentialsException) { |
|
57 | $statusCode = Response::HTTP_UNAUTHORIZED; |
||
58 | } |
||
59 | |||
60 | 6 | $response = new JsonResponse($data); |
|
61 | |||
62 | 6 | if ($exception instanceof HttpExceptionInterface) { |
|
63 | 6 | $statusCode = $exception->getStatusCode(); |
|
64 | 6 | $response->setStatusCode($exception->getStatusCode()); |
|
65 | 6 | $response->headers->add($exception->getHeaders()); |
|
66 | } |
||
67 | |||
68 | 6 | $response->setStatusCode($statusCode); |
|
69 | |||
70 | 6 | $event->setResponse($response); |
|
71 | 6 | } |
|
72 | |||
73 | 14 | private function isInterceptionPath(?Request $request): bool |
|
87 | |||
88 | /** |
||
89 | * @return bool |
||
90 | */ |
||
91 | public function isDebug(): bool |
||
95 | |||
96 | /** |
||
97 | * @param bool $debug |
||
98 | */ |
||
99 | 96 | public function setDebug(bool $debug) |
|
103 | } |
||
104 |