Skip to content

Commit fe7e631

Browse files
authored
♻️ refactor: refactor the google ai return error (#6978)
1 parent 1810cf3 commit fe7e631

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

src/libs/agent-runtime/google/index.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ describe('LobeGoogleAI', () => {
340340
expect(e).toEqual({
341341
errorType: bizErrorType,
342342
error: {
343-
message: `[GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1/models/gemini-pro:streamGenerateContent?alt=sse: [400 Bad Request] API key not valid. Please pass a valid API key. [{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"Error","domain":"googleapis.com","metadata":{"service":"generativelanguage.googleapis.com}}]`,
343+
message: `API key not valid. Please pass a valid API key. [{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"Error","domain":"googleapis.com","metadata":{"service":"generativelanguage.googleapis.com}}]`,
344+
statusCode: 400,
345+
statusCodeText: '[400 Bad Request]',
344346
},
345347
provider,
346348
});

src/libs/agent-runtime/google/index.ts

+50-7
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,12 @@ export class LobeGoogleAI implements LobeRuntimeAI {
325325
if (message.includes('location is not supported'))
326326
return { error: { message }, errorType: AgentRuntimeErrorType.LocationNotSupportError };
327327

328-
try {
329-
const startIndex = message.lastIndexOf('[');
330-
if (startIndex === -1) {
331-
return defaultError;
332-
}
328+
const startIndex = message.lastIndexOf('[');
329+
if (startIndex === -1) {
330+
return defaultError;
331+
}
333332

333+
try {
334334
// 从开始位置截取字符串到最后
335335
const jsonString = message.slice(startIndex);
336336

@@ -349,9 +349,18 @@ export class LobeGoogleAI implements LobeRuntimeAI {
349349
}
350350
}
351351
} catch {
352-
// 如果解析失败,则返回原始错误消息
353-
return defaultError;
352+
//
354353
}
354+
355+
const errorObj = this.extractErrorObjectFromError(message);
356+
357+
const { errorDetails } = errorObj;
358+
359+
if (errorDetails) {
360+
return { error: errorDetails, errorType: AgentRuntimeErrorType.ProviderBizError };
361+
}
362+
363+
return defaultError;
355364
}
356365

357366
private buildGoogleTools(
@@ -392,6 +401,40 @@ export class LobeGoogleAI implements LobeRuntimeAI {
392401
},
393402
};
394403
};
404+
405+
private extractErrorObjectFromError(message: string) {
406+
// 使用正则表达式匹配状态码部分 [数字 描述文本]
407+
const regex = /^(.*?)(\[\d+ [^\]]+])(.*)$/;
408+
const match = message.match(regex);
409+
410+
if (match) {
411+
const prefix = match[1].trim();
412+
const statusCodeWithBrackets = match[2].trim();
413+
const message = match[3].trim();
414+
415+
// 提取状态码数字
416+
const statusCodeMatch = statusCodeWithBrackets.match(/\[(\d+)/);
417+
const statusCode = statusCodeMatch ? parseInt(statusCodeMatch[1]) : null;
418+
419+
// 创建包含状态码和消息的JSON
420+
const resultJson = {
421+
message: message,
422+
statusCode: statusCode,
423+
statusCodeText: statusCodeWithBrackets,
424+
};
425+
426+
return {
427+
errorDetails: resultJson,
428+
prefix: prefix,
429+
};
430+
}
431+
432+
// 如果无法匹配,返回原始消息
433+
return {
434+
errorDetails: null,
435+
prefix: message,
436+
};
437+
}
395438
}
396439

397440
export default LobeGoogleAI;

0 commit comments

Comments
 (0)