Handler.php 5.3 KB

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