src/EventSubscriber/InvalidIRIExceptionSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use ApiPlatform\Core\Exception\InvalidArgumentException;
  5. use Doctrine\DBAL\Exception\DriverException;
  6. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  7. use Doctrine\DBAL\Types\ConversionException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Serializer\Exception\{
  13.     NotEncodableValueException,
  14.     UnexpectedValueException,
  15. };
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class InvalidIRIExceptionSubscriber implements EventSubscriberInterface
  18. {
  19.     public const SYNTAX_ERROR_EXCEPTIONS = [
  20.         NotEncodableValueException::class,
  21.     ];
  22.     public const UNEXPECTED_VALUE_EXCEPTION = [
  23.         UnexpectedValueException::class,
  24.     ];
  25.     public const CONVERSION_ERROR_EXCEPTION = [
  26.         ConversionException::class,
  27.         DriverException::class,
  28.     ];
  29.     public const FOREIGN_KEY_EXCEPTION = [
  30.         ForeignKeyConstraintViolationException::class,
  31.     ];
  32.     public function __construct(private TranslatorInterface $translator)
  33.     {
  34.     }
  35.     public function onRequest(ExceptionEvent $event): void
  36.     {
  37.         $route $event->getRequest()->attributes->get('_route');
  38.         $content = (string) $event->getRequest()->getContent();
  39.         $throwable \get_class($event->getThrowable());
  40.         $mes $event->getThrowable()->getMessage();
  41.         if (\in_array($throwableself::CONVERSION_ERROR_EXCEPTION)) {
  42.             $event->setThrowable(new NotFoundHttpException('Data error: ' $mes));
  43.         }
  44.         if (\in_array($throwableself::FOREIGN_KEY_EXCEPTION)) {
  45.             $event->setThrowable(new InvalidArgumentException($mes));
  46.         }
  47.         if (\in_array($throwableself::SYNTAX_ERROR_EXCEPTIONS)) {
  48.             $event->setThrowable(new InvalidArgumentException('Syntax error: ' $mes));
  49.         }
  50.         if (\in_array($route, ['api_units_delete_item']) && \str_contains($mes'SQLSTATE[23503]')) {
  51.             $event->setThrowable(new InvalidArgumentException('Entity with relations cannot be removed'));
  52.         }
  53.         try {
  54.             $data \json_decode($contenttrueflagsJSON_THROW_ON_ERROR);
  55.         } catch (\Exception) {
  56.             return;
  57.         }
  58.         if (\in_array($route, ['api_time_entries_open_collection''api_time_entries_post_collection'])
  59.             && \array_key_exists('project'$data) && (!\is_string($data['project']) || empty(\trim($data['project'])))
  60.         ) {
  61.             $message $this->translator->trans('The field \'field_name\' must not be an empty string', ['field_name' => 'project']);
  62.             $event->setThrowable(new InvalidArgumentException($message));
  63.         }
  64.         if (\in_array($throwableself::UNEXPECTED_VALUE_EXCEPTION)) {
  65.             $message $this->translator->trans('The entity does not exist: ');
  66.             $event->setThrowable(new InvalidArgumentException($message \str_replace('Invalid IRI '''$mes)));
  67.         }
  68.     }
  69.     public static function getSubscribedEvents(): array
  70.     {
  71.         return [
  72.             KernelEvents::EXCEPTION => ['onRequest'EventPriorities::PRE_RESPOND],
  73.         ];
  74.     }
  75. }