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;
|
2018-08-20 23:09:26 +08:00
|
|
|
use Illuminate\Session\TokenMismatchException;
|
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;
|
2016-11-18 16:46:58 +08:00
|
|
|
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,
|
2018-08-20 23:09:26 +08:00
|
|
|
TokenMismatchException::class,
|
2016-09-30 23:21:04 +08:00
|
|
|
ValidationException::class,
|
2016-11-18 16:46:58 +08:00
|
|
|
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)
|
|
|
|
{
|
2017-10-29 09:19:02 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-11-18 16:46:58 +08:00
|
|
|
if ($e instanceof MethodNotAllowedHttpException) {
|
2018-08-20 23:09:26 +08:00
|
|
|
abort(403, trans('errors.http.method-not-allowed'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($e instanceof TokenMismatchException) {
|
|
|
|
if ($request->expectsJson()) {
|
|
|
|
return json(trans('errors.http.csrf-token-mismatch'), 1);
|
|
|
|
}
|
|
|
|
abort(403, trans('errors.http.csrf-token-mismatch'));
|
2016-08-29 19:47:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-29 09:19:02 +08:00
|
|
|
if ($e instanceof PrettyPageException) {
|
2016-11-18 16:46:58 +08:00
|
|
|
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
|
2016-11-18 16:46:58 +08:00
|
|
|
// @see https://prinzeugen.net/custom-responses-of-laravel-validations/
|
2018-08-12 08:48:42 +08:00
|
|
|
if ($request->expectsJson()) {
|
2018-07-21 17:50:48 +08:00
|
|
|
return response()->json([
|
|
|
|
'errno' => 1,
|
|
|
|
'msg' => $e->validator->errors()->first()
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$request->session()->flash('errors', $e->validator->errors());
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
2016-09-10 21:14:46 +08:00
|
|
|
}
|
|
|
|
|
2016-12-23 21:44:52 +08:00
|
|
|
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
|
2017-06-29 21:34:02 +08:00
|
|
|
if (config('app.debug') && !$request->ajax()) {
|
2016-12-23 21:44:52 +08:00
|
|
|
return $this->renderExceptionWithWhoops($e);
|
|
|
|
} else {
|
|
|
|
return $this->renderExceptionInBrief($e);
|
2016-08-29 15:10:51 +08:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
2017-10-29 09:19:02 +08:00
|
|
|
protected function renderExceptionWithWhoops(Exception $e, $code = 200, $headers = [])
|
2016-08-28 10:05:21 +08:00
|
|
|
{
|
|
|
|
$whoops = new \Whoops\Run;
|
2017-10-29 09:19:02 +08:00
|
|
|
$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),
|
2017-10-29 09:19:02 +08:00
|
|
|
$code,
|
|
|
|
$headers
|
2016-08-28 10:05:21 +08:00
|
|
|
);
|
|
|
|
}
|
2016-12-23 21:44:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render an exception in a short word.
|
|
|
|
*
|
2018-02-16 17:31:04 +08:00
|
|
|
* @param Exception $e
|
|
|
|
* @return Response
|
2016-12-23 21:44:52 +08:00
|
|
|
*/
|
|
|
|
protected function renderExceptionInBrief(Exception $e)
|
|
|
|
{
|
2017-10-29 09:19:02 +08:00
|
|
|
if (request()->isMethod('GET') && !request()->ajax()) {
|
2017-01-22 16:45:19 +08:00
|
|
|
return response()->view('errors.exception', ['message' => $e->getMessage()]);
|
|
|
|
} else {
|
2018-07-11 16:06:10 +08:00
|
|
|
return response($e->getMessage());
|
2017-01-22 16:45:19 +08:00
|
|
|
}
|
2016-12-23 21:44:52 +08:00
|
|
|
}
|
2016-08-28 10:05:21 +08:00
|
|
|
}
|