Handler.php 5.4 KB

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