Handler.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\HttpException;
  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. 'password',
  35. 'password_confirmation',
  36. ];
  37. /**
  38. * Report or log an exception.
  39. *
  40. * @param Throwable $exception
  41. * @return void
  42. *
  43. * @throws Throwable
  44. */
  45. public function report(Throwable $exception)
  46. {
  47. if (config('app.debug') || config('app.demo')) { // 调试模式下记录错误详情
  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. * @param Throwable $exception
  59. * @return \Symfony\Component\HttpFoundation\Response
  60. *
  61. * @throws Throwable
  62. */
  63. public function render($request, Throwable $exception)
  64. {
  65. // 调试模式下直接返回错误信息,非调试模式下渲染在返回
  66. if (! config('app.debug')) {
  67. switch ($exception) {
  68. case $exception instanceof NotFoundHttpException: // 捕获访问异常
  69. Log::warning('异常请求:'.$request->fullUrl().',IP:'.IP::getClientIp());
  70. if ($request->ajax() || $request->wantsJson()) {
  71. return Response::json(['status' => 'fail', 'message' => trans('errors.missing_page')], 404);
  72. }
  73. return Response::view('auth.error', ['message' => trans('errors.missing_page')], 404);
  74. case $exception instanceof AuthenticationException: // 捕获身份校验异常
  75. if ($request->ajax() || $request->wantsJson()) {
  76. return Response::json(['status' => 'fail', 'message' => trans('errors.unauthorized')], 401);
  77. }
  78. return Response::view('auth.error', ['message' => trans('errors.unauthorized')], 401);
  79. case $exception instanceof TokenMismatchException: // 捕获CSRF异常
  80. if ($request->ajax() || $request->wantsJson()) {
  81. return Response::json([
  82. 'status' => 'fail',
  83. 'message' => trans('errors.refresh_page').'<a href="'.route('login').'" target="_blank">'.trans('errors.refresh').'</a>',
  84. ], 419);
  85. }
  86. return Response::view(
  87. 'auth.error',
  88. ['message' => trans('errors.refresh_page').'<a href="'.route('login').'" target="_blank">'.trans('errors.refresh').'</a>'],
  89. 419
  90. );
  91. case $exception instanceof ReflectionException:
  92. if ($request->ajax() || $request->wantsJson()) {
  93. return Response::json(['status' => 'fail', 'message' => trans('errors.system')], 500);
  94. }
  95. return Response::view('auth.error', ['message' => trans('errors.system')], 500);
  96. case $exception instanceof ErrorException: // 捕获系统错误异常
  97. if ($request->ajax() || $request->wantsJson()) {
  98. return Response::json([
  99. 'status' => 'fail',
  100. 'message' => trans('errors.system').', '.trans('errors.visit').'<a href="'.route('log-viewer::dashboard').'" target="_blank">'.trans('errors.log').'</a>',
  101. ], 500);
  102. }
  103. return Response::view(
  104. 'auth.error',
  105. ['message' => trans('errors.system').', '.trans('errors.visit').'<a href="'.route('log-viewer::dashboard').'" target="_blank">'.trans('errors.log').'</a>'],
  106. 500
  107. );
  108. case $exception instanceof ConnectionException:
  109. if ($request->ajax() || $request->wantsJson()) {
  110. return Response::json(['status' => 'fail', 'message' => $exception->getMessage()], 408);
  111. }
  112. return Response::view('auth.error', ['message' => $exception->getMessage()], 408);
  113. }
  114. }
  115. return parent::render($request, $exception);
  116. }
  117. }