Handler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Exceptions;
  3. use Exception;
  4. use Illuminate\Auth\AuthenticationException;
  5. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  6. use Illuminate\Session\TokenMismatchException;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Validation\ValidationException;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Throwable;
  12. class Handler extends ExceptionHandler
  13. {
  14. /**
  15. * The list of the inputs that are never flashed to the session on validation exceptions.
  16. *
  17. * @var array<int, string>
  18. */
  19. protected $dontFlash = [
  20. 'current_password',
  21. 'password',
  22. 'password_confirmation',
  23. ];
  24. /**
  25. * Register the exception handling callbacks for the application.
  26. */
  27. public function register(): void
  28. {
  29. $this->reportable(function (Throwable $e) {
  30. $context = [
  31. 'exception' => get_class($e),
  32. 'file' => $e->getFile(),
  33. 'line' => $e->getLine(),
  34. 'trace' => $e->getTraceAsString(),
  35. 'url' => request()->fullUrl(),
  36. 'method' => request()->method(),
  37. 'ip' => request()->ip(),
  38. 'user_agent' => request()->userAgent(),
  39. ];
  40. if ($e instanceof NotFoundHttpException) {
  41. Log::error('Route not found: '.request()->fullUrl(), $context);
  42. } else {
  43. Log::error($e->getMessage(), $context);
  44. }
  45. });
  46. $this->renderable(function (NotFoundHttpException $e) {
  47. return response()->json([
  48. 'code' => 404,
  49. 'msg' => 'Route not found: '.request()->fullUrl(),
  50. ], Response::HTTP_NOT_FOUND);
  51. });
  52. $this->renderable(function (TokenMismatchException $e) {
  53. return response()->json([
  54. 'code' => $e->getCode() ?: 419,
  55. 'msg' => $e->getMessage(),
  56. ], 419);
  57. });
  58. $this->renderable(function (AuthenticationException $e) {
  59. return response()->json([
  60. 'code' => $e->getCode() ?: 401,
  61. 'msg' => '登录超时!',
  62. ], Response::HTTP_UNAUTHORIZED);
  63. });
  64. $this->renderable(function (ValidationException $e) {
  65. return response()->json([
  66. 'code' => $e->getCode() ?: 422,
  67. 'msg' => $e->getMessage(),
  68. ], Response::HTTP_UNPROCESSABLE_ENTITY);
  69. });
  70. $this->renderable(function (ApiException $e) {
  71. return response()->json([
  72. 'code' => $e->getCode() ?: 200,
  73. 'msg' => $e->getMessage(),
  74. ], 200);
  75. });
  76. $this->renderable(function (Throwable $e) {
  77. return response()->json([
  78. 'code' => $e->getCode() ?: 500,
  79. 'msg' => $e->getMessage(),
  80. ], Response::HTTP_INTERNAL_SERVER_ERROR);
  81. });
  82. }
  83. public function render($request, Throwable $exception)
  84. {
  85. if ($request->expectsJson()) {
  86. if ($exception instanceof HttpException) {
  87. return response()->json([
  88. 'code' => $exception->getStatusCode(),
  89. 'message' => $exception->getMessage(),
  90. 'data' => null,
  91. ], $exception->getStatusCode());
  92. }
  93. }
  94. return parent::render($request, $exception);
  95. }
  96. }