refactor: Introduce standardized API error
This commit refactors the application's error handling mechanism by introducing a new standardized error type, `types.NewAPIError`. It also renames common JSON utility functions for better clarity. Previously, internal error handling was tightly coupled to the `dto.OpenAIError` format. This change decouples the internal logic from the external API representation. Key changes: - A new `types.NewAPIError` struct is introduced to serve as a canonical internal representation for all API errors. - All relay adapters (OpenAI, Claude, Gemini, etc.) are updated to return `*types.NewAPIError`. - Controllers now convert the internal `NewAPIError` to the client-facing `OpenAIError` format at the API boundary, ensuring backward compatibility. - Channel auto-disable/enable logic is updated to use the new standardized error type. - JSON utility functions are renamed to align with Go's standard library conventions (e.g., `UnmarshalJson` -> `Unmarshal`, `EncodeJson` -> `Marshal`).
This commit is contained in:
@@ -2,11 +2,13 @@ package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/dto"
|
||||
"one-api/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -25,32 +27,32 @@ func MidjourneyErrorWithStatusCodeWrapper(code int, desc string, statusCode int)
|
||||
}
|
||||
}
|
||||
|
||||
// OpenAIErrorWrapper wraps an error into an OpenAIErrorWithStatusCode
|
||||
func OpenAIErrorWrapper(err error, code string, statusCode int) *dto.OpenAIErrorWithStatusCode {
|
||||
text := err.Error()
|
||||
lowerText := strings.ToLower(text)
|
||||
if !strings.HasPrefix(lowerText, "get file base64 from url") && !strings.HasPrefix(lowerText, "mime type is not supported") {
|
||||
if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
|
||||
common.SysLog(fmt.Sprintf("error: %s", text))
|
||||
text = "请求上游地址失败"
|
||||
}
|
||||
}
|
||||
openAIError := dto.OpenAIError{
|
||||
Message: text,
|
||||
Type: "new_api_error",
|
||||
Code: code,
|
||||
}
|
||||
return &dto.OpenAIErrorWithStatusCode{
|
||||
Error: openAIError,
|
||||
StatusCode: statusCode,
|
||||
}
|
||||
}
|
||||
|
||||
func OpenAIErrorWrapperLocal(err error, code string, statusCode int) *dto.OpenAIErrorWithStatusCode {
|
||||
openaiErr := OpenAIErrorWrapper(err, code, statusCode)
|
||||
openaiErr.LocalError = true
|
||||
return openaiErr
|
||||
}
|
||||
//// OpenAIErrorWrapper wraps an error into an OpenAIErrorWithStatusCode
|
||||
//func OpenAIErrorWrapper(err error, code string, statusCode int) *dto.OpenAIErrorWithStatusCode {
|
||||
// text := err.Error()
|
||||
// lowerText := strings.ToLower(text)
|
||||
// if !strings.HasPrefix(lowerText, "get file base64 from url") && !strings.HasPrefix(lowerText, "mime type is not supported") {
|
||||
// if strings.Contains(lowerText, "post") || strings.Contains(lowerText, "dial") || strings.Contains(lowerText, "http") {
|
||||
// common.SysLog(fmt.Sprintf("error: %s", text))
|
||||
// text = "请求上游地址失败"
|
||||
// }
|
||||
// }
|
||||
// openAIError := dto.OpenAIError{
|
||||
// Message: text,
|
||||
// Type: "new_api_error",
|
||||
// Code: code,
|
||||
// }
|
||||
// return &dto.OpenAIErrorWithStatusCode{
|
||||
// Error: openAIError,
|
||||
// StatusCode: statusCode,
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//func OpenAIErrorWrapperLocal(err error, code string, statusCode int) *dto.OpenAIErrorWithStatusCode {
|
||||
// openaiErr := OpenAIErrorWrapper(err, code, statusCode)
|
||||
// openaiErr.LocalError = true
|
||||
// return openaiErr
|
||||
//}
|
||||
|
||||
func ClaudeErrorWrapper(err error, code string, statusCode int) *dto.ClaudeErrorWithStatusCode {
|
||||
text := err.Error()
|
||||
@@ -77,43 +79,37 @@ func ClaudeErrorWrapperLocal(err error, code string, statusCode int) *dto.Claude
|
||||
return claudeErr
|
||||
}
|
||||
|
||||
func RelayErrorHandler(resp *http.Response, showBodyWhenFail bool) (errWithStatusCode *dto.OpenAIErrorWithStatusCode) {
|
||||
errWithStatusCode = &dto.OpenAIErrorWithStatusCode{
|
||||
func RelayErrorHandler(resp *http.Response, showBodyWhenFail bool) (newApiErr *types.NewAPIError) {
|
||||
newApiErr = &types.NewAPIError{
|
||||
StatusCode: resp.StatusCode,
|
||||
Error: dto.OpenAIError{
|
||||
Type: "upstream_error",
|
||||
Code: "bad_response_status_code",
|
||||
Param: strconv.Itoa(resp.StatusCode),
|
||||
},
|
||||
}
|
||||
|
||||
responseBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
common.CloseResponseBodyGracefully(resp)
|
||||
var errResponse dto.GeneralErrorResponse
|
||||
err = json.Unmarshal(responseBody, &errResponse)
|
||||
|
||||
err = common.Unmarshal(responseBody, &errResponse)
|
||||
if err != nil {
|
||||
if showBodyWhenFail {
|
||||
errWithStatusCode.Error.Message = string(responseBody)
|
||||
newApiErr.Err = fmt.Errorf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody))
|
||||
} else {
|
||||
errWithStatusCode.Error.Message = fmt.Sprintf("bad response status code %d", resp.StatusCode)
|
||||
newApiErr.Err = fmt.Errorf("bad response status code %d", resp.StatusCode)
|
||||
}
|
||||
return
|
||||
}
|
||||
if errResponse.Error.Message != "" {
|
||||
// OpenAI format error, so we override the default one
|
||||
errWithStatusCode.Error = errResponse.Error
|
||||
// General format error (OpenAI, Anthropic, Gemini, etc.)
|
||||
newApiErr = types.WithOpenAIError(errResponse.Error, resp.StatusCode)
|
||||
} else {
|
||||
errWithStatusCode.Error.Message = errResponse.ToMessage()
|
||||
}
|
||||
if errWithStatusCode.Error.Message == "" {
|
||||
errWithStatusCode.Error.Message = fmt.Sprintf("bad response status code %d", resp.StatusCode)
|
||||
newApiErr = types.NewErrorWithStatusCode(errors.New(errResponse.ToMessage()), types.ErrorCodeBadResponseStatusCode, resp.StatusCode)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ResetStatusCode(openaiErr *dto.OpenAIErrorWithStatusCode, statusCodeMappingStr string) {
|
||||
func ResetStatusCode(newApiErr *types.NewAPIError, statusCodeMappingStr string) {
|
||||
if statusCodeMappingStr == "" || statusCodeMappingStr == "{}" {
|
||||
return
|
||||
}
|
||||
@@ -122,13 +118,13 @@ func ResetStatusCode(openaiErr *dto.OpenAIErrorWithStatusCode, statusCodeMapping
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if openaiErr.StatusCode == http.StatusOK {
|
||||
if newApiErr.StatusCode == http.StatusOK {
|
||||
return
|
||||
}
|
||||
codeStr := strconv.Itoa(openaiErr.StatusCode)
|
||||
codeStr := strconv.Itoa(newApiErr.StatusCode)
|
||||
if _, ok := statusCodeMapping[codeStr]; ok {
|
||||
intCode, _ := strconv.Atoi(statusCodeMapping[codeStr])
|
||||
openaiErr.StatusCode = intCode
|
||||
newApiErr.StatusCode = intCode
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user