src/EventListener/ExceptionListener.php
public function onKernelException(GetResponseForExceptionEvent $event): void
    {
        $request = $event->getRequest();

        if ($this->kernelDebug && $this->debugParameter && $request->query->has($this->debugParameter)) {
            return;
        }

        $exception = $event->getException();
        $errorPresentation = $this->errorPresentationFactory->create($exception);

        if (!$errorPresentation) {
            return;
        }

        if (!$errorPresentation->getResource()) {
            // The presentation not have resource. Send response with only status code.
            $event->setResponse(new Response('', $errorPresentation->getStatusCode()));

            return;
        }

        $acceptMediaType = null;

        try {
            $serializer = $this->serializerResolver->resolveByMediaTypes(
                get_class($errorPresentation->getResource()),
                $request->getAcceptableContentTypes(),
                $acceptMediaType
            );
        } catch (ResourceSerializerNotFoundException $e) {
            // Cannot resolve the serializer for accept media types.

            return;
        }

        $context = $this->serializationContextCollector->collect();
        $serializedData = $serializer->serialize($errorPresentation->getResource(), $context);

        $response = new Response($serializedData, $errorPresentation->getStatusCode(), [
            'Content-Type' => $acceptMediaType,
        ]);

        $event->setResponse($response);
    }
public function onKernelException(GetResponseForExceptionEvent $event): void
    {
        $request = $event->getRequest();

        if ($this->kernelDebug && $this->debugParameter && $request->query->has($this->debugParameter)) {
            return;
        }

        $exception = $event->getException();
        $errorPresentation = $this->errorPresentationFactory->create($exception);

        if (!$errorPresentation) {
            return;
        }

        if (!$errorPresentation->getResource()) {
            // The presentation not have resource. Send response with only status code.
            $event->setResponse(new Response('', $errorPresentation->getStatusCode()));

            return;
        }

        $acceptMediaType = null;

        try {
            $serializer = $this->serializerResolver->resolveByMediaTypes(
                get_class($errorPresentation->getResource()),
                $request->getAcceptableContentTypes(),
                $acceptMediaType
            );
        } catch (ResourceSerializerNotFoundException $e) {
            // Cannot resolve the serializer for accept media types.

            return;
        }

        $context = $this->serializationContextCollector->collect();
        $serializedData = $serializer->serialize($errorPresentation->getResource(), $context);

        $response = new Response($serializedData, $errorPresentation->getStatusCode(), [
            'Content-Type' => $acceptMediaType,
        ]);

        $event->setResponse($response);
    }
public function onKernelException ( GetResponseForExceptionEvent $ event ) : void { $ request = $ event -> getRequest ( ) ; if ( $ this -> kernelDebug && $ this -> debugParameter && $ request -> query -> has ( $ this -> debugParameter ) ) { return ; } $ exception = $ event -> getException ( ) ; $ errorPresentation = $ this -> errorPresentationFactory -> create ( $ exception ) ; if ( ! $ errorPresentation ) { return ; } if ( ! $ errorPresentation -> getResource ( ) ) { // The presentation not have resource. Send response with only status code. $ event -> setResponse ( new Response ( '' , $ errorPresentation -> getStatusCode ( ) ) ) ; return ; } $ acceptMediaType = null ; try { $ serializer = $ this -> serializerResolver -> resolveByMediaTypes ( get_class ( $ errorPresentation -> getResource ( ) ) , $ request -> getAcceptableContentTypes ( ) , $ acceptMediaType ) ; } catch ( ResourceSerializerNotFoundException $ e ) { // Cannot resolve the serializer for accept media types. return ; } $ context = $ this -> serializationContextCollector -> collect ( ) ; $ serializedData = $ serializer -> serialize ( $ errorPresentation -> getResource ( ) , $ context ) ; $ response = new Response ( $ serializedData , $ errorPresentation -> getStatusCode ( ) , [ 'Content-Type' => $ acceptMediaType , ] ) ; $ event -> setResponse ( $ response ) ; }
Render the exception for sending to client

@param GetResponseForExceptionEvent $event
Render the exception for sending to client