public function onError(GetResponseForExceptionEvent $event)
    {
        if ($event->getRequest()->isXmlHttpRequest()) {
            $exception = $event->getException();
            if ($exception instanceof InvalidDataException) {
                // return correct status code for invalid data sent by the user
                $response = new JsonResponse($exception->getErrors(), 422);
            } else {
                $response = new JsonResponse([
                    'message' => $exception->getMessage(),
                    'trace' => $exception->getTrace(),
                    //if <200, not http error code
                ], $exception->getCode() < 200 ? 500 : $exception->getCode());
            }

            $event->setResponse($response);
        }
    }
public function onError(GetResponseForExceptionEvent $event)
    {
        if ($event->getRequest()->isXmlHttpRequest()) {
            $exception = $event->getException();
            if ($exception instanceof InvalidDataException) {
                // return correct status code for invalid data sent by the user
                $response = new JsonResponse($exception->getErrors(), 422);
            } else {
                $response = new JsonResponse([
                    'message' => $exception->getMessage(),
                    'trace' => $exception->getTrace(),
                    //if <200, not http error code
                ], $exception->getCode() < 200 ? 500 : $exception->getCode());
            }

            $event->setResponse($response);
        }
    }
public function onError ( GetResponseForExceptionEvent $ event ) { if ( $ event -> getRequest ( ) -> isXmlHttpRequest ( ) ) { $ exception = $ event -> getException ( ) ; if ( $ exception instanceof InvalidDataException ) { // return correct status code for invalid data sent by the user $ response = new JsonResponse ( $ exception -> getErrors ( ) , 422 ) ; } else { $ response = new JsonResponse ( [ 'message' => $ exception -> getMessage ( ) , 'trace' => $ exception -> getTrace ( ) , //if <200, not http error code ] , $ exception -> getCode ( ) < 200 ? 500 : $ exception -> getCode ( ) ) ; } $ event -> setResponse ( $ response ) ; } }
Converts Exceptions in JSON for the async api.

@DI\Observe("kernel.exception", priority=99)

@param GetResponseForExceptionEvent $event
Converts Exceptions in JSON for the async api .