123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace App\Exceptions;
- use Exception;
- use Illuminate\Auth\AuthenticationException;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Illuminate\Session\TokenMismatchException;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Validation\ValidationException;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Throwable;
- class Handler extends ExceptionHandler
- {
- /**
- * The list of the inputs that are never flashed to the session on validation exceptions.
- *
- * @var array<int, string>
- */
- 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);
- }
- }
|