Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ExceptionListener | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| onKernelException | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\EventListener; |
| 6 | |
| 7 | use App\Exception\Service\UserService\UserExistsException; |
| 8 | use App\Exception\Service\UserService\UserNotFoundException; |
| 9 | use Fig\Http\Message\StatusCodeInterface as StatusCode; |
| 10 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 11 | use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
| 12 | use Throwable; |
| 13 | |
| 14 | class ExceptionListener |
| 15 | { |
| 16 | public function onKernelException(ExceptionEvent $event): void |
| 17 | { |
| 18 | $exception = $event->getThrowable(); |
| 19 | if ($exception instanceof UserExistsException) { |
| 20 | $event->setResponse(new JsonResponse( |
| 21 | data: ['error' => $exception->getMessage()], |
| 22 | status: StatusCode::STATUS_UNPROCESSABLE_ENTITY, |
| 23 | )); |
| 24 | return; |
| 25 | } |
| 26 | if ($exception instanceof UserNotFoundException) { |
| 27 | $event->setResponse(new JsonResponse( |
| 28 | data: ['error' => $exception->getMessage()], |
| 29 | status: StatusCode::STATUS_NOT_FOUND, |
| 30 | )); |
| 31 | return; |
| 32 | } |
| 33 | if ($exception instanceof Throwable) { |
| 34 | $event->setResponse(new JsonResponse( |
| 35 | data: ['error' => 'I have no idea that\'s happening here'], |
| 36 | status: StatusCode::STATUS_INTERNAL_SERVER_ERROR, |
| 37 | )); |
| 38 | } |
| 39 | } |
| 40 | } |