serializer = $serializer; $this->serializerFormats = $serializer_formats; } /** * {@inheritdoc} */ protected function getHandledFormats() { return $this->serializerFormats; } /** * {@inheritdoc} */ protected static function getPriority() { // This will fire after the most common HTML handler, since HTML requests // are still more common than HTTP requests. return -75; } /** * Handles all 4xx errors for all serialization failures. * * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event * The event to process. */ public function on4xx(GetResponseForExceptionEvent $event) { /** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */ $exception = $event->getException(); $request = $event->getRequest(); $format = $request->getRequestFormat(); $content = ['message' => $event->getException()->getMessage()]; $encoded_content = $this->serializer->serialize($content, $format); $headers = $exception->getHeaders(); // Add the MIME type from the request to send back in the header. $headers['Content-Type'] = $request->getMimeType($format); $response = new Response($encoded_content, $exception->getStatusCode(), $headers); $event->setResponse($response); } }