mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 02:25:00 +00:00
fix: support numeric status code mapping in ResetStatusCode
This commit is contained in:
@@ -2,9 +2,11 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -127,10 +129,13 @@ func RelayErrorHandler(ctx context.Context, resp *http.Response, showBodyWhenFai
|
||||
}
|
||||
|
||||
func ResetStatusCode(newApiErr *types.NewAPIError, statusCodeMappingStr string) {
|
||||
if newApiErr == nil {
|
||||
return
|
||||
}
|
||||
if statusCodeMappingStr == "" || statusCodeMappingStr == "{}" {
|
||||
return
|
||||
}
|
||||
statusCodeMapping := make(map[string]string)
|
||||
statusCodeMapping := make(map[string]any)
|
||||
err := common.Unmarshal([]byte(statusCodeMappingStr), &statusCodeMapping)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -139,12 +144,44 @@ func ResetStatusCode(newApiErr *types.NewAPIError, statusCodeMappingStr string)
|
||||
return
|
||||
}
|
||||
codeStr := strconv.Itoa(newApiErr.StatusCode)
|
||||
if _, ok := statusCodeMapping[codeStr]; ok {
|
||||
intCode, _ := strconv.Atoi(statusCodeMapping[codeStr])
|
||||
if value, ok := statusCodeMapping[codeStr]; ok {
|
||||
intCode, ok := parseStatusCodeMappingValue(value)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
newApiErr.StatusCode = intCode
|
||||
}
|
||||
}
|
||||
|
||||
func parseStatusCodeMappingValue(value any) (int, bool) {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
if v == "" {
|
||||
return 0, false
|
||||
}
|
||||
statusCode, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return statusCode, true
|
||||
case float64:
|
||||
if v != math.Trunc(v) {
|
||||
return 0, false
|
||||
}
|
||||
return int(v), true
|
||||
case int:
|
||||
return v, true
|
||||
case json.Number:
|
||||
statusCode, err := strconv.Atoi(v.String())
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return statusCode, true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func TaskErrorWrapperLocal(err error, code string, statusCode int) *dto.TaskError {
|
||||
openaiErr := TaskErrorWrapper(err, code, statusCode)
|
||||
openaiErr.LocalError = true
|
||||
|
||||
Reference in New Issue
Block a user