Handler.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Generated\Exceptions\ApiNotFoundException;
  4. use App\Generated\Exceptions\AuthExpiredException;
  5. use App\Generated\Exceptions\MaintainModeException;
  6. use App\Generated\Exceptions\ServerInternalErrorException;
  7. use App\Services\WeChat\Base;
  8. use Exception;
  9. use Illuminate\Database\Eloquent\ModelNotFoundException;
  10. use Illuminate\Database\QueryException;
  11. use Illuminate\Support\Facades\Config;
  12. use Illuminate\Validation\ValidationException;
  13. use Kamicloud\StubApi\Exceptions\BaseException;
  14. use Sentry\SentryLaravel\SentryFacade;
  15. use Symfony\Component\HttpKernel\Exception\HttpException;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  18. use Tymon\JWTAuth\Exceptions\JWTException;
  19. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  20. class Handler extends ExceptionHandler
  21. {
  22. /**
  23. * A list of the exception types that are not reported.
  24. * 未报告的异常类型列表
  25. * @var array
  26. */
  27. protected $dontReport = [
  28. ApiException::class,
  29. AlertException::class,
  30. VersionException::class,
  31. JWTException::class,
  32. VersionException::class,
  33. DBException::class,
  34. NoticeException::class,
  35. ModelNotFoundException::class,
  36. WeChatException::class,
  37. BaseException::class,
  38. ];
  39. /**
  40. * A list of the inputs that are never flashed for validation exceptions.
  41. * 从不为验证异常刷新的输入列表
  42. * @var array
  43. */
  44. protected $dontFlash = [
  45. 'password',
  46. 'password_confirmation',
  47. ];
  48. /**
  49. * Report or log an exception.
  50. * 报告或记录异常
  51. *
  52. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  53. * 这是一个向Sentry、Bugsnag,etc等发送例外情况的好地方。
  54. *
  55. * @param \Exception $exception
  56. * @return void
  57. * @throws Exception
  58. */
  59. public function report(Exception $exception)
  60. {
  61. if (app()->bound('sentry') && $this->shouldReport($exception)) {
  62. SentryFacade::captureException($exception, [
  63. 'user' => [
  64. 'id' => Config::get('uid', null)
  65. ]
  66. ]);
  67. }
  68. parent::report($exception);
  69. }
  70. /**
  71. * Render an exception into an HTTP response.
  72. * 将异常呈现到HTTP响应中
  73. *
  74. * @param \Illuminate\Http\Request $request
  75. * @param \Exception $exception
  76. * @return \Symfony\Component\HttpFoundation\Response
  77. * @throws Exception
  78. */
  79. public function render($request, Exception $exception)
  80. {
  81. if (starts_with($request->getRequestUri(), '/stub-api')) {
  82. if ($exception instanceof \Illuminate\Foundation\Http\Exceptions\MaintenanceModeException) {
  83. $exception = config('generator.exceptions.maintain-mode', MaintainModeException::class);
  84. $exception = new $exception('Maintaining');
  85. }
  86. if ($exception instanceof BaseException) {
  87. return response()->json($exception->toResponse($request));
  88. }
  89. if ($exception instanceof AlertException) {
  90. return parent::render($request, $exception);
  91. }
  92. if ($exception instanceof JWTException) {
  93. throw new AuthExpiredException('登陆已过期,请重新登陆');
  94. }
  95. if ($exception instanceof NotFoundHttpException) {
  96. throw new ApiNotFoundException('接口不存在');
  97. }
  98. if ($exception instanceof MethodNotAllowedException) {
  99. throw new ApiNotFoundException('请求方式不合法');
  100. }
  101. if (config('app.debug', false) !== true && !$request->input('__test_mode', false)) {
  102. return parent::render($request, $exception);
  103. } else {
  104. $exceptionClass = config(
  105. 'generator.exceptions.server-internal-error',
  106. ServerInternalErrorException::class
  107. );
  108. return self::render($request, new $exceptionClass('Something went wrong.'));
  109. }
  110. } else {
  111. if ($exception instanceof JWTException) {
  112. return response([
  113. 'code' => 301,
  114. 'status_code' => 401,
  115. 'message' => "登陆已过期,请重新登陆",
  116. 'error_message' => $exception->getMessage()
  117. ]);
  118. }
  119. if ($exception instanceof ValidationException) {
  120. return response([
  121. 'code' => 422,
  122. 'status_code' => 422,
  123. 'message' => $exception->validator->errors()->first(),
  124. 'errors' => $exception->errors(),
  125. ]);
  126. }
  127. if ($exception instanceof HttpException) {
  128. return response([
  129. 'code' => $exception->getStatusCode(),
  130. 'status_code' => $exception->getStatusCode(),
  131. 'message' => '请求异常[http]',
  132. 'error_message' => $exception->getMessage() ?: "http请求错误"
  133. ]);
  134. }
  135. if ($exception instanceof QueryException) {
  136. return response([
  137. 'code' => 503,
  138. 'status_code' => 500,
  139. 'message' => "请求异常[DB]",
  140. 'error_message' => $exception->getMessage(),
  141. 'error_code' => $exception->getCode(),
  142. 'sql' => $exception->getSql(),
  143. ]);
  144. }
  145. if ($exception instanceof ModelNotFoundException) {
  146. return response([
  147. 'code' => 404,
  148. 'status_code' => 404,
  149. 'message' => '不存在',
  150. 'error_message' => $exception->getMessage(),
  151. 'model' => $exception->getModel()
  152. ]);
  153. }
  154. if ($request->get('debug') == 98047) {
  155. return response([
  156. 'code' => $exception->getCode(),
  157. 'message' => $exception->getMessage(),
  158. 'exception' => $exception->getPrevious(),
  159. 'file' => $exception->getFile(),
  160. 'line' => $exception->getLine(),
  161. 'trace' => $exception->getTrace()
  162. ]);
  163. } else {
  164. return parent::render($request, $exception);
  165. }
  166. }
  167. }
  168. }