Skip to content

Commit

Permalink
clean and refactor exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 22, 2018
1 parent 15bd214 commit f9162c9
Showing 1 changed file with 50 additions and 64 deletions.
114 changes: 50 additions & 64 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function report(Exception $e)
try {
$logger = $this->container->make(LoggerInterface::class);
} catch (Exception $ex) {
throw $e; // throw the original exception
throw $e;
}

$logger->error(
Expand Down Expand Up @@ -248,14 +248,9 @@ protected function convertValidationExceptionToResponse(ValidationException $e,
*/
protected function invalid($request, ValidationException $exception)
{
$url = $exception->redirectTo ?? url()->previous();

return redirect($url)
->withInput($request->except($this->dontFlash))
->withErrors(
$exception->errors(),
$exception->errorBag
);
return redirect($exception->redirectTo ?? url()->previous())
->withInput($request->except($this->dontFlash))
->withErrors($exception->errors(), $exception->errorBag);
}

/**
Expand Down Expand Up @@ -283,9 +278,7 @@ protected function invalidJson($request, ValidationException $exception)
protected function prepareResponse($request, Exception $e)
{
if (! $this->isHttpException($e) && config('app.debug')) {
return $this->toIlluminateResponse(
$this->convertExceptionToResponse($e), $e
);
return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);
}

if (! $this->isHttpException($e)) {
Expand All @@ -305,21 +298,28 @@ protected function prepareResponse($request, Exception $e)
*/
protected function convertExceptionToResponse(Exception $e)
{
$headers = $this->isHttpException($e) ? $e->getHeaders() : [];

$statusCode = $this->isHttpException($e) ? $e->getStatusCode() : 500;
return SymfonyResponse::create(
$this->renderExceptionContent($e),
$this->isHttpException($e) ? $e->getStatusCode() : 500,
$this->isHttpException($e) ? $e->getHeaders() : []
);
}

/**
* Get the response content for the given exception.
*
* @param \Exception $e
* @return string
*/
protected function renderExceptionContent(Exception $e)
{
try {
$content = config('app.debug') && class_exists(Whoops::class)
? $this->renderExceptionWithWhoops($e)
: $this->renderExceptionWithSymfony($e, config('app.debug'));
return config('app.debug') && class_exists(Whoops::class)
? $this->renderExceptionWithWhoops($e)
: $this->renderExceptionWithSymfony($e, config('app.debug'));
} catch (Exception $e) {
$content = $content ?? $this->renderExceptionWithSymfony($e, config('app.debug'));
return $this->renderExceptionWithSymfony($e, config('app.debug'));
}

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

/**
Expand All @@ -339,6 +339,16 @@ protected function renderExceptionWithWhoops(Exception $e)
})->handleException($e);
}

/**
* Get the Whoops handler for the application.
*
* @return \Whoops\Handler\Handler
*/
protected function whoopsHandler()
{
return (new WhoopsHandler)->forDebug();
}

/**
* Render an exception to a string using Symfony.
*
Expand All @@ -354,58 +364,36 @@ protected function renderExceptionWithSymfony(Exception $e, $debug)
}

/**
* Get the Whoops handler for the application.
* Render the given HttpException.
*
* @return \Whoops\Handler\Handler
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function whoopsHandler()
protected function renderHttpException(HttpException $e)
{
return tap(new PrettyPageHandler, function ($handler) {
$files = new Filesystem;
$this->registerErrorViewPaths();

$handler->handleUnconditionally(true);

foreach (config('app.debug_blacklist', []) as $key => $secrets) {
foreach ($secrets as $secret) {
$handler->blacklist($key, $secret);
}
}

if (config('app.editor', false)) {
$handler->setEditor(config('app.editor'));
}
if (view()->exists($view = "errors::{$e->getStatusCode()}")) {
return response()->view($view, [
'exception' => $e, 'errors' => new ViewErrorBag,
], $e->getStatusCode(), $e->getHeaders());
}

$handler->setApplicationPaths(
array_flip(Arr::except(
array_flip($files->directories(base_path())), [base_path('vendor')]
))
);
});
return $this->convertExceptionToResponse($e);
}

/**
* Render the given HttpException.
* Register the error template hint paths.
*
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e
* @return \Symfony\Component\HttpFoundation\Response
* @return void
*/
protected function renderHttpException(HttpException $e)
protected function registerErrorViewPaths()
{
$status = $e->getStatusCode();

$paths = collect(config('view.paths'));

view()->replaceNamespace('errors', $paths->map(function ($path) {
return "{$path}/errors";
})->push(__DIR__.'/views')->all());

if (view()->exists($view = "errors::{$status}")) {
return response()->view($view, [
'exception' => $e, 'errors' => new ViewErrorBag,
], $status, $e->getHeaders());
}

return $this->convertExceptionToResponse($e);
}

/**
Expand Down Expand Up @@ -439,12 +427,10 @@ protected function toIlluminateResponse($response, Exception $e)
*/
protected function prepareJsonResponse($request, Exception $e)
{
$status = $this->isHttpException($e) ? $e->getStatusCode() : 500;

$headers = $this->isHttpException($e) ? $e->getHeaders() : [];

return new JsonResponse(
$this->convertExceptionToArray($e), $status, $headers,
$this->convertExceptionToArray($e),
$this->isHttpException($e) ? $e->getStatusCode() : 500,
$this->isHttpException($e) ? $e->getHeaders() : [],
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
);
}
Expand Down

0 comments on commit f9162c9

Please sign in to comment.