ExceptionHandle.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app;
  3. use Throwable;
  4. use think\Response;
  5. use think\facade\Log;
  6. use app\common\util\Result;
  7. use think\exception\Handle;
  8. use Firebase\JWT\ExpiredException;
  9. use think\exception\HttpException;
  10. use Firebase\JWT\BeforeValidException;
  11. use think\exception\ValidateException;
  12. use app\common\exception\CatchException;
  13. use think\exception\HttpResponseException;
  14. use Firebase\JWT\SignatureInvalidException;
  15. use app\common\exception\ParentHttpException;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\ModelNotFoundException;
  18. /**
  19. * 应用异常处理类
  20. */
  21. class ExceptionHandle extends Handle
  22. {
  23. /**
  24. * 不需要记录信息(日志)的异常类列表
  25. * @var array
  26. */
  27. protected $ignoreReport = [
  28. HttpException::class,
  29. HttpResponseException::class,
  30. ModelNotFoundException::class,
  31. DataNotFoundException::class,
  32. //ValidateException::class,
  33. ];
  34. /**
  35. * 记录异常信息(包括日志或者其它方式记录)
  36. *
  37. * @access public
  38. * @param Throwable $exception
  39. * @return void
  40. */
  41. public function report(Throwable $exception): void
  42. {
  43. // 使用内置的方式记录异常日志
  44. parent::report($exception);
  45. }
  46. /**
  47. * Render an exception into an HTTP response.
  48. *
  49. * @access public
  50. * @param \think\Request $request
  51. * @param Throwable $e
  52. * @return Response
  53. */
  54. public function render($request, Throwable $e): Response
  55. {
  56. $className = get_class($e);
  57. if (!in_array($className, $this->ignoreReport)) {
  58. Log::debug("尝试捕获异常[{$e->getCode()}]:" . get_class($e) . " | 提示消息:{$e->getMessage()}" . ($e->getPrevious() ? ' | 父错误:' . get_class($e->getPrevious()) : ''));
  59. }
  60. // 应该被捕获的异常
  61. if ($e instanceof CatchException) {
  62. // 常规错误
  63. return Result::restf($e->getCode(), $e->getMessage());
  64. }
  65. // 参数验证错误
  66. if ($e instanceof ValidateException) {
  67. return Result::restf(777, "参数验证错误:\n " . $e->getMessage());
  68. }
  69. if ($e instanceof SignatureInvalidException) {
  70. // provided JWT signature verification failed.
  71. return Result::restf(601, '无效的Jwt签名');
  72. } elseif ($e instanceof BeforeValidException) {
  73. // provided JWT is trying to be used before "nbf" claim OR
  74. // provided JWT is trying to be used before "iat" claim.
  75. return Result::restf(602, 'Jwt在授权期之前');
  76. } elseif ($e instanceof ExpiredException) {
  77. // provided JWT is trying to be used after "exp" claim.
  78. return Result::restf(603, 'Jwt授权已过期');
  79. }
  80. // 请求异常
  81. if ($e instanceof HttpException && request()->isAjax()) {
  82. return response($e->getMessage(), $e->getStatusCode());
  83. }
  84. if (!in_array($className, $this->ignoreReport)) {
  85. Log::error('未捕获异常:' . $e->getTraceAsString());
  86. }
  87. // 其他错误交给系统处理
  88. return parent::render($request, $e);
  89. }
  90. }