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
013b30c2
authored
Aug 15, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: enhance error handling and masking for model not found scenarios
parent
f6f9e73f
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
49 additions
and
5 deletions
+49
-5
common/str.go
+29
-1
middleware/distributor.go
+2
-2
middleware/utils.go
+6
-1
types/error.go
+12
-1
No files found.
common/str.go
View file @
013b30c2
...
@@ -99,12 +99,15 @@ func GetJsonString(data any) string {
...
@@ -99,12 +99,15 @@ func GetJsonString(data any) string {
return
string
(
b
)
return
string
(
b
)
}
}
// MaskSensitiveInfo masks sensitive information like URLs, IPs in a string
// MaskSensitiveInfo masks sensitive information like URLs, IPs
, and domain names
in a string
// Example:
// Example:
// http://example.com -> http://***.com
// http://example.com -> http://***.com
// https://api.test.org/v1/users/123?key=secret -> https://***.org/***/***/?key=***
// https://api.test.org/v1/users/123?key=secret -> https://***.org/***/***/?key=***
// https://sub.domain.co.uk/path/to/resource -> https://***.co.uk/***/***
// https://sub.domain.co.uk/path/to/resource -> https://***.co.uk/***/***
// 192.168.1.1 -> ***.***.***.***
// 192.168.1.1 -> ***.***.***.***
// openai.com -> ***.com
// www.openai.com -> ***.***.com
// api.openai.com -> ***.***.com
func
MaskSensitiveInfo
(
str
string
)
string
{
func
MaskSensitiveInfo
(
str
string
)
string
{
// Mask URLs
// Mask URLs
urlPattern
:=
regexp
.
MustCompile
(
`(http|https)://[^\s/$.?#].[^\s]*`
)
urlPattern
:=
regexp
.
MustCompile
(
`(http|https)://[^\s/$.?#].[^\s]*`
)
...
@@ -184,6 +187,31 @@ func MaskSensitiveInfo(str string) string {
...
@@ -184,6 +187,31 @@ func MaskSensitiveInfo(str string) string {
return
result
return
result
})
})
// Mask domain names without protocol (like openai.com, www.openai.com)
domainPattern
:=
regexp
.
MustCompile
(
`\b(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}\b`
)
str
=
domainPattern
.
ReplaceAllStringFunc
(
str
,
func
(
domain
string
)
string
{
// Skip if it's already been processed as part of a URL
if
strings
.
Contains
(
str
,
"://"
+
domain
)
{
return
domain
}
parts
:=
strings
.
Split
(
domain
,
"."
)
if
len
(
parts
)
<
2
{
return
domain
}
// Handle different domain patterns
if
len
(
parts
)
==
2
{
// openai.com -> ***.com
return
"***."
+
parts
[
1
]
}
else
{
// www.openai.com -> ***.***.com
// api.openai.com -> ***.***.com
lastPart
:=
parts
[
len
(
parts
)
-
1
]
return
"***.***."
+
lastPart
}
})
// Mask IP addresses
// Mask IP addresses
ipPattern
:=
regexp
.
MustCompile
(
`\b(?:\d{1,3}\.){3}\d{1,3}\b`
)
ipPattern
:=
regexp
.
MustCompile
(
`\b(?:\d{1,3}\.){3}\d{1,3}\b`
)
str
=
ipPattern
.
ReplaceAllString
(
str
,
"***.***.***.***"
)
str
=
ipPattern
.
ReplaceAllString
(
str
,
"***.***.***.***"
)
...
...
middleware/distributor.go
View file @
013b30c2
...
@@ -107,11 +107,11 @@ func Distribute() func(c *gin.Context) {
...
@@ -107,11 +107,11 @@ func Distribute() func(c *gin.Context) {
// common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
// common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
// message = "数据库一致性已被破坏,请联系管理员"
// message = "数据库一致性已被破坏,请联系管理员"
//}
//}
abortWithOpenAiMessage
(
c
,
http
.
StatusServiceUnavailable
,
message
)
abortWithOpenAiMessage
(
c
,
http
.
StatusServiceUnavailable
,
message
,
string
(
types
.
ErrorCodeModelNotFound
)
)
return
return
}
}
if
channel
==
nil
{
if
channel
==
nil
{
abortWithOpenAiMessage
(
c
,
http
.
StatusServiceUnavailable
,
fmt
.
Sprintf
(
"分组 %s 下模型 %s 无可用渠道(distributor)"
,
userGroup
,
modelRequest
.
Model
))
abortWithOpenAiMessage
(
c
,
http
.
StatusServiceUnavailable
,
fmt
.
Sprintf
(
"分组 %s 下模型 %s 无可用渠道(distributor)"
,
userGroup
,
modelRequest
.
Model
)
,
string
(
types
.
ErrorCodeModelNotFound
)
)
return
return
}
}
}
}
...
...
middleware/utils.go
View file @
013b30c2
...
@@ -7,12 +7,17 @@ import (
...
@@ -7,12 +7,17 @@ import (
"one-api/logger"
"one-api/logger"
)
)
func
abortWithOpenAiMessage
(
c
*
gin
.
Context
,
statusCode
int
,
message
string
)
{
func
abortWithOpenAiMessage
(
c
*
gin
.
Context
,
statusCode
int
,
message
string
,
code
...
string
)
{
codeStr
:=
""
if
len
(
code
)
>
0
{
codeStr
=
code
[
0
]
}
userId
:=
c
.
GetInt
(
"id"
)
userId
:=
c
.
GetInt
(
"id"
)
c
.
JSON
(
statusCode
,
gin
.
H
{
c
.
JSON
(
statusCode
,
gin
.
H
{
"error"
:
gin
.
H
{
"error"
:
gin
.
H
{
"message"
:
common
.
MessageWithRequestId
(
message
,
c
.
GetString
(
common
.
RequestIdKey
)),
"message"
:
common
.
MessageWithRequestId
(
message
,
c
.
GetString
(
common
.
RequestIdKey
)),
"type"
:
"new_api_error"
,
"type"
:
"new_api_error"
,
"code"
:
codeStr
,
},
},
})
})
c
.
Abort
()
c
.
Abort
()
...
...
types/error.go
View file @
013b30c2
...
@@ -67,6 +67,7 @@ const (
...
@@ -67,6 +67,7 @@ const (
ErrorCodeBadResponseBody
ErrorCode
=
"bad_response_body"
ErrorCodeBadResponseBody
ErrorCode
=
"bad_response_body"
ErrorCodeEmptyResponse
ErrorCode
=
"empty_response"
ErrorCodeEmptyResponse
ErrorCode
=
"empty_response"
ErrorCodeAwsInvokeError
ErrorCode
=
"aws_invoke_error"
ErrorCodeAwsInvokeError
ErrorCode
=
"aws_invoke_error"
ErrorCodeModelNotFound
ErrorCode
=
"model_not_found"
// sql error
// sql error
ErrorCodeQueryDataError
ErrorCode
=
"query_data_error"
ErrorCodeQueryDataError
ErrorCode
=
"query_data_error"
...
@@ -119,7 +120,17 @@ func (e *NewAPIError) MaskSensitiveError() string {
...
@@ -119,7 +120,17 @@ func (e *NewAPIError) MaskSensitiveError() string {
if
e
.
Err
==
nil
{
if
e
.
Err
==
nil
{
return
string
(
e
.
errorCode
)
return
string
(
e
.
errorCode
)
}
}
return
common
.
MaskSensitiveInfo
(
e
.
Err
.
Error
())
errStr
:=
e
.
Err
.
Error
()
if
e
.
StatusCode
==
http
.
StatusServiceUnavailable
{
if
e
.
errorCode
==
ErrorCodeModelNotFound
{
errStr
=
"上游分组模型服务不可用,请稍后再试"
}
else
{
if
strings
.
Contains
(
errStr
,
"分组"
)
||
strings
.
Contains
(
errStr
,
"渠道"
)
{
errStr
=
"上游分组模型服务不可用,请稍后再试"
}
}
}
return
common
.
MaskSensitiveInfo
(
errStr
)
}
}
func
(
e
*
NewAPIError
)
SetMessage
(
message
string
)
{
func
(
e
*
NewAPIError
)
SetMessage
(
message
string
)
{
...
...
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