fix: Upgrade error handling to errors/v2 package and refactor error messages.

- Refactor error handling using `errors/v2` package for improved error management and formatting
- Implement changes in relay/controller/text.go, controller/channel-test.go, controller/relay.go, common/message/email.go, relay/controller/audio.go, controller/channel-billing.go, relay/controller/helper.go, relay/channel/common.go, relay/channel/minimax/main.go, and common/message/main.go
- Add package imports and update package paths for better code organization
- Replace hardcoded strings with error variables for consistency and readability
- Implement secure methods for generating unique Message-ID
- Improve SMTP handling and authentication in email.go
- Update response handling and error messages for better user experience
- Add constant checks and modify functions based on imageRequest parameters in helper.go
- Use `http.NewRequest` instead of `fmt.Fprint` for improved request handling in relay/channel/common.go
- Update error handling and formatting for better consistency across all files
This commit is contained in:
Laisky.Cai
2024-03-12 06:43:54 +00:00
parent 54203e3d30
commit d953da1ff2
10 changed files with 37 additions and 28 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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
}