blessing-skin-server/app/Exceptions/Handler.php

119 lines
3.3 KiB
PHP
Raw Normal View History

2016-08-28 10:05:21 +08:00
<?php
namespace App\Exceptions;
use Exception;
2018-02-16 17:31:04 +08:00
use Illuminate\Http\Response;
2016-09-30 23:21:04 +08:00
use App\Exceptions\PrettyPageException;
2016-08-28 10:05:21 +08:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2018-07-11 16:06:10 +08:00
use Illuminate\Validation\ValidationException;
2016-08-28 10:05:21 +08:00
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
2016-08-28 10:05:21 +08:00
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
HttpException::class,
ModelNotFoundException::class,
2016-09-30 23:21:04 +08:00
ValidationException::class,
PrettyPageException::class,
MethodNotAllowedHttpException::class,
2016-08-28 10:05:21 +08:00
];
/**
* Report or log an exception.
*
2018-02-16 17:31:04 +08:00
* @param Exception $e
2016-08-28 10:05:21 +08:00
* @return void
*/
public function report(Exception $e)
{
parent::report($e);
2016-08-28 10:05:21 +08:00
}
/**
* Render an exception into an HTTP response.
*
2018-02-16 17:31:04 +08:00
* @param \Illuminate\Http\Request $request
* @param Exception $e
* @return Response
2016-08-28 10:05:21 +08:00
*/
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
if ($e instanceof MethodNotAllowedHttpException) {
abort(403, 'Method not allowed.');
}
if ($e instanceof PrettyPageException) {
return $e->showErrorPage();
2016-09-28 17:54:00 +08:00
}
2016-09-10 21:14:46 +08:00
if ($e instanceof ValidationException) {
2018-02-16 17:31:04 +08:00
// Quick fix for returning 422
// @see https://prinzeugen.net/custom-responses-of-laravel-validations/
2016-09-10 21:14:46 +08:00
return $e->getResponse()->setStatusCode(200);
}
foreach ($this->dontReport as $type) {
if ($e instanceof $type) {
return parent::render($request, $e);
} else {
2018-02-16 17:31:04 +08:00
// Hide exception details if we are not in debug mode
if (config('app.debug') && !$request->ajax()) {
return $this->renderExceptionWithWhoops($e);
} else {
return $this->renderExceptionInBrief($e);
}
}
2016-08-29 12:19:21 +08:00
}
2016-08-28 10:05:21 +08:00
}
/**
* Render an exception using Whoops.
*
2018-02-16 17:31:04 +08:00
* @param Exception $e
* @param int $code
* @param array $headers
* @return Response
2016-08-28 10:05:21 +08:00
*/
protected function renderExceptionWithWhoops(Exception $e, $code = 200, $headers = [])
2016-08-28 10:05:21 +08:00
{
$whoops = new \Whoops\Run;
$handler = (request()->isMethod('GET')) ?
2016-08-28 10:05:21 +08:00
new \Whoops\Handler\PrettyPageHandler : new \Whoops\Handler\PlainTextHandler;
$whoops->pushHandler($handler);
2018-02-16 17:31:04 +08:00
return new Response(
2016-08-28 10:05:21 +08:00
$whoops->handleException($e),
$code,
$headers
2016-08-28 10:05:21 +08:00
);
}
/**
* Render an exception in a short word.
*
2018-02-16 17:31:04 +08:00
* @param Exception $e
* @return Response
*/
protected function renderExceptionInBrief(Exception $e)
{
if (request()->isMethod('GET') && !request()->ajax()) {
return response()->view('errors.exception', ['message' => $e->getMessage()]);
} else {
2018-07-11 16:06:10 +08:00
return response($e->getMessage());
}
}
2016-08-28 10:05:21 +08:00
}