diff --git a/common/message/email.go b/common/message/email.go index fe5d8704..585aa37a 100644 --- a/common/message/email.go +++ b/common/message/email.go @@ -5,15 +5,17 @@ import ( "crypto/tls" "encoding/base64" "fmt" - "github.com/songquanpeng/one-api/common/config" "net/smtp" "strings" "time" + + "github.com/Laisky/errors/v2" + "github.com/songquanpeng/one-api/common/config" ) func SendEmail(subject string, receiver string, content string) error { if receiver == "" { - return fmt.Errorf("receiver is empty") + return errors.Errorf("receiver is empty") } if config.SMTPFrom == "" { // for compatibility config.SMTPFrom = config.SMTPAccount diff --git a/common/message/main.go b/common/message/main.go index 5ce82a64..068426fd 100644 --- a/common/message/main.go +++ b/common/message/main.go @@ -1,7 +1,7 @@ package message import ( - "fmt" + "github.com/Laisky/errors/v2" "github.com/songquanpeng/one-api/common/config" ) @@ -18,5 +18,5 @@ func Notify(by string, title string, description string, content string) error { if by == ByMessagePusher { return SendMessage(title, description, content) } - return fmt.Errorf("unknown notify method: %s", by) + return errors.Errorf("unknown notify method: %s", by) } diff --git a/controller/channel-billing.go b/controller/channel-billing.go index 3d2781a2..b9a3908e 100644 --- a/controller/channel-billing.go +++ b/controller/channel-billing.go @@ -101,7 +101,7 @@ func GetResponseBody(method, url string, channel *model.Channel, headers http.He return nil, err } if res.StatusCode != http.StatusOK { - return nil, fmt.Errorf("status code: %d", res.StatusCode) + return nil, errors.Errorf("status code: %d", res.StatusCode) } body, err := io.ReadAll(res.Body) if err != nil { @@ -166,7 +166,7 @@ func updateChannelAIProxyBalance(channel *model.Channel) (float64, error) { return 0, err } if !response.Success { - return 0, fmt.Errorf("code: %d, message: %s", response.ErrorCode, response.Message) + return 0, errors.Errorf("code: %d, message: %s", response.ErrorCode, response.Message) } channel.UpdateBalance(response.Data.TotalPoints) return response.Data.TotalPoints, nil diff --git a/controller/channel-test.go b/controller/channel-test.go index 6df0bb7a..e982bc71 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -60,7 +60,7 @@ func testChannel(channel *model.Channel) (err error, openaiErr *relaymodel.Error apiType := constant.ChannelType2APIType(channel.Type) adaptor := helper.GetAdaptor(apiType) if adaptor == nil { - return fmt.Errorf("invalid api type: %d, adaptor is nil", apiType), nil + return errors.Errorf("invalid api type: %d, adaptor is nil", apiType), nil } adaptor.Init(meta) modelName := adaptor.GetModelList()[0] @@ -89,11 +89,11 @@ func testChannel(channel *model.Channel) (err error, openaiErr *relaymodel.Error } if resp.StatusCode != http.StatusOK { err := util.RelayErrorHandler(resp) - return fmt.Errorf("status code %d: %s", resp.StatusCode, err.Error.Message), &err.Error + return errors.Errorf("status code %d: %s", resp.StatusCode, err.Error.Message), &err.Error } usage, respErr := adaptor.DoResponse(c, resp, meta) if respErr != nil { - return fmt.Errorf("%s", respErr.Error.Message), &respErr.Error + return errors.Errorf("%s", respErr.Error.Message), &respErr.Error } if usage == nil { return errors.New("usage is nil"), nil diff --git a/controller/relay.go b/controller/relay.go index 6d071565..85932368 100644 --- a/controller/relay.go +++ b/controller/relay.go @@ -110,6 +110,7 @@ func shouldRetry(c *gin.Context, statusCode int) error { if statusCode/100 == 2 { return errors.Errorf("status code = %d", statusCode) } + return nil } diff --git a/relay/channel/common.go b/relay/channel/common.go index a848a0e3..e03c1055 100644 --- a/relay/channel/common.go +++ b/relay/channel/common.go @@ -1,12 +1,12 @@ package channel import ( - "fmt" + "io" + "net/http" + "github.com/Laisky/errors/v2" "github.com/gin-gonic/gin" "github.com/songquanpeng/one-api/relay/util" - "io" - "net/http" ) func SetupCommonRequestHeader(c *gin.Context, req *http.Request, meta *util.RelayMeta) { @@ -20,19 +20,19 @@ func SetupCommonRequestHeader(c *gin.Context, req *http.Request, meta *util.Rela func DoRequestHelper(a Adaptor, c *gin.Context, meta *util.RelayMeta, requestBody io.Reader) (*http.Response, error) { fullRequestURL, err := a.GetRequestURL(meta) if err != nil { - return nil, fmt.Errorf("get request url failed: %w", err) + return nil, errors.Wrap(err, "get request url failed") } req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody) if err != nil { - return nil, fmt.Errorf("new request failed: %w", err) + return nil, errors.Wrap(err, "new request failed") } err = a.SetupRequestHeader(c, req, meta) if err != nil { - return nil, fmt.Errorf("setup request header failed: %w", err) + return nil, errors.Wrap(err, "setup request header failed") } resp, err := DoRequest(c, req) if err != nil { - return nil, fmt.Errorf("do request failed: %w", err) + return nil, errors.Wrap(err, "do request failed") } return resp, nil } diff --git a/relay/channel/minimax/main.go b/relay/channel/minimax/main.go index a01821c2..a3cd0f14 100644 --- a/relay/channel/minimax/main.go +++ b/relay/channel/minimax/main.go @@ -2,6 +2,8 @@ package minimax import ( "fmt" + + "github.com/Laisky/errors/v2" "github.com/songquanpeng/one-api/relay/constant" "github.com/songquanpeng/one-api/relay/util" ) @@ -10,5 +12,5 @@ func GetRequestURL(meta *util.RelayMeta) (string, error) { if meta.Mode == constant.RelayModeChatCompletions { return fmt.Sprintf("%s/v1/text/chatcompletion_v2", meta.BaseURL), nil } - return "", fmt.Errorf("unsupported relay mode %d for minimax", meta.Mode) + return "", errors.Errorf("unsupported relay mode %d for minimax", meta.Mode) } diff --git a/relay/controller/audio.go b/relay/controller/audio.go index 9d0d122f..beea6184 100644 --- a/relay/controller/audio.go +++ b/relay/controller/audio.go @@ -6,6 +6,10 @@ import ( "context" "encoding/json" "fmt" + "io" + "net/http" + "strings" + "github.com/Laisky/errors/v2" "github.com/gin-gonic/gin" "github.com/songquanpeng/one-api/common" @@ -16,9 +20,6 @@ import ( "github.com/songquanpeng/one-api/relay/constant" relaymodel "github.com/songquanpeng/one-api/relay/model" "github.com/songquanpeng/one-api/relay/util" - "io" - "net/http" - "strings" ) func RelayAudioHelper(c *gin.Context, relayMode int) *relaymodel.ErrorWithStatusCode { @@ -162,7 +163,7 @@ func RelayAudioHelper(c *gin.Context, relayMode int) *relaymodel.ErrorWithStatus var openAIErr openai.SlimTextResponse if err = json.Unmarshal(responseBody, &openAIErr); err == nil { if openAIErr.Error.Message != "" { - return openai.ErrorWrapper(fmt.Errorf("type %s, code %v, message %s", openAIErr.Error.Type, openAIErr.Error.Code, openAIErr.Error.Message), "request_error", http.StatusInternalServerError) + return openai.ErrorWrapper(errors.Errorf("type %s, code %v, message %s", openAIErr.Error.Type, openAIErr.Error.Code, openAIErr.Error.Message), "request_error", http.StatusInternalServerError) } } @@ -230,8 +231,9 @@ func getTextFromVTT(body []byte) (string, error) { func getTextFromVerboseJSON(body []byte) (string, error) { var whisperResponse openai.WhisperVerboseJSONResponse if err := json.Unmarshal(body, &whisperResponse); err != nil { - return "", fmt.Errorf("unmarshal_response_body_failed err :%w", err) + return "", errors.Wrap(err, "unmarshal_response_body_failed") } + return whisperResponse.Text, nil } @@ -263,7 +265,7 @@ func getTextFromText(body []byte) (string, error) { func getTextFromJSON(body []byte) (string, error) { var whisperResponse openai.WhisperJSONResponse if err := json.Unmarshal(body, &whisperResponse); err != nil { - return "", fmt.Errorf("unmarshal_response_body_failed err :%w", err) + return "", errors.Wrap(err, "unmarshal_response_body_failed") } return whisperResponse.Text, nil } diff --git a/relay/controller/helper.go b/relay/controller/helper.go index 1074737d..a121a429 100644 --- a/relay/controller/helper.go +++ b/relay/controller/helper.go @@ -83,7 +83,7 @@ func getImageCostRatio(imageRequest *openai.ImageRequest) (float64, error) { } imageCostRatio, hasValidSize := constant.DalleSizeRatios[imageRequest.Model][imageRequest.Size] if !hasValidSize { - return 0, fmt.Errorf("size not supported for this image model: %s", imageRequest.Size) + return 0, errors.Errorf("size not supported for this image model: %s", imageRequest.Size) } if imageRequest.Quality == "hd" && imageRequest.Model == "dall-e-3" { if imageRequest.Size == "1024x1024" { diff --git a/relay/controller/text.go b/relay/controller/text.go index 12c28c9c..5dc07d23 100644 --- a/relay/controller/text.go +++ b/relay/controller/text.go @@ -4,6 +4,11 @@ import ( "bytes" "encoding/json" "fmt" + "io" + "net/http" + "strings" + + "github.com/Laisky/errors/v2" "github.com/gin-gonic/gin" "github.com/songquanpeng/one-api/common" "github.com/songquanpeng/one-api/common/logger" @@ -12,9 +17,6 @@ import ( "github.com/songquanpeng/one-api/relay/helper" "github.com/songquanpeng/one-api/relay/model" "github.com/songquanpeng/one-api/relay/util" - "io" - "net/http" - "strings" ) func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode { @@ -49,7 +51,7 @@ func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode { adaptor := helper.GetAdaptor(meta.APIType) if adaptor == nil { - return openai.ErrorWrapper(fmt.Errorf("invalid api type: %d", meta.APIType), "invalid_api_type", http.StatusBadRequest) + return openai.ErrorWrapper(errors.Errorf("invalid api type: %d", meta.APIType), "invalid_api_type", http.StatusBadRequest) } // get request body