Commit 93eb832f by 杨昕

优化异常处理模块

parent 785b41c2
Pipeline #14301 passed with stages
in 4 minutes 5 seconds
...@@ -6,6 +6,8 @@ use Exception; ...@@ -6,6 +6,8 @@ use Exception;
use App\Tool\ToolFunc; use App\Tool\ToolFunc;
use Illuminate\Auth\AuthenticationException; use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
class Handler extends ExceptionHandler class Handler extends ExceptionHandler
{ {
...@@ -74,38 +76,45 @@ class Handler extends ExceptionHandler ...@@ -74,38 +76,45 @@ class Handler extends ExceptionHandler
} }
} }
/**
* Api异常处理模块
*/
if (self::handlerException($exception)) {
# 获取常量类里面的异常常量
$message = json_decode($exception->getMessage(),true);
return error(...$message);
} else if ($exception instanceof AuthenticationException) {
return error($exception->getMessage(),\Symfony\Component\HttpFoundation\Response::HTTP_UNAUTHORIZED);
} else if ($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException) {
return error('请求方法不被允许',\Symfony\Component\HttpFoundation\Response::HTTP_METHOD_NOT_ALLOWED);
} else if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) {
if ($exception->getMessage() == 'Too Many Attempts.') {
return error('请求接口过于频繁',429);
}
}else if($exception instanceof \RedisException){
return error('redis连接异常');
}
if ($request->is('api/*') ) { if ($request->is('api/*') ) {
/**
* Api异常处理模块
*/
if (self::handlerException($exception)) {
# 获取常量类里面的异常常量
$message = json_decode($exception->getMessage(),true);
return error(...$message);
} else if ($exception instanceof AuthenticationException) {
return error($exception->getMessage(),\Symfony\Component\HttpFoundation\Response::HTTP_UNAUTHORIZED);
} else if ($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException) {
return error('请求方法不被允许',\Symfony\Component\HttpFoundation\Response::HTTP_METHOD_NOT_ALLOWED);
} else if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) {
if ($exception->getMessage() == 'Too Many Attempts.') {
return error('请求接口过于频繁',429);
}
}else if($exception instanceof \RedisException){
return error('redis连接异常');
}
return error('服务器请求异常:'.$exception->getMessage()."异常信息详情:".$exception->getTraceAsString(),500); return error('服务器请求异常:'.$exception->getMessage()."异常信息详情:".$exception->getTraceAsString(),500);
} else { } else {
if ($exception->getMessage() != '') { if ($exception->getMessage() != '') {
//print_r($exception->getMessage()); //print_r($exception->getMessage());
if ($exception->getMessage() == 403) { if ($exception->getMessage() == 403) {
return response()->view('errors.403'); return response()->view('errors.403');
} }
//return response()->view('errors.404');
}else if($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException){
return response()->view('errors.403');
}else{
return response()->view('errors.404'); return response()->view('errors.404');
} }
#return response()->json(['code'=>403, 'msg'=>'您没有权限']); #return response()->json(['code'=>403, 'msg'=>'您没有权限']);
} }
return parent::render($request, $exception); return parent::render($request, $exception);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment