Handler.php 5.2 KB

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