80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
use Throwable;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
|
|
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
|
|
use Tymon\JWTAuth\Exceptions\TokenBlacklistedException;
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
|
|
|
class Handler extends ExceptionHandler
|
|
{
|
|
/**
|
|
* A list of the exception types that are not reported.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dontReport = [
|
|
//
|
|
];
|
|
|
|
/**
|
|
* A list of the inputs that are never flashed for validation exceptions.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dontFlash = [
|
|
'current_password',
|
|
'password',
|
|
'password_confirmation',
|
|
];
|
|
|
|
/**
|
|
* Register the exception handling callbacks for the application.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
$this->reportable(function (Throwable $e) {
|
|
//
|
|
});
|
|
}
|
|
|
|
public function render($request, Throwable $e){
|
|
// FirstOrFail 和 FindOrFail 异常处理
|
|
if ($e instanceof ModelNotFoundException) {
|
|
if ($request->ajax() || $request->wantsJson()) {
|
|
return response()->json(['code'=>404 , 'message' => '数据未找到,或已被删除' ], 404);
|
|
}
|
|
}elseif($e instanceof AuthenticationException){
|
|
if ($request->ajax() || $request->wantsJson()) {
|
|
return response()->json(['code'=>401 , 'message' => '未认证' ], 401);
|
|
}
|
|
}elseif($e instanceof TokenBlacklistedException){
|
|
if ($request->ajax() || $request->wantsJson()) {
|
|
return response()->json(['code'=>401 , 'message' => '凭证过期' ], 401);
|
|
}
|
|
}elseif($e instanceof TokenExpiredException){
|
|
if ($request->ajax() || $request->wantsJson()) {
|
|
return response()->json(['code'=>401 , 'message' => '登录过期,请重新登录' ], 401);
|
|
}
|
|
}elseif($e instanceof TokenInvalidException){
|
|
if ($request->ajax() || $request->wantsJson()) {
|
|
return response()->json(['code'=>401 , 'message' => '未认证' ], 401);
|
|
}
|
|
}elseif($e instanceof JWTException){
|
|
if ($request->ajax() || $request->wantsJson()) {
|
|
return response()->json(['code'=>401 , 'message' => '未认证' ], 401);
|
|
}
|
|
}
|
|
|
|
return parent::render($request, $e);
|
|
}
|
|
}
|