mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 02:25:00 +00:00
refactor(error): replace dto.OpenAIError with types.OpenAIError for consistency
This commit is contained in:
@@ -2,9 +2,9 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/QuantumNous/new-api/common"
|
"github.com/QuantumNous/new-api/common"
|
||||||
"github.com/QuantumNous/new-api/dto"
|
|
||||||
"github.com/QuantumNous/new-api/model"
|
"github.com/QuantumNous/new-api/model"
|
||||||
"github.com/QuantumNous/new-api/setting/operation_setting"
|
"github.com/QuantumNous/new-api/setting/operation_setting"
|
||||||
|
"github.com/QuantumNous/new-api/types"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ func GetSubscription(c *gin.Context) {
|
|||||||
expiredTime = 0
|
expiredTime = 0
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
openAIError := dto.OpenAIError{
|
openAIError := types.OpenAIError{
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
Type: "upstream_error",
|
Type: "upstream_error",
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ func GetUsage(c *gin.Context) {
|
|||||||
quota, err = model.GetUserUsedQuota(userId)
|
quota, err = model.GetUserUsedQuota(userId)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
openAIError := dto.OpenAIError{
|
openAIError := types.OpenAIError{
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
Type: "new_api_error",
|
Type: "new_api_error",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/QuantumNous/new-api/service"
|
"github.com/QuantumNous/new-api/service"
|
||||||
"github.com/QuantumNous/new-api/setting/operation_setting"
|
"github.com/QuantumNous/new-api/setting/operation_setting"
|
||||||
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
||||||
|
"github.com/QuantumNous/new-api/types"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
@@ -275,7 +276,7 @@ func RetrieveModel(c *gin.Context, modelType int) {
|
|||||||
c.JSON(200, aiModel)
|
c.JSON(200, aiModel)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
openAIError := dto.OpenAIError{
|
openAIError := types.OpenAIError{
|
||||||
Message: fmt.Sprintf("The model '%s' does not exist", modelId),
|
Message: fmt.Sprintf("The model '%s' does not exist", modelId),
|
||||||
Type: "invalid_request_error",
|
Type: "invalid_request_error",
|
||||||
Param: "model",
|
Param: "model",
|
||||||
|
|||||||
63
dto/error.go
63
dto/error.go
@@ -1,26 +1,31 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
import "github.com/QuantumNous/new-api/types"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
type OpenAIError struct {
|
"github.com/QuantumNous/new-api/common"
|
||||||
Message string `json:"message"`
|
"github.com/QuantumNous/new-api/types"
|
||||||
Type string `json:"type"`
|
)
|
||||||
Param string `json:"param"`
|
|
||||||
Code any `json:"code"`
|
//type OpenAIError struct {
|
||||||
}
|
// Message string `json:"message"`
|
||||||
|
// Type string `json:"type"`
|
||||||
|
// Param string `json:"param"`
|
||||||
|
// Code any `json:"code"`
|
||||||
|
//}
|
||||||
|
|
||||||
type OpenAIErrorWithStatusCode struct {
|
type OpenAIErrorWithStatusCode struct {
|
||||||
Error OpenAIError `json:"error"`
|
Error types.OpenAIError `json:"error"`
|
||||||
StatusCode int `json:"status_code"`
|
StatusCode int `json:"status_code"`
|
||||||
LocalError bool
|
LocalError bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type GeneralErrorResponse struct {
|
type GeneralErrorResponse struct {
|
||||||
Error types.OpenAIError `json:"error"`
|
Error json.RawMessage `json:"error"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Msg string `json:"msg"`
|
Msg string `json:"msg"`
|
||||||
Err string `json:"err"`
|
Err string `json:"err"`
|
||||||
ErrorMsg string `json:"error_msg"`
|
ErrorMsg string `json:"error_msg"`
|
||||||
Header struct {
|
Header struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
} `json:"header"`
|
} `json:"header"`
|
||||||
@@ -31,9 +36,35 @@ type GeneralErrorResponse struct {
|
|||||||
} `json:"response"`
|
} `json:"response"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e GeneralErrorResponse) TryToOpenAIError() *types.OpenAIError {
|
||||||
|
var openAIError types.OpenAIError
|
||||||
|
if len(e.Error) > 0 {
|
||||||
|
err := common.Unmarshal(e.Error, &openAIError)
|
||||||
|
if err == nil && openAIError.Message != "" {
|
||||||
|
return &openAIError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (e GeneralErrorResponse) ToMessage() string {
|
func (e GeneralErrorResponse) ToMessage() string {
|
||||||
if e.Error.Message != "" {
|
if len(e.Error) > 0 {
|
||||||
return e.Error.Message
|
switch common.GetJsonType(e.Error) {
|
||||||
|
case "object":
|
||||||
|
var openAIError types.OpenAIError
|
||||||
|
err := common.Unmarshal(e.Error, &openAIError)
|
||||||
|
if err == nil && openAIError.Message != "" {
|
||||||
|
return openAIError.Message
|
||||||
|
}
|
||||||
|
case "string":
|
||||||
|
var msg string
|
||||||
|
err := common.Unmarshal(e.Error, &msg)
|
||||||
|
if err == nil && msg != "" {
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return string(e.Error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if e.Message != "" {
|
if e.Message != "" {
|
||||||
return e.Message
|
return e.Message
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/QuantumNous/new-api/dto"
|
"github.com/QuantumNous/new-api/dto"
|
||||||
|
"github.com/QuantumNous/new-api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// type ZhipuMessage struct {
|
// type ZhipuMessage struct {
|
||||||
@@ -37,7 +38,7 @@ type ZhipuV4Response struct {
|
|||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
TextResponseChoices []dto.OpenAITextResponseChoice `json:"choices"`
|
TextResponseChoices []dto.OpenAITextResponseChoice `json:"choices"`
|
||||||
Usage dto.Usage `json:"usage"`
|
Usage dto.Usage `json:"usage"`
|
||||||
Error dto.OpenAIError `json:"error"`
|
Error types.OpenAIError `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -96,19 +96,21 @@ func RelayErrorHandler(ctx context.Context, resp *http.Response, showBodyWhenFai
|
|||||||
if showBodyWhenFail {
|
if showBodyWhenFail {
|
||||||
newApiErr.Err = fmt.Errorf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody))
|
newApiErr.Err = fmt.Errorf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody))
|
||||||
} else {
|
} else {
|
||||||
if common.DebugEnabled {
|
logger.LogError(ctx, fmt.Sprintf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody)))
|
||||||
logger.LogInfo(ctx, fmt.Sprintf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody)))
|
|
||||||
}
|
|
||||||
newApiErr.Err = fmt.Errorf("bad response status code %d", resp.StatusCode)
|
newApiErr.Err = fmt.Errorf("bad response status code %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if errResponse.Error.Message != "" {
|
|
||||||
|
if common.GetJsonType(errResponse.Error) == "object" {
|
||||||
// General format error (OpenAI, Anthropic, Gemini, etc.)
|
// General format error (OpenAI, Anthropic, Gemini, etc.)
|
||||||
newApiErr = types.WithOpenAIError(errResponse.Error, resp.StatusCode)
|
oaiError := errResponse.TryToOpenAIError()
|
||||||
} else {
|
if oaiError != nil {
|
||||||
newApiErr = types.NewOpenAIError(errors.New(errResponse.ToMessage()), types.ErrorCodeBadResponseStatusCode, resp.StatusCode)
|
newApiErr = types.WithOpenAIError(*oaiError, resp.StatusCode)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
newApiErr = types.NewOpenAIError(errors.New(errResponse.ToMessage()), types.ErrorCodeBadResponseStatusCode, resp.StatusCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user