Skip to content

Commit

Permalink
Make sure an Exception in the FormatNegotiator causes a 406 response
Browse files Browse the repository at this point in the history
  • Loading branch information
mpbzh authored and GuilhemN committed Nov 24, 2015
1 parent 4c24162 commit 1af7cab
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions Controller/ExceptionController.php
Expand Up @@ -60,12 +60,16 @@ protected function createExceptionWrapper(array $parameters)
*/
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
{
$format = $this->getFormat($request, $request->getRequestFormat());
try {
$format = $this->getFormat($request, $request->getRequestFormat());
} catch (\Exception $e) {
$format = null;
}
if (null === $format) {
$message = 'No matching accepted Response format could be determined, while handling: ';
$message .= $this->getExceptionMessage($exception);

return new Response($message, Response::HTTP_NOT_ACCEPTABLE, $exception->getHeaders());
return $this->createPlainResponse($message, Response::HTTP_NOT_ACCEPTABLE, $exception->getHeaders());
}

$currentContent = $this->getAndCleanOutputBuffering(
Expand All @@ -76,7 +80,6 @@ public function showAction(Request $request, FlattenException $exception, DebugL
$viewHandler = $this->container->get('fos_rest.view_handler');
$parameters = $this->getParameters($viewHandler, $currentContent, $code, $exception, $logger, $format);
$showException = $request->attributes->get('showException', $this->container->get('kernel')->isDebug());

try {
if (!$viewHandler->isFormatTemplating($format)) {
$parameters = $this->createExceptionWrapper($parameters);
Expand All @@ -93,12 +96,28 @@ public function showAction(Request $request, FlattenException $exception, DebugL
} catch (\Exception $e) {
$message = 'An Exception was thrown while handling: ';
$message .= $this->getExceptionMessage($exception);
$response = new Response($message, Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getHeaders());
$response = $this->createPlainResponse($message, Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getHeaders());
}

return $response;
}

/**
* Returns a Response Object with content type text/plain.
*
* @param string $content
* @param int $status
* @param array $headers
*
* @return Response
*/
private function createPlainResponse($content, $status, $headers)
{
$headers['content-type'] = 'text/plain';

return new Response($content, $status, $headers);
}

/**
* Gets and cleans any content that was already outputted.
*
Expand Down

0 comments on commit 1af7cab

Please sign in to comment.