Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ExceptionSubscriber
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 getSubscribedEvents
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 onKernelException
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3namespace App\EventListener;
4
5use Symfony\Component\HttpFoundation\Response;
6use Symfony\Component\HttpKernel\Event\ExceptionEvent;
7use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
9class ExceptionSubscriber extends AbstractController
10{
11    /**
12     * Returns the subscribed events for the ExceptionSubscriber.
13     *
14     * @return array<mixed> The subscribed events.
15     */
16    public static function getSubscribedEvents(): array
17    {
18        return [
19            'kernel.exception' => 'onKernelException',
20        ];
21    }
22    /**
23     * Event listener method that handles kernel exceptions.
24     *
25     * @param ExceptionEvent $event The exception event object.
26     * @return void
27     */
28    /**
29     * @codeCoverageIgnore
30     */
31    public function onKernelException(ExceptionEvent $event): void
32    {
33        $exception = $event->getThrowable();
34
35        $response = new Response();
36        $response->setContent('Une erreur est survenue : ' . $exception->getMessage());
37        $response->setStatusCode($exception->getCode());
38
39        $event->setResponse($response);
40
41        // We display the error message in a banner
42        $this->addFlash('danger', 'Une erreur est survenue : ' . $exception->getMessage());
43    }
44}