-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel][FrameworkBundle] Turn HTTP exceptions to HTTP status codes by default #27519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,10 @@ | |
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Debug\Exception\FlattenException; | ||
use Symfony\Component\Debug\ExceptionHandler; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; | ||
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; | ||
|
@@ -33,12 +35,17 @@ class ExceptionListener implements EventSubscriberInterface | |
protected $controller; | ||
protected $logger; | ||
protected $debug; | ||
private $charset; | ||
private $fileLinkFormat; | ||
private $isTerminating = false; | ||
|
||
public function __construct($controller, LoggerInterface $logger = null, $debug = false) | ||
public function __construct($controller, LoggerInterface $logger = null, $debug = false, string $charset = null, $fileLinkFormat = null) | ||
{ | ||
$this->controller = $controller; | ||
$this->logger = $logger; | ||
$this->debug = $debug; | ||
$this->charset = $charset; | ||
$this->fileLinkFormat = $fileLinkFormat; | ||
} | ||
|
||
public function logKernelException(GetResponseForExceptionEvent $event) | ||
|
@@ -50,6 +57,17 @@ public function logKernelException(GetResponseForExceptionEvent $event) | |
|
||
public function onKernelException(GetResponseForExceptionEvent $event) | ||
{ | ||
if (null === $this->controller) { | ||
if (!$event->isMasterRequest()) { | ||
return; | ||
} | ||
if (!$this->isTerminating) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for the record, the state managed by this property ensures that we don't break apps that handle exceptions outside of the stack: try {
$kernel->handle($request);
} catch (SomeException $e) {
// ...
} as required e.g. in some framework migration strategies. |
||
$this->isTerminating = true; | ||
|
||
return; | ||
} | ||
$this->isTerminating = false; | ||
} | ||
$exception = $event->getException(); | ||
$request = $this->duplicateRequest($exception, $event->getRequest()); | ||
$eventDispatcher = func_num_args() > 2 ? func_get_arg(2) : null; | ||
|
@@ -85,6 +103,11 @@ public function onKernelException(GetResponseForExceptionEvent $event) | |
} | ||
} | ||
|
||
public function reset() | ||
{ | ||
$this->isTerminating = false; | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
|
@@ -123,8 +146,12 @@ protected function logException(\Exception $exception, $message) | |
protected function duplicateRequest(\Exception $exception, Request $request) | ||
{ | ||
$attributes = array( | ||
'_controller' => $this->controller, | ||
'exception' => FlattenException::create($exception), | ||
'exception' => $exception = FlattenException::create($exception), | ||
'_controller' => $this->controller ?: function () use ($exception) { | ||
$handler = new ExceptionHandler($this->debug, $this->charset, $this->fileLinkFormat); | ||
|
||
return new Response($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders()); | ||
}, | ||
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null, | ||
); | ||
$request = $request->duplicate(null, null, $attributes); | ||
|
Uh oh!
There was an error while loading. Please reload this page.