*/ protected $dontFlash = [ 'current_password', 'password', 'password_confirmation', ]; /** * Register the exception handling callbacks for the application. */ public function register(): void { $this->reportable(function (Throwable $e) { $context = [ 'exception' => get_class($e), 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => $e->getTraceAsString(), 'url' => request()->fullUrl(), 'method' => request()->method(), 'ip' => request()->ip(), 'user_agent' => request()->userAgent(), ]; if ($e instanceof NotFoundHttpException) { Log::error('Route not found: '.request()->fullUrl(), $context); } else { Log::error($e->getMessage(), $context); } }); $this->renderable(function (NotFoundHttpException $e) { return response()->json([ 'code' => 404, 'msg' => 'Route not found: '.request()->fullUrl(), ], Response::HTTP_NOT_FOUND); }); $this->renderable(function (TokenMismatchException $e) { return response()->json([ 'code' => $e->getCode() ?: 419, 'msg' => $e->getMessage(), ], 419); }); $this->renderable(function (AuthenticationException $e) { return response()->json([ 'code' => $e->getCode() ?: 401, 'msg' => '登录超时!', ], Response::HTTP_UNAUTHORIZED); }); $this->renderable(function (ValidationException $e) { return response()->json([ 'code' => $e->getCode() ?: 422, 'msg' => $e->getMessage(), ], Response::HTTP_UNPROCESSABLE_ENTITY); }); $this->renderable(function (ApiException $e) { return response()->json([ 'code' => $e->getCode() ?: 200, 'msg' => $e->getMessage(), ], 200); }); $this->renderable(function (Throwable $e) { return response()->json([ 'code' => $e->getCode() ?: 500, 'msg' => $e->getMessage(), ], Response::HTTP_INTERNAL_SERVER_ERROR); }); } public function render($request, Throwable $exception) { if ($request->expectsJson()) { if ($exception instanceof HttpException) { return response()->json([ 'code' => $exception->getStatusCode(), 'message' => $exception->getMessage(), 'data' => null, ], $exception->getStatusCode()); } } return parent::render($request, $exception); } }