mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-02 18:39:06 +08:00
refactor(middleware): Add error property to CompletionResult and handle errors when checking API (#7407)
* refactor(aiCore): 添加错误处理 * remove console.log
This commit is contained in:
parent
d9b8e68c30
commit
50d6f1f831
@ -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: () => '' // 错误情况下没有文本结果
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ export interface CompletionsResult {
|
||||
rawOutput?: SdkRawOutput
|
||||
stream?: ReadableStream<SdkRawChunk> | ReadableStream<Chunk> | AsyncIterable<Chunk>
|
||||
controller?: AbortController
|
||||
|
||||
error?: string
|
||||
getText: () => string
|
||||
}
|
||||
|
||||
|
||||
@ -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')
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user