mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-22 11:26:37 +08:00
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:
parent
54203e3d30
commit
d953da1ff2
@ -5,15 +5,17 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/songquanpeng/one-api/common/config"
|
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Laisky/errors/v2"
|
||||||
|
"github.com/songquanpeng/one-api/common/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SendEmail(subject string, receiver string, content string) error {
|
func SendEmail(subject string, receiver string, content string) error {
|
||||||
if receiver == "" {
|
if receiver == "" {
|
||||||
return fmt.Errorf("receiver is empty")
|
return errors.Errorf("receiver is empty")
|
||||||
}
|
}
|
||||||
if config.SMTPFrom == "" { // for compatibility
|
if config.SMTPFrom == "" { // for compatibility
|
||||||
config.SMTPFrom = config.SMTPAccount
|
config.SMTPFrom = config.SMTPAccount
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package message
|
package message
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"github.com/Laisky/errors/v2"
|
||||||
"github.com/songquanpeng/one-api/common/config"
|
"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 {
|
if by == ByMessagePusher {
|
||||||
return SendMessage(title, description, content)
|
return SendMessage(title, description, content)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("unknown notify method: %s", by)
|
return errors.Errorf("unknown notify method: %s", by)
|
||||||
}
|
}
|
||||||
|
@ -101,7 +101,7 @@ func GetResponseBody(method, url string, channel *model.Channel, headers http.He
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if res.StatusCode != http.StatusOK {
|
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)
|
body, err := io.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -166,7 +166,7 @@ func updateChannelAIProxyBalance(channel *model.Channel) (float64, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
if !response.Success {
|
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)
|
channel.UpdateBalance(response.Data.TotalPoints)
|
||||||
return response.Data.TotalPoints, nil
|
return response.Data.TotalPoints, nil
|
||||||
|
@ -60,7 +60,7 @@ func testChannel(channel *model.Channel) (err error, openaiErr *relaymodel.Error
|
|||||||
apiType := constant.ChannelType2APIType(channel.Type)
|
apiType := constant.ChannelType2APIType(channel.Type)
|
||||||
adaptor := helper.GetAdaptor(apiType)
|
adaptor := helper.GetAdaptor(apiType)
|
||||||
if adaptor == nil {
|
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)
|
adaptor.Init(meta)
|
||||||
modelName := adaptor.GetModelList()[0]
|
modelName := adaptor.GetModelList()[0]
|
||||||
@ -89,11 +89,11 @@ func testChannel(channel *model.Channel) (err error, openaiErr *relaymodel.Error
|
|||||||
}
|
}
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
err := util.RelayErrorHandler(resp)
|
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)
|
usage, respErr := adaptor.DoResponse(c, resp, meta)
|
||||||
if respErr != nil {
|
if respErr != nil {
|
||||||
return fmt.Errorf("%s", respErr.Error.Message), &respErr.Error
|
return errors.Errorf("%s", respErr.Error.Message), &respErr.Error
|
||||||
}
|
}
|
||||||
if usage == nil {
|
if usage == nil {
|
||||||
return errors.New("usage is nil"), nil
|
return errors.New("usage is nil"), nil
|
||||||
|
@ -110,6 +110,7 @@ func shouldRetry(c *gin.Context, statusCode int) error {
|
|||||||
if statusCode/100 == 2 {
|
if statusCode/100 == 2 {
|
||||||
return errors.Errorf("status code = %d", statusCode)
|
return errors.Errorf("status code = %d", statusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package channel
|
package channel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/Laisky/errors/v2"
|
"github.com/Laisky/errors/v2"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/songquanpeng/one-api/relay/util"
|
"github.com/songquanpeng/one-api/relay/util"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupCommonRequestHeader(c *gin.Context, req *http.Request, meta *util.RelayMeta) {
|
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) {
|
func DoRequestHelper(a Adaptor, c *gin.Context, meta *util.RelayMeta, requestBody io.Reader) (*http.Response, error) {
|
||||||
fullRequestURL, err := a.GetRequestURL(meta)
|
fullRequestURL, err := a.GetRequestURL(meta)
|
||||||
if err != nil {
|
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)
|
req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody)
|
||||||
if err != nil {
|
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)
|
err = a.SetupRequestHeader(c, req, meta)
|
||||||
if err != nil {
|
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)
|
resp, err := DoRequest(c, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("do request failed: %w", err)
|
return nil, errors.Wrap(err, "do request failed")
|
||||||
}
|
}
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package minimax
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Laisky/errors/v2"
|
||||||
"github.com/songquanpeng/one-api/relay/constant"
|
"github.com/songquanpeng/one-api/relay/constant"
|
||||||
"github.com/songquanpeng/one-api/relay/util"
|
"github.com/songquanpeng/one-api/relay/util"
|
||||||
)
|
)
|
||||||
@ -10,5 +12,5 @@ func GetRequestURL(meta *util.RelayMeta) (string, error) {
|
|||||||
if meta.Mode == constant.RelayModeChatCompletions {
|
if meta.Mode == constant.RelayModeChatCompletions {
|
||||||
return fmt.Sprintf("%s/v1/text/chatcompletion_v2", meta.BaseURL), nil
|
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)
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/Laisky/errors/v2"
|
"github.com/Laisky/errors/v2"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/songquanpeng/one-api/common"
|
"github.com/songquanpeng/one-api/common"
|
||||||
@ -16,9 +20,6 @@ import (
|
|||||||
"github.com/songquanpeng/one-api/relay/constant"
|
"github.com/songquanpeng/one-api/relay/constant"
|
||||||
relaymodel "github.com/songquanpeng/one-api/relay/model"
|
relaymodel "github.com/songquanpeng/one-api/relay/model"
|
||||||
"github.com/songquanpeng/one-api/relay/util"
|
"github.com/songquanpeng/one-api/relay/util"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func RelayAudioHelper(c *gin.Context, relayMode int) *relaymodel.ErrorWithStatusCode {
|
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
|
var openAIErr openai.SlimTextResponse
|
||||||
if err = json.Unmarshal(responseBody, &openAIErr); err == nil {
|
if err = json.Unmarshal(responseBody, &openAIErr); err == nil {
|
||||||
if openAIErr.Error.Message != "" {
|
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) {
|
func getTextFromVerboseJSON(body []byte) (string, error) {
|
||||||
var whisperResponse openai.WhisperVerboseJSONResponse
|
var whisperResponse openai.WhisperVerboseJSONResponse
|
||||||
if err := json.Unmarshal(body, &whisperResponse); err != nil {
|
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
|
return whisperResponse.Text, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,7 +265,7 @@ func getTextFromText(body []byte) (string, error) {
|
|||||||
func getTextFromJSON(body []byte) (string, error) {
|
func getTextFromJSON(body []byte) (string, error) {
|
||||||
var whisperResponse openai.WhisperJSONResponse
|
var whisperResponse openai.WhisperJSONResponse
|
||||||
if err := json.Unmarshal(body, &whisperResponse); err != nil {
|
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
|
return whisperResponse.Text, nil
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ func getImageCostRatio(imageRequest *openai.ImageRequest) (float64, error) {
|
|||||||
}
|
}
|
||||||
imageCostRatio, hasValidSize := constant.DalleSizeRatios[imageRequest.Model][imageRequest.Size]
|
imageCostRatio, hasValidSize := constant.DalleSizeRatios[imageRequest.Model][imageRequest.Size]
|
||||||
if !hasValidSize {
|
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.Quality == "hd" && imageRequest.Model == "dall-e-3" {
|
||||||
if imageRequest.Size == "1024x1024" {
|
if imageRequest.Size == "1024x1024" {
|
||||||
|
@ -4,6 +4,11 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Laisky/errors/v2"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/songquanpeng/one-api/common"
|
"github.com/songquanpeng/one-api/common"
|
||||||
"github.com/songquanpeng/one-api/common/logger"
|
"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/helper"
|
||||||
"github.com/songquanpeng/one-api/relay/model"
|
"github.com/songquanpeng/one-api/relay/model"
|
||||||
"github.com/songquanpeng/one-api/relay/util"
|
"github.com/songquanpeng/one-api/relay/util"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode {
|
func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode {
|
||||||
@ -49,7 +51,7 @@ func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode {
|
|||||||
|
|
||||||
adaptor := helper.GetAdaptor(meta.APIType)
|
adaptor := helper.GetAdaptor(meta.APIType)
|
||||||
if adaptor == nil {
|
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
|
// get request body
|
||||||
|
Loading…
Reference in New Issue
Block a user