mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 20:37:39 +00:00
feat: oai账号增加402适配
This commit is contained in:
@@ -416,8 +416,12 @@ const handleResponses = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
} else if (upstream.status === 401) {
|
} else if (upstream.status === 401 || upstream.status === 402) {
|
||||||
logger.warn(`🔐 Unauthorized error detected for OpenAI account ${accountId} (Codex API)`)
|
const unauthorizedStatus = upstream.status
|
||||||
|
const statusDescription = unauthorizedStatus === 401 ? 'Unauthorized' : 'Payment required'
|
||||||
|
logger.warn(
|
||||||
|
`🔐 ${statusDescription} error detected for OpenAI account ${accountId} (Codex API)`
|
||||||
|
)
|
||||||
|
|
||||||
let errorData = null
|
let errorData = null
|
||||||
|
|
||||||
@@ -435,18 +439,20 @@ const handleResponses = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
errorData = JSON.parse(fullResponse)
|
errorData = JSON.parse(fullResponse)
|
||||||
} catch (parseError) {
|
} catch (parseError) {
|
||||||
logger.error('Failed to parse 401 error response:', parseError)
|
logger.error(`Failed to parse ${unauthorizedStatus} error response:`, parseError)
|
||||||
logger.debug('Raw 401 response:', fullResponse)
|
logger.debug(`Raw ${unauthorizedStatus} response:`, fullResponse)
|
||||||
errorData = { error: { message: fullResponse || 'Unauthorized' } }
|
errorData = { error: { message: fullResponse || 'Unauthorized' } }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
errorData = upstream.data
|
errorData = upstream.data
|
||||||
}
|
}
|
||||||
} catch (parseError) {
|
} catch (parseError) {
|
||||||
logger.error('⚠️ Failed to handle 401 error response:', parseError)
|
logger.error(`⚠️ Failed to handle ${unauthorizedStatus} error response:`, parseError)
|
||||||
}
|
}
|
||||||
|
|
||||||
let reason = 'OpenAI账号认证失败(401错误)'
|
const statusLabel = unauthorizedStatus === 401 ? '401错误' : '402错误'
|
||||||
|
const extraHint = unauthorizedStatus === 402 ? ',可能欠费' : ''
|
||||||
|
let reason = `OpenAI账号认证失败(${statusLabel}${extraHint})`
|
||||||
if (errorData) {
|
if (errorData) {
|
||||||
const messageCandidate =
|
const messageCandidate =
|
||||||
errorData.error &&
|
errorData.error &&
|
||||||
@@ -457,7 +463,7 @@ const handleResponses = async (req, res) => {
|
|||||||
? errorData.message.trim()
|
? errorData.message.trim()
|
||||||
: null
|
: null
|
||||||
if (messageCandidate) {
|
if (messageCandidate) {
|
||||||
reason = `OpenAI账号认证失败(401错误):${messageCandidate}`
|
reason = `OpenAI账号认证失败(${statusLabel}${extraHint}):${messageCandidate}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -469,7 +475,10 @@ const handleResponses = async (req, res) => {
|
|||||||
reason
|
reason
|
||||||
)
|
)
|
||||||
} catch (markError) {
|
} catch (markError) {
|
||||||
logger.error('❌ Failed to mark OpenAI account unauthorized after 401:', markError)
|
logger.error(
|
||||||
|
`❌ Failed to mark OpenAI account unauthorized after ${unauthorizedStatus}:`,
|
||||||
|
markError
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
let errorResponse = errorData
|
let errorResponse = errorData
|
||||||
@@ -485,7 +494,7 @@ const handleResponses = async (req, res) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(401).json(errorResponse)
|
res.status(unauthorizedStatus).json(errorResponse)
|
||||||
return
|
return
|
||||||
} else if (upstream.status === 200 || upstream.status === 201) {
|
} else if (upstream.status === 200 || upstream.status === 201) {
|
||||||
// 请求成功,检查并移除限流状态
|
// 请求成功,检查并移除限流状态
|
||||||
@@ -744,23 +753,25 @@ const handleResponses = async (req, res) => {
|
|||||||
// 优先使用主动设置的 statusCode,然后是上游响应的状态码,最后默认 500
|
// 优先使用主动设置的 statusCode,然后是上游响应的状态码,最后默认 500
|
||||||
const status = error.statusCode || error.response?.status || 500
|
const status = error.statusCode || error.response?.status || 500
|
||||||
|
|
||||||
if (status === 401 && accountId) {
|
if ((status === 401 || status === 402) && accountId) {
|
||||||
let reason = 'OpenAI账号认证失败(401错误)'
|
const statusLabel = status === 401 ? '401错误' : '402错误'
|
||||||
|
const extraHint = status === 402 ? ',可能欠费' : ''
|
||||||
|
let reason = `OpenAI账号认证失败(${statusLabel}${extraHint})`
|
||||||
const errorData = error.response?.data
|
const errorData = error.response?.data
|
||||||
if (errorData) {
|
if (errorData) {
|
||||||
if (typeof errorData === 'string' && errorData.trim()) {
|
if (typeof errorData === 'string' && errorData.trim()) {
|
||||||
reason = `OpenAI账号认证失败(401错误):${errorData.trim()}`
|
reason = `OpenAI账号认证失败(${statusLabel}${extraHint}):${errorData.trim()}`
|
||||||
} else if (
|
} else if (
|
||||||
errorData.error &&
|
errorData.error &&
|
||||||
typeof errorData.error.message === 'string' &&
|
typeof errorData.error.message === 'string' &&
|
||||||
errorData.error.message.trim()
|
errorData.error.message.trim()
|
||||||
) {
|
) {
|
||||||
reason = `OpenAI账号认证失败(401错误):${errorData.error.message.trim()}`
|
reason = `OpenAI账号认证失败(${statusLabel}${extraHint}):${errorData.error.message.trim()}`
|
||||||
} else if (typeof errorData.message === 'string' && errorData.message.trim()) {
|
} else if (typeof errorData.message === 'string' && errorData.message.trim()) {
|
||||||
reason = `OpenAI账号认证失败(401错误):${errorData.message.trim()}`
|
reason = `OpenAI账号认证失败(${statusLabel}${extraHint}):${errorData.message.trim()}`
|
||||||
}
|
}
|
||||||
} else if (error.message) {
|
} else if (error.message) {
|
||||||
reason = `OpenAI账号认证失败(401错误):${error.message}`
|
reason = `OpenAI账号认证失败(${statusLabel}${extraHint}):${error.message}`
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user