Handler.php 3.1 KB

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