Handler.php 5.2 KB

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