Skip to content

Commit 251f7aa

Browse files
committed
improve error code
1 parent f394b21 commit 251f7aa

File tree

9 files changed

+50
-56
lines changed

9 files changed

+50
-56
lines changed

src/app/(backend)/middleware/auth/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ export const checkAuth =
4848
nextAuthAuthorized: oauthAuthorized,
4949
});
5050
} catch (e) {
51+
// if the error is not a ChatCompletionErrorPayload, it means the application error
52+
if (!(e as ChatCompletionErrorPayload).errorType) {
53+
if ((e as any).code === 'ERR_JWT_EXPIRED')
54+
return createErrorResponse(ChatErrorType.SystemTimeNotMatchError, e);
55+
56+
// other issue will be internal server error
57+
return createErrorResponse(ChatErrorType.InternalServerError, e);
58+
}
59+
5160
const {
5261
errorType = ChatErrorType.InternalServerError,
5362
error: errorContent,

src/features/Conversation/Error/OpenAiBizError.tsx

-29
This file was deleted.

src/features/Conversation/Error/index.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import ClerkLogin from './ClerkLogin';
1414
import ErrorJsonViewer from './ErrorJsonViewer';
1515
import InvalidAPIKey from './InvalidAPIKey';
1616
import InvalidAccessCode from './InvalidAccessCode';
17-
import OpenAiBizError from './OpenAiBizError';
1817

1918
const loading = () => <Skeleton active />;
2019

@@ -34,8 +33,11 @@ const getErrorAlertConfig = (
3433
};
3534

3635
switch (errorType) {
36+
case ChatErrorType.SystemTimeNotMatchError:
3737
case AgentRuntimeErrorType.PermissionDenied:
38+
case AgentRuntimeErrorType.InsufficientQuota:
3839
case AgentRuntimeErrorType.QuotaLimitReached:
40+
case AgentRuntimeErrorType.ExceededContextWindow:
3941
case AgentRuntimeErrorType.LocationNotSupportError: {
4042
return {
4143
type: 'warning',
@@ -82,10 +84,6 @@ const ErrorMessageExtra = memo<{ data: ChatMessage }>(({ data }) => {
8284
return <PluginSettings id={data.id} plugin={data.plugin} />;
8385
}
8486

85-
case AgentRuntimeErrorType.OpenAIBizError: {
86-
return <OpenAiBizError {...data} />;
87-
}
88-
8987
case AgentRuntimeErrorType.OllamaBizError: {
9088
return <OllamaBizError {...data} />;
9189
}

src/libs/agent-runtime/error.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
export const AgentRuntimeErrorType = {
44
AgentRuntimeError: 'AgentRuntimeError', // Agent Runtime 模块运行时错误
55
LocationNotSupportError: 'LocationNotSupportError',
6+
67
QuotaLimitReached: 'QuotaLimitReached',
8+
InsufficientQuota: 'InsufficientQuota',
9+
710
PermissionDenied: 'PermissionDenied',
11+
ExceededContextWindow: 'ExceededContextWindow',
812

913
InvalidProviderAPIKey: 'InvalidProviderAPIKey',
1014
ProviderBizError: 'ProviderBizError',
@@ -24,10 +28,6 @@ export const AgentRuntimeErrorType = {
2428
* @deprecated
2529
*/
2630
NoOpenAIAPIKey: 'NoOpenAIAPIKey',
27-
/**
28-
* @deprecated
29-
*/
30-
OpenAIBizError: 'OpenAIBizError',
3131
} as const;
3232

3333
export const AGENT_RUNTIME_ERROR_SET = new Set<string>(Object.values(AgentRuntimeErrorType));

src/libs/agent-runtime/utils/openaiCompatibleFactory/index.ts

+22
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,28 @@ export const LobeOpenAICompatibleFactory = <T extends Record<string, any> = any>
386386

387387
const { errorResult, RuntimeError } = handleOpenAIError(error);
388388

389+
switch (errorResult.code) {
390+
case 'insufficient_quota': {
391+
return AgentRuntimeError.chat({
392+
endpoint: desensitizedEndpoint,
393+
error: errorResult,
394+
errorType: AgentRuntimeErrorType.InsufficientQuota,
395+
provider: provider as ModelProvider,
396+
});
397+
}
398+
399+
// content too long
400+
case 'context_length_exceeded':
401+
case 'string_above_max_length': {
402+
return AgentRuntimeError.chat({
403+
endpoint: desensitizedEndpoint,
404+
error: errorResult,
405+
errorType: AgentRuntimeErrorType.ExceededContextWindow,
406+
provider: provider as ModelProvider,
407+
});
408+
}
409+
}
410+
389411
return AgentRuntimeError.chat({
390412
endpoint: desensitizedEndpoint,
391413
error: errorResult,

src/locales/default/error.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ export default {
9090

9191
InvalidAccessCode: '密码不正确或为空,请输入正确的访问密码,或者添加自定义 API Key',
9292
InvalidClerkUser: '很抱歉,你当前尚未登录,请先登录或注册账号后继续操作',
93+
SystemTimeNotMatchError: '很抱歉,您的系统时间和服务器不匹配,请检查您的系统时间后重试',
9394
LocationNotSupportError:
9495
'很抱歉,你的所在地区不支持此模型服务,可能是由于区域限制或服务未开通。请确认当前地区是否支持使用此服务,或尝试使用切换到其他地区后重试。',
96+
InsufficientQuota:
97+
'很抱歉,该密钥的配额(quota)已达上限,请检查账户余额是否充足,或增大密钥配额后再试',
98+
ExceededContextWindow: '当前请求内容超出模型可处理的长度,请减少内容量后重试',
9599
QuotaLimitReached:
96100
'很抱歉,当前 Token 用量或请求次数已达该密钥的配额(quota)上限,请增加该密钥的配额或稍后再试',
97101
PermissionDenied: '很抱歉,你没有权限访问该服务,请检查你的密钥是否有访问权限',
@@ -101,10 +105,6 @@ export default {
101105
* @deprecated
102106
*/
103107
NoOpenAIAPIKey: 'OpenAI API Key 不正确或为空,请添加自定义 OpenAI API Key',
104-
/**
105-
* @deprecated
106-
*/
107-
OpenAIBizError: '请求 OpenAI 服务出错,请根据以下信息排查或重试',
108108

109109
InvalidVertexCredentials: 'Vertex 鉴权未通过,请检查鉴权凭证后重试',
110110
InvalidBedrockCredentials: 'Bedrock 鉴权未通过,请检查 AccessKeyId/SecretAccessKey 后重试',

src/types/fetch.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const ChatErrorType = {
1313
OllamaServiceUnavailable: 'OllamaServiceUnavailable', // 未启动/检测到 Ollama 服务
1414
PluginFailToTransformArguments: 'PluginFailToTransformArguments',
1515
UnknownChatFetchError: 'UnknownChatFetchError',
16+
SystemTimeNotMatchError: 'SystemTimeNotMatchError',
1617

1718
// ******* 客户端错误 ******* //
1819
BadRequest: 400,

src/utils/errorResponse.test.ts

-12
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ describe('createErrorResponse', () => {
3333
});
3434

3535
describe('Provider Biz Error', () => {
36-
it('returns a 471 status for OpenAIBizError error type', () => {
37-
const errorType = AgentRuntimeErrorType.OpenAIBizError;
38-
const response = createErrorResponse(errorType);
39-
expect(response.status).toBe(471);
40-
});
41-
4236
it('returns a 471 status for ProviderBizError error type', () => {
4337
const errorType = AgentRuntimeErrorType.ProviderBizError;
4438
const response = createErrorResponse(errorType);
@@ -50,12 +44,6 @@ describe('createErrorResponse', () => {
5044
const response = createErrorResponse(errorType);
5145
expect(response.status).toBe(470);
5246
});
53-
54-
it('returns a 471 status for OpenAIBizError error type', () => {
55-
const errorType = AgentRuntimeErrorType.OpenAIBizError;
56-
const response = createErrorResponse(errorType as any);
57-
expect(response.status).toBe(471);
58-
});
5947
});
6048

6149
// 测试状态码不在200-599范围内的情况

src/utils/errorResponse.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ const getStatus = (errorType: ILobeAgentRuntimeErrorType | ErrorType) => {
1212
return 401;
1313
}
1414

15+
case AgentRuntimeErrorType.ExceededContextWindow:
16+
case ChatErrorType.SystemTimeNotMatchError: {
17+
return 400;
18+
}
19+
1520
case AgentRuntimeErrorType.LocationNotSupportError: {
1621
return 403;
1722
}
1823

24+
case AgentRuntimeErrorType.InsufficientQuota:
1925
case AgentRuntimeErrorType.QuotaLimitReached: {
2026
return 429;
2127
}
@@ -25,8 +31,7 @@ const getStatus = (errorType: ILobeAgentRuntimeErrorType | ErrorType) => {
2531
return 470;
2632
}
2733

28-
case AgentRuntimeErrorType.ProviderBizError:
29-
case AgentRuntimeErrorType.OpenAIBizError: {
34+
case AgentRuntimeErrorType.ProviderBizError: {
3035
return 471;
3136
}
3237

0 commit comments

Comments
 (0)