public function testFallbackWhenNotSupported() { $request = new Request(); $request->setRequestFormat('html'); $format = ErrorFormatGuesser::guessErrorFormat($request, ['xml' => ['text/xml'], 'jsonld' => ['application/ld+json', 'application/json']]); $this->assertEquals('xml', $format['key']); $this->assertEquals('text/xml', $format['value'][0]); }
/** * Returns a list of violations normalized in the Hydra format. * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if (!$exception instanceof ValidationException) { return; } $format = ErrorFormatGuesser::guessErrorFormat($event->getRequest(), $this->errorFormats); $event->setResponse(new Response($this->serializer->serialize($exception->getConstraintViolationList(), $format['key']), Response::HTTP_BAD_REQUEST, ['Content-Type' => sprintf('%s; charset=utf-8', $format['value'][0]), 'X-Content-Type-Options' => 'nosniff', 'X-Frame-Options' => 'deny'])); }
/** * Converts a an exception to a JSON response. * * @param FlattenException $exception * @param Request $request * * @return Response */ public function __invoke(FlattenException $exception, Request $request) : Response { $exceptionClass = $exception->getClass(); foreach ($this->exceptionToStatus as $class => $status) { if (is_a($exceptionClass, $class, true)) { $exception->setStatusCode($status); break; } } $headers = $exception->getHeaders(); $format = ErrorFormatGuesser::guessErrorFormat($request, $this->errorFormats); $headers['Content-Type'] = sprintf('%s; charset=utf-8', $format['value'][0]); $headers['X-Content-Type-Options'] = 'nosniff'; $headers['X-Frame-Options'] = 'deny'; return new Response($this->serializer->serialize($exception, $format['key']), $exception->getStatusCode(), $headers); }