src/Exception/EventListener/ExceptionListener.php
public function kernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();

        if ($exception instanceof ValidationException) {
            return;
        }

        $data = [
            'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
            'title' =>'An error occurred'
        ];

        if (in_array($this->environment, ['dev', 'test'], true)) {
            $data['detail'] = $exception->getMessage();
            $data['trace'] = $exception->getTrace();
        }

        $request = $event->getRequest();
        $contentTypes = $request->getAcceptableContentTypes();
        $accept = array_shift($contentTypes);

        switch ($accept) {
            case 'application/json':
            case 'application/ld+json':
                $code = Response::HTTP_INTERNAL_SERVER_ERROR;

                if ($exception instanceof HttpException) {
                    $code = $exception->getStatusCode();
                }

                $response = new JsonResponse($data, $code);
                $event->setResponse($response);
                break;
        }
    }
public function kernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();

        if ($exception instanceof ValidationException) {
            return;
        }

        $data = [
            'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
            'title' =>'An error occurred'
        ];

        if (in_array($this->environment, ['dev', 'test'], true)) {
            $data['detail'] = $exception->getMessage();
            $data['trace'] = $exception->getTrace();
        }

        $request = $event->getRequest();
        $contentTypes = $request->getAcceptableContentTypes();
        $accept = array_shift($contentTypes);

        switch ($accept) {
            case 'application/json':
            case 'application/ld+json':
                $code = Response::HTTP_INTERNAL_SERVER_ERROR;

                if ($exception instanceof HttpException) {
                    $code = $exception->getStatusCode();
                }

                $response = new JsonResponse($data, $code);
                $event->setResponse($response);
                break;
        }
    }
Format uncaught exceptions into the proper format based on request

@param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event