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
a1299114
authored
Dec 13, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(error): replace dto.OpenAIError with types.OpenAIError for consistency
parent
6175f254
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
58 additions
and
23 deletions
+58
-23
controller/billing.go
+3
-3
controller/model.go
+2
-1
dto/error.go
+42
-11
relay/channel/zhipu_4v/dto.go
+2
-1
service/error.go
+9
-7
No files found.
controller/billing.go
View file @
a1299114
...
@@ -2,9 +2,9 @@ package controller
...
@@ -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) {
...
@@ -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) {
...
@@ -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"
,
}
}
...
...
controller/model.go
View file @
a1299114
...
@@ -18,6 +18,7 @@ import (
...
@@ -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) {
...
@@ -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"
,
...
...
dto/error.go
View file @
a1299114
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"`
...
@@ -31,9 +36,35 @@ type GeneralErrorResponse struct {
...
@@ -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
...
...
relay/channel/zhipu_4v/dto.go
View file @
a1299114
...
@@ -4,6 +4,7 @@ import (
...
@@ -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 {
...
@@ -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"`
}
}
//
//
...
...
service/error.go
View file @
a1299114
...
@@ -96,19 +96,21 @@ func RelayErrorHandler(ctx context.Context, resp *http.Response, showBodyWhenFai
...
@@ -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
}
}
...
...
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