Handler.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // 记录异常来源
  48. Log::info('异常来源:'.get_class($exception));
  49. // 调试模式下记录错误详情
  50. if (config('app.debug') || config('app.demo')) {
  51. Log::debug('来自链接:'.url()->full());
  52. Log::debug($exception);
  53. }
  54. parent::report($exception);
  55. }
  56. /**
  57. * Render an exception into an HTTP response.
  58. *
  59. * @param Request $request
  60. * @param Throwable $exception
  61. * @return \Symfony\Component\HttpFoundation\Response
  62. *
  63. * @throws Throwable
  64. */
  65. public function render($request, Throwable $exception)
  66. {
  67. // 调试模式下直接返回错误信息,非调试模式下渲染在返回
  68. if (! config('app.debug')) {
  69. switch ($exception) {
  70. case $exception instanceof NotFoundHttpException: // 捕获访问异常
  71. Log::info('异常请求:'.$request->fullUrl().',IP:'.IP::getClientIp());
  72. if ($request->ajax() || $request->wantsJson()) {
  73. return Response::json(['status' => 'fail', 'message' => trans('error.missing_page')], 404);
  74. }
  75. return Response::view('auth.error', ['message' => trans('error.missing_page')], 404);
  76. case $exception instanceof AuthenticationException: // 捕获身份校验异常
  77. if ($request->ajax() || $request->wantsJson()) {
  78. return Response::json(['status' => 'fail', 'message' => trans('error.unauthorized')], 401);
  79. }
  80. return Response::view('auth.error', ['message' => trans('error.unauthorized')], 401);
  81. case $exception instanceof TokenMismatchException: // 捕获CSRF异常
  82. if ($request->ajax() || $request->wantsJson()) {
  83. return Response::json([
  84. 'status' => 'fail',
  85. 'message' => trans('error.refresh_page').'<a href="'.route('login').'" target="_blank">'.trans('error.refresh').'</a>',
  86. ], 419);
  87. }
  88. return Response::view(
  89. 'auth.error',
  90. ['message' => trans('error.refresh_page').'<a href="'.route('login').'" target="_blank">'.trans('error.refresh').'</a>'],
  91. 419
  92. );
  93. case $exception instanceof ReflectionException:
  94. if ($request->ajax() || $request->wantsJson()) {
  95. return Response::json(['status' => 'fail', 'message' => trans('error.system')], 500);
  96. }
  97. return Response::view('auth.error', ['message' => trans('error.system')], 500);
  98. case $exception instanceof ErrorException: // 捕获系统错误异常
  99. if ($request->ajax() || $request->wantsJson()) {
  100. return Response::json([
  101. 'status' => 'fail',
  102. 'message' => trans('error.system').', '.trans('error.visit').'<a href="'.route('admin.log.viewer').'" target="_blank">'.trans('error.log').'</a>',
  103. ], 500);
  104. }
  105. return Response::view(
  106. 'auth.error',
  107. ['message' => trans('error.system').', '.trans('error.visit').'<a href="'.route('admin.log.viewer').'" target="_blank">'.trans('error.log').'</a>'],
  108. 500
  109. );
  110. case $exception instanceof ConnectionException:
  111. if ($request->ajax() || $request->wantsJson()) {
  112. return Response::json(['status' => 'fail', 'message' => $exception->getMessage()], 408);
  113. }
  114. return Response::view('auth.error', ['message' => $exception->getMessage()], 408);
  115. }
  116. }
  117. return parent::render($request, $exception);
  118. }
  119. }