Laravel应用中所有的异常都通过 App\Exceptions\Handler
进行处理,下面我们先简单分析下给异常处理器类的属性和方法:
$dontReport属性
protected $dontReport = [ HttpException::class, ModelNotFoundException::class, TokenMismatchException::class, //引入totken类 ];
render方法
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
//TODO 这里一条自定义http错误自动跳转到首页
if (getenv('APP_ENV') == 'production' && $e instanceof HttpException) {
Log::error($e);
return Redirect::to('admin/dashboard');
}
if (getenv('APP_ENV') == 'production' && $e instanceof TokenMismatchException) {
Log::error($e);
if ($request->ajax()) {
return Response::json(
[
'status' => 'failed',
'error' =>
[
'status_code' => 401,
'message' => '操作未完成,系统加载失败,重新登录或者刷新当前页面!'
]
]
);
}
return Redirect::to('admin/logout');
}
return parent::render($request, $e);
}