Skip to content
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

[5.5] Fix Whoops exception rendering #19471

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 44 additions & 21 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,22 @@ protected function prepareResponse($request, Exception $e)
*/
protected function convertExceptionToResponse(Exception $e)
{
if (config('app.debug')) {
return SymfonyResponse::create(
$this->renderExceptionWithWhoops($e), $e->getStatusCode(), $e->getHeaders()
);
} else {
$e = FlattenException::create($e);
$headers = $this->isHttpException($e) ? $e->getHeaders() : [];
$statusCode = $this->isHttpException($e) ? $e->getStatusCode() : 500;

return SymfonyResponse::create(
(new SymfonyExceptionHandler(false))->getHtml($e),
$e->getStatusCode(),
$e->getHeaders()
);
$showStackTraces = config('app.debug');

try {
$content = $showStackTraces
? $this->renderExceptionWithWhoops($e)
: $this->renderExceptionWithSymfony($e, $showStackTraces);
} finally {
$content = $content ?? $this->renderExceptionWithSymfony($e, $showStackTraces);
}

return SymfonyResponse::create(
$content, $statusCode, $headers
);
}

/**
Expand All @@ -268,9 +271,26 @@ protected function convertExceptionToResponse(Exception $e)
*/
protected function renderExceptionWithWhoops(Exception $e)
{
return tap(
(new Whoops)->pushHandler($this->whoopsHandler())
)->handleException($e);
$whoops = tap(new Whoops, function ($whoops) {
$whoops->pushHandler($this->whoopsHandler());
$whoops->writeToOutput(false);
$whoops->allowQuit(false);
});

return $whoops->handleException($e);
}

/**
* Render an exception to a string using Symfony.
*
* @param \Exception $e
* @return string
*/
protected function renderExceptionWithSymfony(Exception $e, $showStackTraces)
{
$e = FlattenException::create($e);

return (new SymfonyExceptionHandler($showStackTraces))->getHtml($e);
}

/**
Expand All @@ -280,13 +300,16 @@ protected function renderExceptionWithWhoops(Exception $e)
*/
protected function whoopsHandler()
{
$files = new Filesystem;

return tap(new PrettyPageHandler)->setApplicationPaths(
array_flip(array_except(
array_flip($files->directories(base_path())), [base_path('vendor')]
))
);
return tap(new PrettyPageHandler, function ($handler) {
$files = new Filesystem;

$handler->handleUnconditionally(true);
$handler->setApplicationPaths(
array_flip(array_except(
array_flip($files->directories(base_path())), [base_path('vendor')]
))
);
});
}

/**
Expand Down