isJsonApiExceptionEvent($event)) { return; } if (($exception = $event->getThrowable()) && !$exception instanceof HttpException) { $exception = new HttpException(500, $exception->getMessage(), $exception); $event->setThrowable($exception); } $this->setEventResponse($event, $exception->getStatusCode()); } /** * {@inheritdoc} */ protected function setEventResponse(ExceptionEvent $event, $status) { /** @var \Symfony\Component\HttpKernel\Exception\HttpException $exception */ $exception = $event->getThrowable(); $document = new JsonApiDocumentTopLevel(new ErrorCollection([$exception]), new NullIncludedData(), new LinkCollection([])); if ($event->getRequest()->isMethodCacheable()) { $response = new CacheableResourceResponse($document, $exception->getStatusCode(), $exception->getHeaders()); $response->addCacheableDependency($exception); } else { $response = new ResourceResponse($document, $exception->getStatusCode(), $exception->getHeaders()); } $event->setResponse($response); } /** * Check if the error should be formatted using JSON:API. * * The JSON:API format is supported if the format is explicitly set or the * request is for a known JSON:API route. * * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $exception_event * The exception event. * * @return bool * TRUE if it needs to be formatted using JSON:API. FALSE otherwise. */ protected function isJsonApiExceptionEvent(ExceptionEvent $exception_event) { $request = $exception_event->getRequest(); $parameters = $request->attributes->all(); return $request->getRequestFormat() === 'api_json' || (bool) Routes::getResourceTypeNameFromParameters($parameters); } }