From f9162c9898c58be18f166e1832699b83602404b1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 22 Mar 2018 11:47:22 -0700 Subject: [PATCH] clean and refactor exception handler --- .../Foundation/Exceptions/Handler.php | 114 ++++++++---------- 1 file changed, 50 insertions(+), 64 deletions(-) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 3975a3caddb9..0d152417184c 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -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( @@ -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); } /** @@ -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)) { @@ -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 - ); } /** @@ -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. * @@ -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); } /** @@ -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 ); }