Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
41df3162
authored
Feb 12, 2026
by
Seefs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: support numeric status code mapping in ResetStatusCode
parent
4fdd12ac
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
3 deletions
+97
-3
service/error.go
+40
-3
service/error_test.go
+57
-0
No files found.
service/error.go
View file @
41df3162
...
@@ -2,9 +2,11 @@ package service
...
@@ -2,9 +2,11 @@ package service
import
(
import
(
"context"
"context"
"encoding/json"
"errors"
"errors"
"fmt"
"fmt"
"io"
"io"
"math"
"net/http"
"net/http"
"strconv"
"strconv"
"strings"
"strings"
...
@@ -127,10 +129,13 @@ func RelayErrorHandler(ctx context.Context, resp *http.Response, showBodyWhenFai
...
@@ -127,10 +129,13 @@ func RelayErrorHandler(ctx context.Context, resp *http.Response, showBodyWhenFai
}
}
func
ResetStatusCode
(
newApiErr
*
types
.
NewAPIError
,
statusCodeMappingStr
string
)
{
func
ResetStatusCode
(
newApiErr
*
types
.
NewAPIError
,
statusCodeMappingStr
string
)
{
if
newApiErr
==
nil
{
return
}
if
statusCodeMappingStr
==
""
||
statusCodeMappingStr
==
"{}"
{
if
statusCodeMappingStr
==
""
||
statusCodeMappingStr
==
"{}"
{
return
return
}
}
statusCodeMapping
:=
make
(
map
[
string
]
string
)
statusCodeMapping
:=
make
(
map
[
string
]
any
)
err
:=
common
.
Unmarshal
([]
byte
(
statusCodeMappingStr
),
&
statusCodeMapping
)
err
:=
common
.
Unmarshal
([]
byte
(
statusCodeMappingStr
),
&
statusCodeMapping
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
...
@@ -139,12 +144,44 @@ func ResetStatusCode(newApiErr *types.NewAPIError, statusCodeMappingStr string)
...
@@ -139,12 +144,44 @@ func ResetStatusCode(newApiErr *types.NewAPIError, statusCodeMappingStr string)
return
return
}
}
codeStr
:=
strconv
.
Itoa
(
newApiErr
.
StatusCode
)
codeStr
:=
strconv
.
Itoa
(
newApiErr
.
StatusCode
)
if
_
,
ok
:=
statusCodeMapping
[
codeStr
];
ok
{
if
value
,
ok
:=
statusCodeMapping
[
codeStr
];
ok
{
intCode
,
_
:=
strconv
.
Atoi
(
statusCodeMapping
[
codeStr
])
intCode
,
ok
:=
parseStatusCodeMappingValue
(
value
)
if
!
ok
{
return
}
newApiErr
.
StatusCode
=
intCode
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
{
func
TaskErrorWrapperLocal
(
err
error
,
code
string
,
statusCode
int
)
*
dto
.
TaskError
{
openaiErr
:=
TaskErrorWrapper
(
err
,
code
,
statusCode
)
openaiErr
:=
TaskErrorWrapper
(
err
,
code
,
statusCode
)
openaiErr
.
LocalError
=
true
openaiErr
.
LocalError
=
true
...
...
service/error_test.go
0 → 100644
View file @
41df3162
package
service
import
(
"testing"
"github.com/QuantumNous/new-api/types"
"github.com/stretchr/testify/require"
)
func
TestResetStatusCode
(
t
*
testing
.
T
)
{
t
.
Parallel
()
testCases
:=
[]
struct
{
name
string
statusCode
int
statusCodeConfig
string
expectedCode
int
}{
{
name
:
"map string value"
,
statusCode
:
429
,
statusCodeConfig
:
`{"429":"503"}`
,
expectedCode
:
503
,
},
{
name
:
"map int value"
,
statusCode
:
429
,
statusCodeConfig
:
`{"429":503}`
,
expectedCode
:
503
,
},
{
name
:
"skip invalid string value"
,
statusCode
:
429
,
statusCodeConfig
:
`{"429":"bad-code"}`
,
expectedCode
:
429
,
},
{
name
:
"skip status code 200"
,
statusCode
:
200
,
statusCodeConfig
:
`{"200":503}`
,
expectedCode
:
200
,
},
}
for
_
,
tc
:=
range
testCases
{
tc
:=
tc
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Parallel
()
newAPIError
:=
&
types
.
NewAPIError
{
StatusCode
:
tc
.
statusCode
,
}
ResetStatusCode
(
newAPIError
,
tc
.
statusCodeConfig
)
require
.
Equal
(
t
,
tc
.
expectedCode
,
newAPIError
.
StatusCode
)
})
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment