Skip to content

Instantly share code, notes, and snippets.

@DCzajkowski
Last active August 19, 2018 11:44
Show Gist options
  • Save DCzajkowski/195c9c34fc0868d7548b4e8f017dd3b5 to your computer and use it in GitHub Desktop.
Save DCzajkowski/195c9c34fc0868d7548b4e8f017dd3b5 to your computer and use it in GitHub Desktop.
app/Exceptions/Handler.php
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Support\ViewErrorBag;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
// ...
/**
* Prepare a response for the given exception.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function prepareResponse($request, Exception $e)
{
if (view()->exists($view = "errors.{$e->getStatusCode()}")) {
return response()->view($view, [
'errors' => new ViewErrorBag,
'exception' => $e,
], $e->getStatusCode(), $e->getHeaders());
}
if (view()->exists($view = 'errors.default')) { // you can also add `&& config('app.env') !== 'local'` for this behavior to only happen in production
return response()->view($view, [
'errors' => new ViewErrorBag,
'exception' => $e,
], $e->getStatusCode(), $e->getHeaders());
}
return parent::prepareResponse($request, Exception $e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment