Commit 71035117 authored by Haralan Dobrev's avatar Haralan Dobrev
parent eb559d4c
......@@ -516,10 +516,23 @@ setting a more specific type hint for the Closure argument::
As Silex ensures that the Response status code is set to the most
appropriate one depending on the exception, setting the status on the
response won't work. If you want to overwrite the status code, set the
``X-Status-Code`` header::
response alone won't work. If you want to overwrite the status code
of the exception response, which you should not without a good reason, call
``GetResponseForExceptionEvent::allowCustomResponseCode()`` first and then
then set the status code on the response as normal. The kernel will
now use your status code when sending the response to the client.
The ``GetResponseForExceptionEvent`` is passed to the error callback as a 4th parameter::
return new Response('Error', 404 /* ignored */, array('X-Status-Code' => 200));
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
$app->error(function (\Exception $e, Request $request, $code, GetResponseForExceptionEvent $event) {
$event->allowCustomResponseCode();
$response = new Response('No Content', 204);
return $response;
});
If you want to use a separate error handler for logging, make sure you register
it with a higher priority than response error handlers, because once a response
......
......@@ -50,7 +50,7 @@ class ExceptionListenerWrapper
$code = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
$response = call_user_func($this->callback, $exception, $event->getRequest(), $code);
$response = call_user_func($this->callback, $exception, $event->getRequest(), $code, $event);
$this->ensureResponse($response, $event);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment