正在显示
1 个修改的文件
包含
110 行增加
和
10 行删除
| @@ -2,10 +2,19 @@ | @@ -2,10 +2,19 @@ | ||
| 2 | 2 | ||
| 3 | namespace App\Exceptions; | 3 | namespace App\Exceptions; |
| 4 | 4 | ||
| 5 | +use App\Enums\Common\Code; | ||
| 6 | +use App\Enums\Common\Common; | ||
| 7 | +use App\Services\DingService; | ||
| 8 | +use App\Traits\RedisTrait; | ||
| 9 | +use App\Utils\EncryptUtils; | ||
| 10 | +use App\Utils\LogUtils; | ||
| 11 | +use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
| 5 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | 12 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
| 6 | - | ||
| 7 | use Illuminate\Support\Arr; | 13 | use Illuminate\Support\Arr; |
| 8 | - | 14 | +use Illuminate\Support\Facades\Route; |
| 15 | +use Illuminate\Validation\ValidationException; | ||
| 16 | +use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; | ||
| 17 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
| 9 | use Throwable; | 18 | use Throwable; |
| 10 | 19 | ||
| 11 | class Handler extends ExceptionHandler | 20 | class Handler extends ExceptionHandler |
| @@ -13,7 +22,7 @@ class Handler extends ExceptionHandler | @@ -13,7 +22,7 @@ class Handler extends ExceptionHandler | ||
| 13 | /** | 22 | /** |
| 14 | * A list of the exception types that are not reported. | 23 | * A list of the exception types that are not reported. |
| 15 | * | 24 | * |
| 16 | - * @var array | 25 | + * @var array<int, class-string<Throwable>> |
| 17 | */ | 26 | */ |
| 18 | protected $dontReport = [ | 27 | protected $dontReport = [ |
| 19 | // | 28 | // |
| @@ -22,37 +31,128 @@ class Handler extends ExceptionHandler | @@ -22,37 +31,128 @@ class Handler extends ExceptionHandler | ||
| 22 | /** | 31 | /** |
| 23 | * A list of the inputs that are never flashed for validation exceptions. | 32 | * A list of the inputs that are never flashed for validation exceptions. |
| 24 | * | 33 | * |
| 25 | - * @var array | 34 | + * @var array<int, string> |
| 26 | */ | 35 | */ |
| 27 | protected $dontFlash = [ | 36 | protected $dontFlash = [ |
| 37 | + 'current_password', | ||
| 28 | 'password', | 38 | 'password', |
| 29 | 'password_confirmation', | 39 | 'password_confirmation', |
| 30 | ]; | 40 | ]; |
| 31 | 41 | ||
| 32 | /** | 42 | /** |
| 43 | + * Register the exception handling callbacks for the application. | ||
| 44 | + * | ||
| 45 | + * @return void | ||
| 46 | + */ | ||
| 47 | + public function register() | ||
| 48 | + { | ||
| 49 | + $this->reportable(function (Throwable $e) { | ||
| 50 | + // | ||
| 51 | + }); | ||
| 52 | + } | ||
| 53 | + /** | ||
| 33 | * Report or log an exception. | 54 | * Report or log an exception. |
| 34 | * | 55 | * |
| 35 | - * @param \Throwable $exception | 56 | + * @param \Throwable $exception |
| 36 | * @return void | 57 | * @return void |
| 37 | * | 58 | * |
| 38 | * @throws \Throwable | 59 | * @throws \Throwable |
| 39 | */ | 60 | */ |
| 40 | public function report(Throwable $exception) | 61 | public function report(Throwable $exception) |
| 41 | { | 62 | { |
| 42 | - parent::report($exception); | ||
| 43 | - } | ||
| 44 | 63 | ||
| 64 | + //日志记录 | ||
| 65 | + $exceptionMessage = "错误CODE:" . $exception->getCode() . | ||
| 66 | + "-----错误message:" . $exception->getMessage() . | ||
| 67 | + '------错误文件:' . $exception->getFile() . | ||
| 68 | + '-------错误行数:' . $exception->getLine(); | ||
| 69 | + //A端错误 | ||
| 70 | + if ($exception instanceof AsideGlobalException) { | ||
| 71 | + LogUtils::error("AsideGlobalException", [], $exceptionMessage); | ||
| 72 | + } | ||
| 73 | + //B端错误 | ||
| 74 | + elseif($exception instanceof BsideGlobalException) { | ||
| 75 | + LogUtils::error("BsideGlobalException", [], $exceptionMessage); | ||
| 76 | + } | ||
| 77 | + //验证错误(非手动抛出) | ||
| 78 | + elseif ($exception instanceof ValidationException) { | ||
| 79 | + LogUtils::error("参数验证失败", [], $exceptionMessage); | ||
| 80 | + } | ||
| 81 | + //Redis错误(非手动抛出) | ||
| 82 | + elseif ($exception instanceof \RedisException) { | ||
| 83 | + LogUtils::error("Redis服务异常", [], $exceptionMessage); | ||
| 84 | + } | ||
| 85 | + //Mysql错误(非手动抛出) | ||
| 86 | + elseif ($exception instanceof \PDOException) { | ||
| 87 | + LogUtils::error("数据库服务异常", [], $exceptionMessage); | ||
| 88 | + } | ||
| 89 | + //模型未找到错误(非手动抛出) | ||
| 90 | + elseif ($exception instanceof ModelNotFoundException) { | ||
| 91 | + LogUtils::error("模型资源未找到", [], $exceptionMessage); | ||
| 92 | + } | ||
| 93 | + //其他 | ||
| 94 | + else { | ||
| 95 | + LogUtils::error("全局系统异常", [], $exceptionMessage); | ||
| 96 | + } | ||
| 97 | + } | ||
| 45 | /** | 98 | /** |
| 46 | * Render an exception into an HTTP response. | 99 | * Render an exception into an HTTP response. |
| 47 | * | 100 | * |
| 48 | - * @param \Illuminate\Http\Request $request | ||
| 49 | - * @param \Throwable $exception | 101 | + * @param \Illuminate\Http\Request $request |
| 102 | + * @param \Throwable $exception | ||
| 50 | * @return \Symfony\Component\HttpFoundation\Response | 103 | * @return \Symfony\Component\HttpFoundation\Response |
| 51 | * | 104 | * |
| 52 | * @throws \Throwable | 105 | * @throws \Throwable |
| 53 | */ | 106 | */ |
| 54 | public function render($request, Throwable $exception) | 107 | public function render($request, Throwable $exception) |
| 55 | { | 108 | { |
| 56 | - return parent::render($request, $exception); | 109 | + $message = $exception->getMessage(); |
| 110 | + | ||
| 111 | + if ($exception instanceof AsideGlobalException) { | ||
| 112 | + $code = $exception->getCode(); | ||
| 113 | + }elseif ($exception instanceof BsideGlobalException) { | ||
| 114 | + $code = $exception->getCode(); | ||
| 115 | + } elseif ($exception instanceof ValidationException) { | ||
| 116 | + $code = Code::USER_PARAMS_ERROE(); | ||
| 117 | + } elseif ($exception instanceof \RedisException) { | ||
| 118 | + $code = Code::SERVER_REDIS_ERROR(); | ||
| 119 | + } elseif ($exception instanceof \PDOException) { | ||
| 120 | + $code = Code::SERVER_MYSQL_ERROR(); | ||
| 121 | + } elseif ($exception instanceof ModelNotFoundException) { | ||
| 122 | + $code = Code::USER_MODEL_NOTFOUND_ERROE(); | ||
| 123 | + } elseif ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) { | ||
| 124 | + return response('404 Not Found', 404); | ||
| 125 | + } else { | ||
| 126 | + $code = Code::SYSTEM_ERROR(); | ||
| 127 | + } | ||
| 128 | + //钉钉通知错误信息 | ||
| 129 | + if (in_array(config('app.env'), ['test', 'production']) && (in_array(substr($code, 0, 1), ['B', 'C']))) { | ||
| 130 | + $exceptionMessage = "路由:" . Route::current()->uri . | ||
| 131 | + "-----错误CODE:" . $exception->getCode() . | ||
| 132 | + "-----错误message:" . $exception->getMessage() . | ||
| 133 | + '------错误文件:' . $exception->getFile() . '-------错误行数:' . | ||
| 134 | + $exception->getLine(); | ||
| 135 | + (new DingService())->handle(['keyword' => "ERROR", 'msg' => config('app.env') . '环境报错:' . $exceptionMessage, 'isAtAll' => false]); | ||
| 136 | + } | ||
| 137 | + //开启debug 错误原样输出 | ||
| 138 | + $debub = config('app.debug'); | ||
| 139 | + $message = $debub ? $message : ($code->description ?? $message); | ||
| 140 | + | ||
| 141 | + //请求参数错误 输出具体错误信息 | ||
| 142 | + if($code == Code::USER_PARAMS_ERROE()){ | ||
| 143 | + $message = Arr::first(Arr::first($exception->errors())); | ||
| 144 | + } | ||
| 145 | + $response = [ | ||
| 146 | + 'code' => $code, | ||
| 147 | + 'message' => $message | ||
| 148 | + ]; | ||
| 149 | + //加密返回 | ||
| 150 | + if (config('app.params_encrypt')) { | ||
| 151 | + $k = config('app.params_encrypt_key'); | ||
| 152 | + $i = config('app.params_encrypt_iv'); | ||
| 153 | + $response = [ | ||
| 154 | + 'p' => (new EncryptUtils())->openssl_en($response, $k, $i)]; | ||
| 155 | + } | ||
| 156 | + return response($response); | ||
| 57 | } | 157 | } |
| 58 | } | 158 | } |
-
请 注册 或 登录 后发表评论