exception) return $this->render($request, $response->exception); return $response; } public function render($request, Throwable $e): JsonResponse { $code = $e->getCode(); $message = $e->getMessage(); $status = Response::HTTP_INTERNAL_SERVER_ERROR; // CSRF 跨站请求伪造 if ($e instanceof TokenMismatchException) { $status = 419; } if ($e instanceof AuthenticationException) { //Unauthenticated. $status = Response::HTTP_UNAUTHORIZED; $message = '登录超时!'; } // 表单验证 if ($e instanceof ValidationException) { $status = Response::HTTP_UNPROCESSABLE_ENTITY; } if ($e instanceof ApiException) { // // 400 错误请求 // // 401 未授权 // // 403 禁止访问 // // 409 数据冲突 // 419 CSRF跨域伪造 // // 422 验证规则 // // 500 服务器内部错误 $status = $code; } return response()->json(['code' => $status, 'message' => $message], $status); } }