vendor/assoconnect/graphql-mutation-validator-bundle/Formatter/Formatter.php line 11

Open in your IDE?
  1. <?php
  2. namespace AssoConnect\GraphQLMutationValidatorBundle\Formatter;
  3. use AssoConnect\GraphQLMutationValidatorBundle\Exception\UserException;
  4. use Overblog\GraphQLBundle\Event\ErrorFormattingEvent;
  5. Class Formatter
  6. {
  7.     public function onErrorFormatting(ErrorFormattingEvent $event) :void
  8.     {
  9.         // We need getPrevious as GraphQL wraps the thrown exception in a GraphQL\Error\Error
  10.         $error $event->getError()->getPrevious();
  11.         if($error instanceof UserException)
  12.         {
  13.             // Lists all the Symfony violations
  14.             $state = array();
  15.             $code = array();
  16.             $violations $error->getViolations();
  17.             foreach($violations as $violation)
  18.             {
  19.                 /** @var $violation \Symfony\Component\Validator\ConstraintViolationInterface */
  20.                 $state[$violation->getPropertyPath()][] = $violation->getMessage();
  21.                 $code[$violation->getPropertyPath()][] = $violation->getCode();
  22.             }
  23.             // Adds state & code to the formatted error
  24.             $formattedError $event->getFormattedError();
  25.             $formattedError->offsetSet('state'$state);
  26.             $formattedError->offsetSet('code'$code);
  27.         }
  28.     }
  29. }