Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Jul 4, 2018. It is now read-only.

Commit 684812b

Browse filesBrowse files
committed
bug #1613 Allow setting custom status code on exception response (hkdobrev)
This PR was merged into the 2.3.x-dev branch. Discussion ---------- Allow setting custom status code on exception response Closes #1450. Prior and related PRs and issues: - symfony/symfony#19822 - symfony/symfony-docs@5f0becf - symfony/symfony-docs#9336 Commits ------- 7103511 Allow setting custom status code on exception response
2 parents 013b71b + 7103511 commit 684812b
Copy full SHA for 684812b

File tree

Expand file treeCollapse file tree

2 files changed

+17
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-4
lines changed

‎doc/usage.rst

Copy file name to clipboardExpand all lines: doc/usage.rst
+16-3Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,23 @@ setting a more specific type hint for the Closure argument::
516516

517517
As Silex ensures that the Response status code is set to the most
518518
appropriate one depending on the exception, setting the status on the
519-
response won't work. If you want to overwrite the status code, set the
520-
``X-Status-Code`` header::
519+
response alone won't work. If you want to overwrite the status code
520+
of the exception response, which you should not without a good reason, call
521+
``GetResponseForExceptionEvent::allowCustomResponseCode()`` first and then
522+
then set the status code on the response as normal. The kernel will
523+
now use your status code when sending the response to the client.
524+
The ``GetResponseForExceptionEvent`` is passed to the error callback as a 4th parameter::
521525

522-
return new Response('Error', 404 /* ignored */, array('X-Status-Code' => 200));
526+
use Symfony\Component\HttpFoundation\Response;
527+
use Symfony\Component\HttpFoundation\Request;
528+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
529+
530+
$app->error(function (\Exception $e, Request $request, $code, GetResponseForExceptionEvent $event) {
531+
$event->allowCustomResponseCode();
532+
$response = new Response('No Content', 204);
533+
534+
return $response;
535+
});
523536

524537
If you want to use a separate error handler for logging, make sure you register
525538
it with a higher priority than response error handlers, because once a response

‎src/Silex/ExceptionListenerWrapper.php

Copy file name to clipboardExpand all lines: src/Silex/ExceptionListenerWrapper.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __invoke(GetResponseForExceptionEvent $event)
5050

5151
$code = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
5252

53-
$response = call_user_func($this->callback, $exception, $event->getRequest(), $code);
53+
$response = call_user_func($this->callback, $exception, $event->getRequest(), $code, $event);
5454

5555
$this->ensureResponse($response, $event);
5656
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.