Handler.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. ConnectionException::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. }
  45. parent::report($exception);
  46. }
  47. /**
  48. * Render an exception into an HTTP response.
  49. *
  50. * @param Request $request
  51. * @return \Symfony\Component\HttpFoundation\Response
  52. *
  53. * @throws Throwable
  54. */
  55. public function render($request, Throwable $exception)
  56. {
  57. // 调试模式下直接返回错误信息,非调试模式下渲染在返回
  58. if (! config('app.debug')) {
  59. switch ($exception) {
  60. case $exception instanceof NotFoundHttpException: // 捕获访问异常
  61. Log::warning('异常请求:'.$request->fullUrl().',IP:'.IP::getClientIp());
  62. if ($request->ajax() || $request->wantsJson()) {
  63. return Response::json(['status' => 'fail', 'message' => trans('http-statuses.404')], 404);
  64. }
  65. return Response::view('auth.error', ['message' => trans('http-statuses.404')], 404);
  66. case $exception instanceof AuthenticationException: // 捕获身份校验异常
  67. if ($request->ajax() || $request->wantsJson()) {
  68. return Response::json(['status' => 'fail', 'message' => trans('http-statuses.401')], 401);
  69. }
  70. return Response::view('auth.error', ['message' => trans('http-statuses.401')], 401);
  71. case $exception instanceof TokenMismatchException: // 捕获CSRF异常
  72. if ($request->ajax() || $request->wantsJson()) {
  73. return Response::json(['status' => 'fail', 'message' => trans('http-statuses.419')], 419);
  74. }
  75. return Response::view('auth.error', ['message' => trans('errors.refresh_page').'<a href="'.route('login').'" target="_blank">'.trans('errors.refresh').'</a>'],
  76. 419);
  77. case $exception instanceof ReflectionException:
  78. if ($request->ajax() || $request->wantsJson()) {
  79. return Response::json(['status' => 'fail', 'message' => trans('http-statuses.500')], 500);
  80. }
  81. return Response::view('auth.error', ['message' => trans('http-statuses.500')], 500);
  82. case $exception instanceof MethodNotAllowedHttpException:
  83. if ($request->ajax() || $request->wantsJson()) {
  84. return Response::json(['status' => 'fail', 'message' => trans('http-statuses.405')], 405);
  85. }
  86. return Response::view('auth.error', ['message' => trans('http-statuses.405')], 405);
  87. case $exception instanceof ErrorException: // 捕获系统错误异常
  88. if ($request->ajax() || $request->wantsJson()) {
  89. return Response::json([
  90. 'status' => 'fail',
  91. 'message' => trans('http-statuses.500').', '.trans('errors.visit').'<a href="'.route('log-viewer::dashboard').'" target="_blank">'.trans('errors.log').'</a>',
  92. ], 500);
  93. }
  94. return Response::view('auth.error',
  95. ['message' => trans('http-statuses.500').', '.trans('errors.visit').'<a href="'.route('log-viewer::dashboard').'" target="_blank">'.trans('errors.log').'</a>'],
  96. 500);
  97. case $exception instanceof ConnectionException:
  98. if ($request->ajax() || $request->wantsJson()) {
  99. return Response::json(['status' => 'fail', 'message' => $exception->getMessage()], 408);
  100. }
  101. return Response::view('auth.error', ['message' => $exception->getMessage()], 408);
  102. }
  103. }
  104. return parent::render($request, $exception);
  105. }
  106. }