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

@ -3,7 +3,7 @@ package message
import (
"bytes"
"encoding/json"
"errors"
"github.com/Laisky/errors/v2"
"github.com/songquanpeng/one-api/common/config"
"net/http"
)

View File

@ -2,8 +2,8 @@ package controller
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"

View File

@ -3,8 +3,8 @@ package controller
import (
"bytes"
"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"

View File

@ -3,8 +3,8 @@ package controller
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"

View File

@ -98,16 +98,12 @@ func Relay(c *gin.Context) {
}
}
// shouldRetry returns nil if should retry, otherwise returns error
func shouldRetry(c *gin.Context, statusCode int) error {
if v, ok := c.Get("specific_channel_id"); ok {
return errors.Errorf("specific channel = %v", v)
}
if statusCode == http.StatusTooManyRequests {
return nil
}
if statusCode/100 == 5 {
return nil
}
if statusCode == http.StatusBadRequest {
return errors.Errorf("status code = %d", statusCode)
}

View File

@ -2,8 +2,8 @@ package controller
import (
"encoding/json"
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/songquanpeng/one-api
go 1.21
require (
github.com/Laisky/errors/v2 v2.0.1
github.com/gin-contrib/cors v1.5.0
github.com/gin-contrib/gzip v0.0.6
github.com/gin-contrib/sessions v0.0.5

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/Laisky/errors/v2 v2.0.1 h1:yqCBrRzaP012AMB+7fVlXrP34OWRHrSO/hZ38CFdH84=
github.com/Laisky/errors/v2 v2.0.1/go.mod h1:mTn1LHSmKm4CYug0rpYO7rz13dp/DKrtzlSELSrxvT0=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.1 h1:7a1wuFXL1cMy7a3f7/VFcEtriuXQnUBhtoVfOZiaysc=

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"

View File

@ -1,8 +1,8 @@
package aiproxy
import (
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/relay/channel"

View File

@ -1,7 +1,7 @@
package ali
// import (
// "errors"
// "github.com/Laisky/errors/v2"
// "fmt"
// "github.com/gin-gonic/gin"
// "github.com/songquanpeng/one-api/common"

View File

@ -1,8 +1,8 @@
package anthropic
import (
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"io"
"net/http"

View File

@ -1,7 +1,7 @@
package baidu
// import (
// "errors"
// "github.com/Laisky/errors/v2"
// "github.com/gin-gonic/gin"
// "github.com/songquanpeng/one-api/relay/channel"
// "github.com/songquanpeng/one-api/relay/constant"

View File

@ -3,7 +3,7 @@ package baidu
// import (
// "bufio"
// "encoding/json"
// "errors"
// "github.com/Laisky/errors/v2"
// "fmt"
// "github.com/gin-gonic/gin"
// "io"

View File

@ -1,8 +1,8 @@
package channel
import (
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/relay/util"
"io"

View File

@ -1,8 +1,8 @@
package gemini
import (
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/helper"
channelhelper "github.com/songquanpeng/one-api/relay/channel"

View File

@ -1,8 +1,8 @@
package openai
import (
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/relay/channel"

View File

@ -1,8 +1,8 @@
package openai
import (
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/pkoukk/tiktoken-go"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"

View File

@ -1,8 +1,8 @@
package palm
import (
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/relay/channel"
"github.com/songquanpeng/one-api/relay/channel/openai"

View File

@ -1,7 +1,7 @@
package tencent
// import (
// "errors"
// "github.com/Laisky/errors/v2"
// "fmt"
// "github.com/gin-gonic/gin"
// "github.com/songquanpeng/one-api/relay/channel"

View File

@ -6,7 +6,7 @@ package tencent
// "crypto/sha1"
// "encoding/base64"
// "encoding/json"
// "errors"
// "github.com/Laisky/errors/v2"
// "fmt"
// "github.com/gin-gonic/gin"
// "github.com/songquanpeng/one-api/common"

View File

@ -1,7 +1,7 @@
package xunfei
// import (
// "errors"
// "github.com/Laisky/errors/v2"
// "github.com/gin-gonic/gin"
// "github.com/songquanpeng/one-api/relay/channel"
// "github.com/songquanpeng/one-api/relay/channel/openai"

View File

@ -1,7 +1,7 @@
package zhipu
// import (
// "errors"
// "github.com/Laisky/errors/v2"
// "fmt"
// "github.com/gin-gonic/gin"
// "github.com/songquanpeng/one-api/relay/channel"

View File

@ -5,8 +5,8 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"

View File

@ -2,8 +2,8 @@ package controller
import (
"context"
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"

View File

@ -4,8 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/Laisky/errors/v2"
"io"
"net/http"
"strings"

View File

@ -1,7 +1,7 @@
package util
import (
"errors"
"github.com/Laisky/errors/v2"
"github.com/songquanpeng/one-api/relay/constant"
"github.com/songquanpeng/one-api/relay/model"
"math"