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