src/Lms/SharedKernel/Application/LmsExceptionListener.php line 13

Open in your IDE?
  1. <?php
  2. namespace CodersLab\Lms\SharedKernel\Application;
  3. use CodersLab\Lms\SharedKernel\Application\Response\ExceptionMetadata;
  4. use CodersLab\Lms\SharedKernel\Application\Response\LmsApiResponse;
  5. use CodersLab\Lms\SharedKernel\Common\Exception\LmsApiException;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. final class LmsExceptionListener
  9. {
  10.     public function onKernelException(ExceptionEvent $event): void
  11.     {
  12.         $exception $event->getThrowable();
  13.         if (!$exception instanceof LmsApiException) {
  14.             return;
  15.         }
  16.         $metadata = new ExceptionMetadata(
  17.             $exception->getMessage(),
  18.             $exception->getCode()
  19.         );
  20.         $event->setResponse(
  21.             new LmsApiResponse(
  22.                 [],
  23.                 $metadata,
  24.                 $metadata->getCode() === Response::HTTP_INTERNAL_SERVER_ERROR $metadata->getCode()
  25.             )
  26.         );
  27.     }
  28. }