fix: Update error handling to Laisky/errors/v2 package across project

- Updated error handling across multiple files with `Laisky/errors/v2` package
- Replaced hardcoded error messages with `Laisky/errors` in relay/channel/tencent/adaptor.go
- Added a function to check if a request should be retried in relay/controller/relay.go
- Removed unused imports and variables, and updated comments in various files
- Changed Redis cache handling in model/cache.go
- Refactored error handling in relay/channel/tencent/main.go and relay/channel/baidu/main.go
- Updated import paths and error handling in model/user.go, model/redemption.go, and controller/github.go
- Added import for tiktoken-go package in relay/channel/openai/token.go
- Added GetSign and ParseConfig functions in relay/channel/tencent/main.go
- Replaced specific error imports with a more general one in relay/channel/ali/adaptor.go
- Updated import comments and function calls in relay/channel/ali/adaptor.go
- Added checks and custom error messages in model/token.go
- Removed unused functions and variables in relay/channel/baidu/adaptor.go
- Imported "github.com/Laisky/errors/v2" package in controller/channel-billing.go
- Removed unused import packages in [relay/channel/tencent/adaptor.go](http://relay/channel/tencent/adaptor.go) and relay/channel/palm/adaptor.go
- Updated go.mod and go.sum files with new dependencies and versions
This commit is contained in:
Laisky.Cai
2024-03-12 06:40:23 +00:00
parent 914f1ccd8c
commit 54203e3d30
30 changed files with 38 additions and 35 deletions

View File

@@ -2,8 +2,8 @@ package model
import (
"encoding/json"
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/logger"
@@ -48,6 +48,7 @@ func CacheGetTokenByKey(key string) (*Token, error) {
}
return &token, nil
}
err = json.Unmarshal([]byte(tokenObjectString), &token)
return &token, err
}

View File

@@ -1,8 +1,8 @@
package model
import (
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/helper"
"gorm.io/gorm"

View File

@@ -1,8 +1,9 @@
package model
import (
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/helper"
@@ -45,10 +46,12 @@ func ValidateUserToken(key string) (token *Token, err error) {
if err != nil {
logger.SysError("CacheGetTokenByKey failed: " + err.Error())
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("无效的令牌")
return nil, errors.Wrap(err, "token not found")
}
return nil, errors.New("令牌验证失败")
return nil, errors.Wrap(err, "failed to get token by key")
}
if token.Status == common.TokenStatusExhausted {
return nil, errors.New("该令牌额度已用尽")
} else if token.Status == common.TokenStatusExpired {

View File

@@ -1,8 +1,8 @@
package model
import (
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/blacklist"
"github.com/songquanpeng/one-api/common/config"