Handler.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Exceptions;
  3. use ErrorException;
  4. use Exception;
  5. use Illuminate\Auth\AuthenticationException;
  6. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  7. use Illuminate\Session\TokenMismatchException;
  8. use InvalidArgumentException;
  9. use ReflectionException;
  10. use RuntimeException;
  11. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  14. use Tymon\JWTAuth\Exceptions\TokenExpiredException;
  15. use Tymon\JWTAuth\Exceptions\TokenInvalidException;
  16. class Handler extends ExceptionHandler
  17. {
  18. /**
  19. * A list of the exception types that are not reported.
  20. *
  21. * @var array
  22. */
  23. protected $dontReport = [
  24. //
  25. ];
  26. /**
  27. * A list of the inputs that are never flashed for validation exceptions.
  28. *
  29. * @var array
  30. */
  31. protected $dontFlash = [
  32. 'password',
  33. 'password_confirmation',
  34. ];
  35. /**
  36. * Report or log an exception.
  37. *
  38. * @param Exception $exception
  39. *
  40. * @return mixed|void
  41. * @throws Exception
  42. */
  43. public function report(Exception $exception)
  44. {
  45. // 记录异常来源
  46. \Log::info('异常来源:' . get_class($exception));
  47. // 调试模式下记录错误详情
  48. if (config('app.debug')) {
  49. \Log::info($exception);
  50. }
  51. parent::report($exception);
  52. }
  53. /**
  54. * Render an exception into an HTTP response.
  55. *
  56. * @param \Illuminate\Http\Request $request
  57. * @param \Exception $exception
  58. *
  59. * @return \Illuminate\Http\Response
  60. */
  61. public function render($request, Exception $exception)
  62. {
  63. // 调试模式下直接返回错误信息
  64. if (config('app.debug')) {
  65. return parent::render($request, $exception);
  66. }
  67. // 捕获未生成key异常
  68. if ($exception instanceof RuntimeException) {
  69. if ($request->expectsJson()) {
  70. return response()->json(['status' => 'fail', 'data' => '', 'message' => $exception->getMessage()]);
  71. } else {
  72. return response()->view('auth.error', ['message' => $exception->getMessage()]);
  73. }
  74. }
  75. // 捕获访问异常
  76. if ($exception instanceof NotFoundHttpException) {
  77. \Log::info("异常请求:" . $request->fullUrl() . ",IP:" . getClientIp());
  78. if ($request->expectsJson()) {
  79. return response()->json(['status' => 'fail', 'data' => '', 'message' => $exception->getMessage()]);
  80. } else {
  81. return response()->view('auth.error', ['message' => $exception->getMessage()]);
  82. }
  83. }
  84. // 路由参数异常
  85. if ($exception instanceof InvalidArgumentException) {
  86. if ($request->expectsJson()) {
  87. return response()->json(['status' => 'fail', 'data' => '', 'message' => $exception->getMessage()]);
  88. } else {
  89. return response()->view('auth.error', ['message' => $exception->getMessage()]);
  90. }
  91. }
  92. // 请求方式不允许异常
  93. if ($exception instanceof MethodNotAllowedHttpException) {
  94. if ($request->expectsJson()) {
  95. return response()->json(['status' => 'fail', 'data' => '', 'message' => $exception->getMessage()]);
  96. } else {
  97. return response()->view('auth.error', ['message' => $exception->getMessage()]);
  98. }
  99. }
  100. // 捕获身份校验异常
  101. if ($exception instanceof AuthenticationException) {
  102. if ($request->expectsJson()) {
  103. return response()->json(['status' => 'fail', 'data' => '', 'message' => $exception->getMessage()]);
  104. } else {
  105. return response()->view('auth.error', ['message' => $exception->getMessage()]);
  106. }
  107. }
  108. // 捕获CSRF异常
  109. if ($exception instanceof TokenMismatchException) {
  110. if ($request->expectsJson()) {
  111. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'System Error, Please Refresh Page, Try One More Time']);
  112. } else {
  113. return response()->view('auth.error', ['message' => trans('error.RefreshPage')]);
  114. }
  115. }
  116. // 捕获反射异常
  117. if ($exception instanceof ReflectionException) {
  118. if ($request->expectsJson()) {
  119. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'System Error']);
  120. } else {
  121. return response()->view('auth.error', ['message' => trans('error.SystemError')]);
  122. }
  123. }
  124. // 捕获系统错误异常
  125. if ($exception instanceof ErrorException) {
  126. if ($request->expectsJson()) {
  127. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'System Error']);
  128. } else {
  129. return response()->view('auth.error', ['message' => trans('error.SystemError') . ', ' . trans('error.Visit') . '<a href="/logs" target="_blank">' . trans('error.log') . '</a>']);
  130. }
  131. }
  132. // 未授权异常
  133. if ($exception instanceof UnauthorizedHttpException) {
  134. if ($request->expectsJson()) {
  135. return response()->json(['status' => 'fail', 'data' => '', 'message' => $exception->getMessage()]);
  136. } else {
  137. return response()->view('auth.error', ['message' => $exception->getMessage()]);
  138. }
  139. }
  140. // 客户端API:捕获认证过期异常
  141. if ($exception instanceof TokenExpiredException) {
  142. return response()->json(['status' => 'fail', 'data' => '', 'message' => $exception->getMessage()]);
  143. }
  144. // 客户端API:捕获认证不合法异常
  145. if ($exception instanceof TokenInvalidException) {
  146. return response()->json(['status' => 'fail', 'data' => '', 'message' => $exception->getMessage()]);
  147. }
  148. return parent::render($request, $exception);
  149. }
  150. }