-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExceptionListener.php
43 lines (37 loc) · 1.55 KB
/
ExceptionListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class ExceptionListener
{
private bool $debug;
public function __construct(KernelInterface $kernel)
{
$this->debug = $kernel->isDebug();
}
public function __invoke(ExceptionEvent $event): void
{
// You get the exception object from the received event
$exception = $event->getThrowable();
// Customize your response object to display the exception details
$response = new Response();
// HttpExceptionInterface is a special type of exception that
// holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}
$responseData = [
'status' => $response->getStatusCode(),
'message' => $this->debug ? $exception->getMessage() : 'An error occurred',
'trace' => $this->debug ? $exception->getTrace() : 'Enable debug mode for details.',
];
$response->setContent(json_encode($responseData, JSON_THROW_ON_ERROR));
// sends the modified response object to the event
$event->setResponse($response);
}
}