Handler.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\Session\TokenMismatchException;
  8. use Log;
  9. use ReflectionException;
  10. use Response;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Throwable;
  13. class Handler extends ExceptionHandler
  14. {
  15. /**
  16. * A list of the exception types that are not reported.
  17. *
  18. * @var array
  19. */
  20. protected $dontReport = [];
  21. /**
  22. * A list of the inputs that are never flashed for validation exceptions.
  23. *
  24. * @var array
  25. */
  26. protected $dontFlash = [
  27. 'password',
  28. 'password_confirmation',
  29. ];
  30. /**
  31. * Report or log an exception.
  32. *
  33. * @param \Throwable $exception
  34. *
  35. * @return void
  36. *
  37. * @throws \Exception|\Throwable
  38. */
  39. public function report(Throwable $exception)
  40. {
  41. // 记录异常来源
  42. Log::info('异常来源:' . get_class($exception));
  43. // 调试模式下记录错误详情
  44. if (env('APP_DEBUG')) {
  45. Log::debug('来自链接:' . url()->full());
  46. Log::debug($exception);
  47. }
  48. parent::report($exception);
  49. }
  50. /**
  51. * Render an exception into an HTTP response.
  52. *
  53. * @param \Illuminate\Http\Request $request
  54. * @param \Throwable $exception
  55. *
  56. * @return \Symfony\Component\HttpFoundation\Response
  57. *
  58. * @throws \Throwable
  59. */
  60. public function render($request, Throwable $exception)
  61. {
  62. // 调试模式下直接返回错误信息
  63. if (env('APP_DEBUG')) {
  64. return parent::render($request, $exception);
  65. }
  66. // 捕获访问异常
  67. if ($exception instanceof NotFoundHttpException) {
  68. Log::info(
  69. "异常请求:" . $request->fullUrl() . ",IP:" . IP::getClientIp()
  70. );
  71. if ($request->ajax()) {
  72. return Response::json(
  73. [
  74. 'status' => 'fail',
  75. 'message' => trans('error.MissingPage'),
  76. ]
  77. );
  78. }
  79. return Response::view(
  80. 'auth.error',
  81. ['message' => trans('error.MissingPage')],
  82. 404
  83. );
  84. }
  85. // 捕获身份校验异常
  86. if ($exception instanceof AuthenticationException) {
  87. if ($request->ajax()) {
  88. return Response::json(
  89. [
  90. 'status' => 'fail',
  91. 'message' => trans('error.Unauthorized'),
  92. ]
  93. );
  94. }
  95. return Response::view(
  96. 'auth.error',
  97. ['message' => trans('error.Unauthorized')],
  98. 401
  99. );
  100. }
  101. // 捕获CSRF异常
  102. if ($exception instanceof TokenMismatchException) {
  103. if ($request->ajax()) {
  104. return Response::json(
  105. [
  106. 'status' => 'fail',
  107. 'message' => trans(
  108. 'error.RefreshPage'
  109. ) . '<a href="/login" target="_blank">' . trans(
  110. 'error.Refresh'
  111. ) . '</a>',
  112. ]
  113. );
  114. }
  115. return Response::view(
  116. 'auth.error',
  117. [
  118. 'message' => trans(
  119. 'error.RefreshPage'
  120. ) . '<a href="/login" target="_blank">' . trans(
  121. 'error.Refresh'
  122. ) . '</a>',
  123. ],
  124. 419
  125. );
  126. }
  127. // 捕获反射异常
  128. if ($exception instanceof ReflectionException) {
  129. if ($request->ajax()) {
  130. return Response::json(
  131. [
  132. 'status' => 'fail',
  133. 'message' => trans('error.SystemError'),
  134. ]
  135. );
  136. }
  137. return Response::view(
  138. 'auth.error',
  139. ['message' => trans('error.SystemError')],
  140. 500
  141. );
  142. }
  143. // 捕获系统错误异常
  144. if ($exception instanceof ErrorException) {
  145. if ($request->ajax()) {
  146. return Response::json(
  147. [
  148. 'status' => 'fail',
  149. 'message' => trans('error.SystemError') . ', ' . trans(
  150. 'error.Visit'
  151. ) . '<a href="/logs" target="_blank">' . trans(
  152. 'error.log'
  153. ) . '</a>',
  154. ]
  155. );
  156. }
  157. return Response::view(
  158. 'auth.error',
  159. [
  160. 'message' => trans('error.SystemError') . ', ' . trans(
  161. 'error.Visit'
  162. ) . '<a href="/logs" target="_blank">' . trans(
  163. 'error.log'
  164. ) . '</a>',
  165. ],
  166. 500
  167. );
  168. }
  169. return parent::render($request, $exception);
  170. }
  171. }