12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- namespace App\Exceptions;
- use Closure;
- use Exception;
- use Illuminate\Auth\AuthenticationException;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Session\TokenMismatchException;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Validation\ValidationException;
- use Symfony\Component\HttpFoundation\Response;
- use Throwable;
- class Handler
- {
- public function handle($request, Closure $next)
- {
- $response = $next($request);
- if ($response->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;
-
- if ($e instanceof TokenMismatchException) {
- $status = 419;
- }
- if ($e instanceof AuthenticationException) {
-
- $status = Response::HTTP_UNAUTHORIZED;
- $message = '登录超时!';
- }
-
- if ($e instanceof ValidationException) {
- $status = Response::HTTP_UNPROCESSABLE_ENTITY;
- }
- if ($e instanceof ApiException) {
- $status = 200;
-
-
-
-
-
-
-
- }
- return response()->json(['code' => $code, 'msg' => $message], $status);
- }
- }
|