Skip to content

Commit f9162c9

Browse files
committed
clean and refactor exception handler
1 parent 15bd214 commit f9162c9

File tree

1 file changed

+50
-64
lines changed

1 file changed

+50
-64
lines changed

src/Illuminate/Foundation/Exceptions/Handler.php

+50-64
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function report(Exception $e)
106106
try {
107107
$logger = $this->container->make(LoggerInterface::class);
108108
} catch (Exception $ex) {
109-
throw $e; // throw the original exception
109+
throw $e;
110110
}
111111

112112
$logger->error(
@@ -248,14 +248,9 @@ protected function convertValidationExceptionToResponse(ValidationException $e,
248248
*/
249249
protected function invalid($request, ValidationException $exception)
250250
{
251-
$url = $exception->redirectTo ?? url()->previous();
252-
253-
return redirect($url)
254-
->withInput($request->except($this->dontFlash))
255-
->withErrors(
256-
$exception->errors(),
257-
$exception->errorBag
258-
);
251+
return redirect($exception->redirectTo ?? url()->previous())
252+
->withInput($request->except($this->dontFlash))
253+
->withErrors($exception->errors(), $exception->errorBag);
259254
}
260255

261256
/**
@@ -283,9 +278,7 @@ protected function invalidJson($request, ValidationException $exception)
283278
protected function prepareResponse($request, Exception $e)
284279
{
285280
if (! $this->isHttpException($e) && config('app.debug')) {
286-
return $this->toIlluminateResponse(
287-
$this->convertExceptionToResponse($e), $e
288-
);
281+
return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);
289282
}
290283

291284
if (! $this->isHttpException($e)) {
@@ -305,21 +298,28 @@ protected function prepareResponse($request, Exception $e)
305298
*/
306299
protected function convertExceptionToResponse(Exception $e)
307300
{
308-
$headers = $this->isHttpException($e) ? $e->getHeaders() : [];
309-
310-
$statusCode = $this->isHttpException($e) ? $e->getStatusCode() : 500;
301+
return SymfonyResponse::create(
302+
$this->renderExceptionContent($e),
303+
$this->isHttpException($e) ? $e->getStatusCode() : 500,
304+
$this->isHttpException($e) ? $e->getHeaders() : []
305+
);
306+
}
311307

308+
/**
309+
* Get the response content for the given exception.
310+
*
311+
* @param \Exception $e
312+
* @return string
313+
*/
314+
protected function renderExceptionContent(Exception $e)
315+
{
312316
try {
313-
$content = config('app.debug') && class_exists(Whoops::class)
314-
? $this->renderExceptionWithWhoops($e)
315-
: $this->renderExceptionWithSymfony($e, config('app.debug'));
317+
return config('app.debug') && class_exists(Whoops::class)
318+
? $this->renderExceptionWithWhoops($e)
319+
: $this->renderExceptionWithSymfony($e, config('app.debug'));
316320
} catch (Exception $e) {
317-
$content = $content ?? $this->renderExceptionWithSymfony($e, config('app.debug'));
321+
return $this->renderExceptionWithSymfony($e, config('app.debug'));
318322
}
319-
320-
return SymfonyResponse::create(
321-
$content, $statusCode, $headers
322-
);
323323
}
324324

325325
/**
@@ -339,6 +339,16 @@ protected function renderExceptionWithWhoops(Exception $e)
339339
})->handleException($e);
340340
}
341341

342+
/**
343+
* Get the Whoops handler for the application.
344+
*
345+
* @return \Whoops\Handler\Handler
346+
*/
347+
protected function whoopsHandler()
348+
{
349+
return (new WhoopsHandler)->forDebug();
350+
}
351+
342352
/**
343353
* Render an exception to a string using Symfony.
344354
*
@@ -354,58 +364,36 @@ protected function renderExceptionWithSymfony(Exception $e, $debug)
354364
}
355365

356366
/**
357-
* Get the Whoops handler for the application.
367+
* Render the given HttpException.
358368
*
359-
* @return \Whoops\Handler\Handler
369+
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e
370+
* @return \Symfony\Component\HttpFoundation\Response
360371
*/
361-
protected function whoopsHandler()
372+
protected function renderHttpException(HttpException $e)
362373
{
363-
return tap(new PrettyPageHandler, function ($handler) {
364-
$files = new Filesystem;
374+
$this->registerErrorViewPaths();
365375

366-
$handler->handleUnconditionally(true);
367-
368-
foreach (config('app.debug_blacklist', []) as $key => $secrets) {
369-
foreach ($secrets as $secret) {
370-
$handler->blacklist($key, $secret);
371-
}
372-
}
373-
374-
if (config('app.editor', false)) {
375-
$handler->setEditor(config('app.editor'));
376-
}
376+
if (view()->exists($view = "errors::{$e->getStatusCode()}")) {
377+
return response()->view($view, [
378+
'exception' => $e, 'errors' => new ViewErrorBag,
379+
], $e->getStatusCode(), $e->getHeaders());
380+
}
377381

378-
$handler->setApplicationPaths(
379-
array_flip(Arr::except(
380-
array_flip($files->directories(base_path())), [base_path('vendor')]
381-
))
382-
);
383-
});
382+
return $this->convertExceptionToResponse($e);
384383
}
385384

386385
/**
387-
* Render the given HttpException.
386+
* Register the error template hint paths.
388387
*
389-
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e
390-
* @return \Symfony\Component\HttpFoundation\Response
388+
* @return void
391389
*/
392-
protected function renderHttpException(HttpException $e)
390+
protected function registerErrorViewPaths()
393391
{
394-
$status = $e->getStatusCode();
395-
396392
$paths = collect(config('view.paths'));
397393

398394
view()->replaceNamespace('errors', $paths->map(function ($path) {
399395
return "{$path}/errors";
400396
})->push(__DIR__.'/views')->all());
401-
402-
if (view()->exists($view = "errors::{$status}")) {
403-
return response()->view($view, [
404-
'exception' => $e, 'errors' => new ViewErrorBag,
405-
], $status, $e->getHeaders());
406-
}
407-
408-
return $this->convertExceptionToResponse($e);
409397
}
410398

411399
/**
@@ -439,12 +427,10 @@ protected function toIlluminateResponse($response, Exception $e)
439427
*/
440428
protected function prepareJsonResponse($request, Exception $e)
441429
{
442-
$status = $this->isHttpException($e) ? $e->getStatusCode() : 500;
443-
444-
$headers = $this->isHttpException($e) ? $e->getHeaders() : [];
445-
446430
return new JsonResponse(
447-
$this->convertExceptionToArray($e), $status, $headers,
431+
$this->convertExceptionToArray($e),
432+
$this->isHttpException($e) ? $e->getStatusCode() : 500,
433+
$this->isHttpException($e) ? $e->getHeaders() : [],
448434
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
449435
);
450436
}

0 commit comments

Comments
 (0)