Handler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Components\IP;
  4. use ErrorException;
  5. use Exception;
  6. use Illuminate\Auth\AuthenticationException;
  7. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Session\TokenMismatchException;
  10. use Log;
  11. use ReflectionException;
  12. use Response;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Throwable;
  15. class Handler extends ExceptionHandler
  16. {
  17. /**
  18. * A list of the exception types that are not reported.
  19. *
  20. * @var array
  21. */
  22. protected $dontReport = [];
  23. /**
  24. * A list of the inputs that are never flashed for validation exceptions.
  25. *
  26. * @var array
  27. */
  28. protected $dontFlash = [
  29. 'password',
  30. 'password_confirmation',
  31. ];
  32. /**
  33. * Report or log an exception.
  34. *
  35. * @param Throwable $exception
  36. *
  37. * @return void
  38. *
  39. * @throws Exception|Throwable
  40. */
  41. public function report(Throwable $exception)
  42. {
  43. // 记录异常来源
  44. Log::info('异常来源:'.get_class($exception));
  45. // 调试模式下记录错误详情
  46. if (config('app.debug')) {
  47. Log::debug('来自链接:'.url()->full());
  48. Log::debug($exception);
  49. }
  50. parent::report($exception);
  51. }
  52. /**
  53. * Render an exception into an HTTP response.
  54. *
  55. * @param Request $request
  56. * @param Throwable $exception
  57. *
  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. return parent::render($request, $exception);
  67. }
  68. // 捕获访问异常
  69. if ($exception instanceof NotFoundHttpException) {
  70. Log::info("异常请求:".$request->fullUrl().",IP:".IP::getClientIp());
  71. if ($request->ajax()) {
  72. return Response::json(['status' => 'fail', 'message' => trans('error.MissingPage')]);
  73. }
  74. return Response::view('auth.error', ['message' => trans('error.MissingPage')], 404);
  75. }
  76. // 捕获身份校验异常
  77. if ($exception instanceof AuthenticationException) {
  78. if ($request->ajax()) {
  79. return Response::json(['status' => 'fail', 'message' => trans('error.Unauthorized')]);
  80. }
  81. return Response::view('auth.error', ['message' => trans('error.Unauthorized')], 401);
  82. }
  83. // 捕获CSRF异常
  84. if ($exception instanceof TokenMismatchException) {
  85. if ($request->ajax()) {
  86. return Response::json([
  87. 'status' => 'fail',
  88. 'message' => trans('error.RefreshPage').'<a href="'.route('login').'" target="_blank">'.trans('error.Refresh').'</a>',
  89. ]);
  90. }
  91. return Response::view('auth.error',
  92. ['message' => trans('error.RefreshPage').'<a href="'.route('login').'" target="_blank">'.trans('error.Refresh').'</a>'], 419);
  93. }
  94. // 捕获反射异常
  95. if ($exception instanceof ReflectionException) {
  96. if ($request->ajax()) {
  97. return Response::json(['status' => 'fail', 'message' => trans('error.SystemError')]);
  98. }
  99. return Response::view('auth.error', ['message' => trans('error.SystemError')], 500);
  100. }
  101. // 捕获系统错误异常
  102. if ($exception instanceof ErrorException) {
  103. if ($request->ajax()) {
  104. return Response::json([
  105. 'status' => 'fail',
  106. 'message' => trans('error.SystemError').', '.trans('error.Visit').'<a href="'.route('log.viewer').'" target="_blank">'.trans('error.log').'</a>',
  107. ]);
  108. }
  109. return Response::view('auth.error',
  110. ['message' => trans('error.SystemError').', '.trans('error.Visit').'<a href="'.route('log.viewer').'" target="_blank">'.trans('error.log').'</a>'],
  111. 500);
  112. }
  113. return parent::render($request, $exception);
  114. }
  115. }