refactor(middleware): Add error property to CompletionResult and handle errors when checking API (#7407)

* refactor(aiCore): 添加错误处理

* remove console.log
This commit is contained in:
Wang Jiyuan 2025-06-22 17:03:43 +08:00 committed by GitHub
parent d9b8e68c30
commit 50d6f1f831
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 2 deletions

View File

@ -25,7 +25,7 @@ export const ErrorHandlerMiddleware =
// 尝试执行下一个中间件
return await next(ctx, params)
} catch (error: any) {
console.log('ErrorHandlerMiddleware_error', error)
console.error('ErrorHandlerMiddleware_error', error)
// 1. 使用通用的工具函数将错误解析为标准格式
const errorChunk = createErrorChunk(error)
// 2. 调用从外部传入的 onError 回调
@ -50,6 +50,7 @@ export const ErrorHandlerMiddleware =
rawOutput: undefined,
stream: errorStream, // 将包含错误的流传递下去
controller: undefined,
error: typeof error?.message === 'string' ? error.message : 'unknown error',
getText: () => '' // 错误情况下没有文本结果
}
}

View File

@ -62,7 +62,7 @@ export interface CompletionsResult {
rawOutput?: SdkRawOutput
stream?: ReadableStream<SdkRawChunk> | ReadableStream<Chunk> | AsyncIterable<Chunk>
controller?: AbortController
error?: string
getText: () => string
}

View File

@ -595,6 +595,9 @@ export async function checkApi(provider: Provider, model: Model): Promise<void>
// Try streaming check first
const result = await ai.completions(params)
if (result.error) {
throw new Error(result.error)
}
if (!result.getText()) {
throw new Error('No response received')
}
@ -608,6 +611,9 @@ export async function checkApi(provider: Provider, model: Model): Promise<void>
streamOutput: false
}
const result = await ai.completions(params)
if (result.error) {
throw new Error(result.error)
}
if (!result.getText()) {
throw new Error('No response received')
}