From 3a27c90910b28f444bee42312af67dcfbfc2dd6a Mon Sep 17 00:00:00 2001 From: qinguoyi <1532979219@qq.com> Date: Sat, 21 Sep 2024 23:00:29 +0800 Subject: [PATCH] fix: getTokenById return token nil, make panic (#1728) * fix:getTokenById return token nil, make panic * chore: remove useless err check --------- Co-authored-by: JustSong --- controller/billing.go | 8 +++++--- model/token.go | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/controller/billing.go b/controller/billing.go index 0d03e4c189..e837157f03 100644 --- a/controller/billing.go +++ b/controller/billing.go @@ -17,9 +17,11 @@ func GetSubscription(c *gin.Context) { if config.DisplayTokenStatEnabled { tokenId := c.GetInt(ctxkey.TokenId) token, err = model.GetTokenById(tokenId) - expiredTime = token.ExpiredTime - remainQuota = token.RemainQuota - usedQuota = token.UsedQuota + if err == nil { + expiredTime = token.ExpiredTime + remainQuota = token.RemainQuota + usedQuota = token.UsedQuota + } } else { userId := c.GetInt(ctxkey.Id) remainQuota, err = model.GetUserQuota(userId) diff --git a/model/token.go b/model/token.go index 96e6b4918a..9616d23624 100644 --- a/model/token.go +++ b/model/token.go @@ -254,14 +254,14 @@ func PreConsumeTokenQuota(tokenId int, quota int64) (err error) { func PostConsumeTokenQuota(tokenId int, quota int64) (err error) { token, err := GetTokenById(tokenId) + if err != nil { + return err + } if quota > 0 { err = DecreaseUserQuota(token.UserId, quota) } else { err = IncreaseUserQuota(token.UserId, -quota) } - if err != nil { - return err - } if !token.UnlimitedQuota { if quota > 0 { err = DecreaseTokenQuota(tokenId, quota)