fix: use shouldThrow param in checkApi instead of adding error property to CompletionsResult (#7457)

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

This reverts commit 50d6f1f831.

* fix: use shouldThrow param in checkApi
This commit is contained in:
Wang Jiyuan 2025-06-22 21:33:17 +08:00 committed by GitHub
parent 355d2aebb4
commit 2350919f36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 6 additions and 11 deletions

View File

@ -25,7 +25,7 @@ export const ErrorHandlerMiddleware =
// 尝试执行下一个中间件
return await next(ctx, params)
} catch (error: any) {
console.error('ErrorHandlerMiddleware_error', error)
console.log('ErrorHandlerMiddleware_error', error)
// 1. 使用通用的工具函数将错误解析为标准格式
const errorChunk = createErrorChunk(error)
// 2. 调用从外部传入的 onError 回调
@ -50,7 +50,6 @@ 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

@ -590,14 +590,12 @@ export async function checkApi(provider: Provider, model: Model): Promise<void>
callType: 'check',
messages: 'hi',
assistant,
streamOutput: true
streamOutput: true,
shouldThrow: true
}
// 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,12 +606,10 @@ export async function checkApi(provider: Provider, model: Model): Promise<void>
callType: 'check',
messages: 'hi',
assistant,
streamOutput: false
streamOutput: false,
shouldThrow: true
}
const result = await ai.completions(params)
if (result.error) {
throw new Error(result.error)
}
if (!result.getText()) {
throw new Error('No response received')
}