Skip to content

Commit 9a16b0e

Browse files
mpbzhGuilhemN
authored andcommitted
Make sure an Exception in the FormatNegotiator causes a 406 response
1 parent f80dd96 commit 9a16b0e

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

Controller/ExceptionController.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,16 @@ public function showAction(Request $request, $exception, DebugLoggerInterface $l
7575
));
7676
}
7777

78-
$format = $this->getFormat($request, $request->getRequestFormat());
78+
try {
79+
$format = $this->getFormat($request, $request->getRequestFormat());
80+
} catch (\Exception $e) {
81+
$format = null;
82+
}
7983
if (null === $format) {
8084
$message = 'No matching accepted Response format could be determined, while handling: ';
8185
$message .= $this->getExceptionMessage($exception);
8286

83-
return new Response($message, Codes::HTTP_NOT_ACCEPTABLE, $exception->getHeaders());
87+
return $this->createPlainResponse($message, Codes::HTTP_NOT_ACCEPTABLE, $exception->getHeaders());
8488
}
8589

8690
$currentContent = $this->getAndCleanOutputBuffering();
@@ -105,12 +109,28 @@ public function showAction(Request $request, $exception, DebugLoggerInterface $l
105109
} catch (\Exception $e) {
106110
$message = 'An Exception was thrown while handling: ';
107111
$message .= $this->getExceptionMessage($exception);
108-
$response = new Response($message, Codes::HTTP_INTERNAL_SERVER_ERROR, $exception->getHeaders());
112+
$response = $this->createPlainResponse($message, Codes::HTTP_INTERNAL_SERVER_ERROR, $exception->getHeaders());
109113
}
110114

111115
return $response;
112116
}
113117

118+
/**
119+
* Returns a Response Object with content type text/plain.
120+
*
121+
* @param string $content
122+
* @param int $status
123+
* @param array $headers
124+
*
125+
* @return Response
126+
*/
127+
private function createPlainResponse($content, $status = 200, $headers = array())
128+
{
129+
$headers['content-type'] = 'text/plain';
130+
131+
return new Response($content, $status, $headers);
132+
}
133+
114134
/**
115135
* Gets and cleans any content that was already outputted.
116136
*

0 commit comments

Comments
 (0)