From bbe1104a0042ea6e84b7e33397088878afc1e60e Mon Sep 17 00:00:00 2001 From: cktsun1031 <65409152+cktsun1031@users.noreply.github.com> Date: Mon, 6 Nov 2023 21:37:33 +0800 Subject: [PATCH] Refactored error declaration, removed unnecessary return statements, and added error handling in PostConsumeTokenQuota function --- controller/log.go | 1 - controller/misc.go | 7 ------- controller/option.go | 2 -- controller/redemption.go | 9 ++------- controller/token.go | 9 ++------- controller/user.go | 14 -------------- controller/wechat.go | 4 ++-- model/ability.go | 2 +- model/channel.go | 2 +- model/token.go | 3 +++ model/user.go | 2 +- 11 files changed, 12 insertions(+), 43 deletions(-) diff --git a/controller/log.go b/controller/log.go index 04f6f8c2..6327a6a8 100644 --- a/controller/log.go +++ b/controller/log.go @@ -159,5 +159,4 @@ func DeleteHistoryLogs(c *gin.Context) { "message": "", "data": count, }) - return } diff --git a/controller/misc.go b/controller/misc.go index 1318066e..334505dd 100644 --- a/controller/misc.go +++ b/controller/misc.go @@ -39,7 +39,6 @@ func GetStatus(c *gin.Context) { "display_in_currency": common.DisplayInCurrencyEnabled, }, }) - return } func GetNotice(c *gin.Context) { @@ -50,7 +49,6 @@ func GetNotice(c *gin.Context) { "message": "", "data": common.OptionMap["Notice"], }) - return } func GetAbout(c *gin.Context) { @@ -61,7 +59,6 @@ func GetAbout(c *gin.Context) { "message": "", "data": common.OptionMap["About"], }) - return } func GetHomePageContent(c *gin.Context) { @@ -72,7 +69,6 @@ func GetHomePageContent(c *gin.Context) { "message": "", "data": common.OptionMap["HomePageContent"], }) - return } func SendEmailVerification(c *gin.Context) { @@ -125,7 +121,6 @@ func SendEmailVerification(c *gin.Context) { "success": true, "message": "", }) - return } func SendPasswordResetEmail(c *gin.Context) { @@ -164,7 +159,6 @@ func SendPasswordResetEmail(c *gin.Context) { "success": true, "message": "", }) - return } type PasswordResetRequest struct { @@ -212,5 +206,4 @@ func ResetPassword(c *gin.Context) { "message": "", "data": password, }) - return } diff --git a/controller/option.go b/controller/option.go index eae0e5e6..6e92ac5d 100644 --- a/controller/option.go +++ b/controller/option.go @@ -28,7 +28,6 @@ func GetOptions(c *gin.Context) { "message": "", "data": options, }) - return } func UpdateOption(c *gin.Context) { @@ -103,5 +102,4 @@ func UpdateOption(c *gin.Context) { "success": true, "message": "", }) - return } diff --git a/controller/redemption.go b/controller/redemption.go index 0f656be0..963ab2de 100644 --- a/controller/redemption.go +++ b/controller/redemption.go @@ -1,11 +1,12 @@ package controller import ( - "github.com/gin-gonic/gin" "net/http" "one-api/common" "one-api/model" "strconv" + + "github.com/gin-gonic/gin" ) func GetAllRedemptions(c *gin.Context) { @@ -26,7 +27,6 @@ func GetAllRedemptions(c *gin.Context) { "message": "", "data": redemptions, }) - return } func SearchRedemptions(c *gin.Context) { @@ -44,7 +44,6 @@ func SearchRedemptions(c *gin.Context) { "message": "", "data": redemptions, }) - return } func GetRedemption(c *gin.Context) { @@ -69,7 +68,6 @@ func GetRedemption(c *gin.Context) { "message": "", "data": redemption, }) - return } func AddRedemption(c *gin.Context) { @@ -129,7 +127,6 @@ func AddRedemption(c *gin.Context) { "message": "", "data": keys, }) - return } func DeleteRedemption(c *gin.Context) { @@ -146,7 +143,6 @@ func DeleteRedemption(c *gin.Context) { "success": true, "message": "", }) - return } func UpdateRedemption(c *gin.Context) { @@ -188,5 +184,4 @@ func UpdateRedemption(c *gin.Context) { "message": "", "data": cleanRedemption, }) - return } diff --git a/controller/token.go b/controller/token.go index 8642122c..a4e3a235 100644 --- a/controller/token.go +++ b/controller/token.go @@ -1,11 +1,12 @@ package controller import ( - "github.com/gin-gonic/gin" "net/http" "one-api/common" "one-api/model" "strconv" + + "github.com/gin-gonic/gin" ) func GetAllTokens(c *gin.Context) { @@ -27,7 +28,6 @@ func GetAllTokens(c *gin.Context) { "message": "", "data": tokens, }) - return } func SearchTokens(c *gin.Context) { @@ -46,7 +46,6 @@ func SearchTokens(c *gin.Context) { "message": "", "data": tokens, }) - return } func GetToken(c *gin.Context) { @@ -72,7 +71,6 @@ func GetToken(c *gin.Context) { "message": "", "data": token, }) - return } func GetTokenStatus(c *gin.Context) { @@ -138,7 +136,6 @@ func AddToken(c *gin.Context) { "success": true, "message": "", }) - return } func DeleteToken(c *gin.Context) { @@ -156,7 +153,6 @@ func DeleteToken(c *gin.Context) { "success": true, "message": "", }) - return } func UpdateToken(c *gin.Context) { @@ -224,5 +220,4 @@ func UpdateToken(c *gin.Context) { "message": "", "data": cleanToken, }) - return } diff --git a/controller/user.go b/controller/user.go index 8fd10b82..45732a45 100644 --- a/controller/user.go +++ b/controller/user.go @@ -173,7 +173,6 @@ func Register(c *gin.Context) { "success": true, "message": "", }) - return } func GetAllUsers(c *gin.Context) { @@ -194,7 +193,6 @@ func GetAllUsers(c *gin.Context) { "message": "", "data": users, }) - return } func SearchUsers(c *gin.Context) { @@ -212,7 +210,6 @@ func SearchUsers(c *gin.Context) { "message": "", "data": users, }) - return } func GetUser(c *gin.Context) { @@ -245,7 +242,6 @@ func GetUser(c *gin.Context) { "message": "", "data": user, }) - return } func GenerateAccessToken(c *gin.Context) { @@ -281,7 +277,6 @@ func GenerateAccessToken(c *gin.Context) { "message": "", "data": user.AccessToken, }) - return } func GetAffCode(c *gin.Context) { @@ -309,7 +304,6 @@ func GetAffCode(c *gin.Context) { "message": "", "data": user.AffCode, }) - return } func GetSelf(c *gin.Context) { @@ -327,7 +321,6 @@ func GetSelf(c *gin.Context) { "message": "", "data": user, }) - return } func UpdateUser(c *gin.Context) { @@ -391,7 +384,6 @@ func UpdateUser(c *gin.Context) { "success": true, "message": "", }) - return } func UpdateSelf(c *gin.Context) { @@ -438,7 +430,6 @@ func UpdateSelf(c *gin.Context) { "success": true, "message": "", }) - return } func DeleteUser(c *gin.Context) { @@ -500,7 +491,6 @@ func DeleteSelf(c *gin.Context) { "success": true, "message": "", }) - return } func CreateUser(c *gin.Context) { @@ -549,7 +539,6 @@ func CreateUser(c *gin.Context) { "success": true, "message": "", }) - return } type ManageRequest struct { @@ -666,7 +655,6 @@ func ManageUser(c *gin.Context) { "message": "", "data": clearUser, }) - return } func EmailBind(c *gin.Context) { @@ -708,7 +696,6 @@ func EmailBind(c *gin.Context) { "success": true, "message": "", }) - return } type topUpRequest struct { @@ -739,5 +726,4 @@ func TopUp(c *gin.Context) { "message": "", "data": quota, }) - return } diff --git a/controller/wechat.go b/controller/wechat.go index ff4c9fb6..fbd7d2bd 100644 --- a/controller/wechat.go +++ b/controller/wechat.go @@ -4,12 +4,13 @@ import ( "encoding/json" "errors" "fmt" - "github.com/gin-gonic/gin" "net/http" "one-api/common" "one-api/model" "strconv" "time" + + "github.com/gin-gonic/gin" ) type wechatLoginResponse struct { @@ -160,5 +161,4 @@ func WeChatBind(c *gin.Context) { "success": true, "message": "", }) - return } diff --git a/model/ability.go b/model/ability.go index 01e5bb62..2182ae98 100644 --- a/model/ability.go +++ b/model/ability.go @@ -24,7 +24,7 @@ func GetRandomSatisfiedChannel(group string, model string, stream bool) (*Channe trueVal = "true" } - var err error = nil + var err error cmdWhere := groupCol + " = ? and model = ? and enabled = " + trueVal diff --git a/model/channel.go b/model/channel.go index 7fdea9d9..18f23c5b 100644 --- a/model/channel.go +++ b/model/channel.go @@ -51,7 +51,7 @@ func SearchChannels(keyword string) (channels []*Channel, err error) { func GetChannelById(id int, selectAll bool) (*Channel, error) { channel := Channel{Id: id} - var err error = nil + var err error if selectAll { err = DB.First(&channel, "id = ?", id).Error } else { diff --git a/model/token.go b/model/token.go index c9db2c70..3791eac7 100644 --- a/model/token.go +++ b/model/token.go @@ -218,6 +218,9 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) { func PostConsumeTokenQuota(tokenId int, quota int) (err error) { token, err := GetTokenById(tokenId) + if err != nil { + return err + } if quota > 0 { err = DecreaseUserQuota(token.UserId, quota) } else { diff --git a/model/user.go b/model/user.go index eb87c178..679e60a1 100644 --- a/model/user.go +++ b/model/user.go @@ -54,7 +54,7 @@ func GetUserById(id int, selectAll bool) (*User, error) { return nil, errors.New("id 为空!") } user := User{Id: id} - var err error = nil + var err error if selectAll { err = DB.First(&user, "id = ?", id).Error } else {