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

136 lines
4.0 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;
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;
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,
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) {
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'));
}
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/
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
}
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
}