Handler.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 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. * @return void
  37. *
  38. * @throws Throwable
  39. */
  40. public function report(Throwable $exception)
  41. {
  42. // 记录异常来源
  43. Log::info('异常来源:'.get_class($exception));
  44. // 调试模式下记录错误详情
  45. if (config('app.debug')) {
  46. Log::debug('来自链接:'.url()->full());
  47. Log::debug($exception);
  48. }
  49. parent::report($exception);
  50. }
  51. /**
  52. * Render an exception into an HTTP response.
  53. *
  54. * @param Request $request
  55. * @param Throwable $exception
  56. * @return \Symfony\Component\HttpFoundation\Response
  57. *
  58. * @throws Throwable
  59. */
  60. public function render($request, Throwable $exception)
  61. {
  62. // 调试模式下直接返回错误信息,非调试模式下渲染在返回
  63. if (! config('app.debug')) {
  64. switch ($exception) {
  65. case $exception instanceof NotFoundHttpException: // 捕获访问异常
  66. Log::info('异常请求:'.$request->fullUrl().',IP:'.IP::getClientIp());
  67. if ($request->ajax()) {
  68. return Response::json(['status' => 'fail', 'message' => trans('error.MissingPage')]);
  69. }
  70. return Response::view('auth.error', ['message' => trans('error.MissingPage')], 404);
  71. case $exception instanceof AuthenticationException: // 捕获身份校验异常
  72. if ($request->ajax()) {
  73. return Response::json(['status' => 'fail', 'message' => trans('error.Unauthorized')]);
  74. }
  75. return Response::view('auth.error', ['message' => trans('error.Unauthorized')], 401);
  76. case $exception instanceof TokenMismatchException: // 捕获CSRF异常
  77. if ($request->ajax()) {
  78. return Response::json([
  79. 'status' => 'fail',
  80. 'message' => trans('error.RefreshPage').'<a href="'.route('login').'" target="_blank">'.trans('error.Refresh').'</a>',
  81. ]);
  82. }
  83. return Response::view(
  84. 'auth.error',
  85. ['message' => trans('error.RefreshPage').'<a href="'.route('login').'" target="_blank">'.trans('error.Refresh').'</a>'],
  86. 419
  87. );
  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(
  101. 'auth.error',
  102. ['message' => trans('error.SystemError').', '.trans('error.Visit').'<a href="'.route('admin.log.viewer').'" target="_blank">'.trans('error.log').'</a>'],
  103. 500
  104. );
  105. case $exception instanceof ConnectionException:
  106. if ($request->ajax()) {
  107. return Response::json([
  108. 'status' => 'fail',
  109. 'message' => $exception->getMessage(),
  110. ]);
  111. }
  112. return Response::view('auth.error', ['message' => $exception->getMessage()], 408);
  113. default:
  114. if ($request->ajax()) {
  115. return Response::json([
  116. 'status' => 'fail',
  117. 'message' => $exception->getMessage(),
  118. ]);
  119. }
  120. return Response::view('auth.error', ['message' => $exception->getMessage()]);
  121. }
  122. }
  123. return parent::render($request, $exception);
  124. }
  125. }