Handler.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Utils\IP;
  4. use ErrorException;
  5. use Illuminate\Auth\AuthenticationException;
  6. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  7. use Illuminate\Http\Client\ConnectionException;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Session\TokenMismatchException;
  10. use Illuminate\Validation\ValidationException;
  11. use Log;
  12. use ReflectionException;
  13. use Response;
  14. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Throwable;
  17. class Handler extends ExceptionHandler
  18. {
  19. protected $dontReport = [
  20. // HttpException::class,
  21. ValidationException::class,
  22. ];
  23. /**
  24. * The list of the inputs that are never flashed to the session on validation exceptions.
  25. *
  26. * @var array<int, string>
  27. */
  28. protected $dontFlash = [
  29. 'current_password',
  30. 'password',
  31. 'password_confirmation',
  32. ];
  33. /**
  34. * Report or log an exception.
  35. *
  36. * @return void
  37. *
  38. * @throws Throwable
  39. */
  40. public function report(Throwable $exception)
  41. {
  42. if (config('app.debug')) { // 调试模式下记录错误详情
  43. Log::debug('来源:'.url()->full().PHP_EOL.'访问者IP:'.IP::getClientIP().PHP_EOL.$exception);
  44. } else {
  45. Log::error('来源:'.url()->full().PHP_EOL.'访问者IP:'.IP::getClientIP().get_class($exception)); // 记录异常来源
  46. }
  47. parent::report($exception);
  48. }
  49. /**
  50. * Render an exception into an HTTP response.
  51. *
  52. * @param Request $request
  53. * @return \Symfony\Component\HttpFoundation\Response
  54. *
  55. * @throws Throwable
  56. */
  57. public function render($request, Throwable $exception)
  58. {
  59. // 调试模式下直接返回错误信息,非调试模式下渲染在返回
  60. if (! config('app.debug')) {
  61. switch ($exception) {
  62. case $exception instanceof NotFoundHttpException: // 捕获访问异常
  63. Log::warning('异常请求:'.$request->fullUrl().',IP:'.IP::getClientIp());
  64. if ($request->ajax() || $request->wantsJson()) {
  65. return Response::json(['status' => 'fail', 'message' => trans('http-statuses.404')], 404);
  66. }
  67. return Response::view('auth.error', ['message' => trans('http-statuses.404')], 404);
  68. case $exception instanceof AuthenticationException: // 捕获身份校验异常
  69. if ($request->ajax() || $request->wantsJson()) {
  70. return Response::json(['status' => 'fail', 'message' => trans('http-statuses.401')], 401);
  71. }
  72. return Response::view('auth.error', ['message' => trans('http-statuses.401')], 401);
  73. case $exception instanceof TokenMismatchException: // 捕获CSRF异常
  74. if ($request->ajax() || $request->wantsJson()) {
  75. return Response::json(['status' => 'fail', 'message' => trans('http-statuses.419')], 419);
  76. }
  77. return Response::view('auth.error', ['message' => trans('errors.refresh_page').'<a href="'.route('login').'" target="_blank">'.trans('errors.refresh').'</a>'],
  78. 419);
  79. case $exception instanceof ReflectionException:
  80. if ($request->ajax() || $request->wantsJson()) {
  81. return Response::json(['status' => 'fail', 'message' => trans('http-statuses.500')], 500);
  82. }
  83. return Response::view('auth.error', ['message' => trans('http-statuses.500')], 500);
  84. case $exception instanceof MethodNotAllowedHttpException:
  85. if ($request->ajax() || $request->wantsJson()) {
  86. return Response::json(['status' => 'fail', 'message' => trans('http-statuses.405')], 405);
  87. }
  88. return Response::view('auth.error', ['message' => trans('http-statuses.405')], 405);
  89. case $exception instanceof ErrorException: // 捕获系统错误异常
  90. if ($request->ajax() || $request->wantsJson()) {
  91. return Response::json([
  92. 'status' => 'fail',
  93. 'message' => trans('http-statuses.500').', '.trans('errors.visit').'<a href="'.route('log-viewer::dashboard').'" target="_blank">'.trans('errors.log').'</a>',
  94. ], 500);
  95. }
  96. return Response::view('auth.error',
  97. ['message' => trans('http-statuses.500').', '.trans('errors.visit').'<a href="'.route('log-viewer::dashboard').'" target="_blank">'.trans('errors.log').'</a>'],
  98. 500);
  99. case $exception instanceof ConnectionException:
  100. if ($request->ajax() || $request->wantsJson()) {
  101. return Response::json(['status' => 'fail', 'message' => $exception->getMessage()], 408);
  102. }
  103. return Response::view('auth.error', ['message' => $exception->getMessage()], 408);
  104. }
  105. }
  106. return parent::render($request, $exception);
  107. }
  108. }