From e4631d44de721ece08c3594492d093de13a41335 Mon Sep 17 00:00:00 2001 From: Jack Tri Le Date: Wed, 10 Apr 2024 16:12:59 +0700 Subject: [PATCH 1/3] fix error handling of core for mistral integration --- core/src/browser/extensions/engines/helpers/sse.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/browser/extensions/engines/helpers/sse.ts b/core/src/browser/extensions/engines/helpers/sse.ts index ab8d261a5d..ced0f8559e 100644 --- a/core/src/browser/extensions/engines/helpers/sse.ts +++ b/core/src/browser/extensions/engines/helpers/sse.ts @@ -38,7 +38,7 @@ export function requestInference( const data = await response.json() const error = { message: data.error?.message ?? 'Error occurred.', - code: data.error?.code ?? data.error?.type ?? ErrorCode.Unknown, + code: data.error?.code ?? data.error?.type ?? (data.message ==='Unauthorized'|| data.message === 'No API key found in request')? ErrorCode.InvalidApiKey: ErrorCode.Unknown, } subscriber.error(error) subscriber.complete() From 6987bc872f226b830a502509e81c0647756c663f Mon Sep 17 00:00:00 2001 From: Jack Tri Le Date: Wed, 10 Apr 2024 16:56:01 +0700 Subject: [PATCH 2/3] refactor logic using status code --- core/src/browser/extensions/engines/helpers/sse.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core/src/browser/extensions/engines/helpers/sse.ts b/core/src/browser/extensions/engines/helpers/sse.ts index ced0f8559e..93a4cf7632 100644 --- a/core/src/browser/extensions/engines/helpers/sse.ts +++ b/core/src/browser/extensions/engines/helpers/sse.ts @@ -36,9 +36,15 @@ export function requestInference( .then(async (response) => { if (!response.ok) { const data = await response.json() + let errorCode = ErrorCode.Unknown; + if (data.error) { + errorCode = data.error.code ?? data.error.type; + } else if (response.status === 401) { + errorCode = ErrorCode.InvalidApiKey; + } const error = { message: data.error?.message ?? 'Error occurred.', - code: data.error?.code ?? data.error?.type ?? (data.message ==='Unauthorized'|| data.message === 'No API key found in request')? ErrorCode.InvalidApiKey: ErrorCode.Unknown, + code: errorCode, } subscriber.error(error) subscriber.complete() From fa02ca4a4e73bdbd311dcb0902c8e57581d6ce33 Mon Sep 17 00:00:00 2001 From: Jack Tri Le Date: Wed, 10 Apr 2024 17:01:24 +0700 Subject: [PATCH 3/3] nitpicking fix --- core/src/browser/extensions/engines/helpers/sse.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/browser/extensions/engines/helpers/sse.ts b/core/src/browser/extensions/engines/helpers/sse.ts index 93a4cf7632..dbc58501aa 100644 --- a/core/src/browser/extensions/engines/helpers/sse.ts +++ b/core/src/browser/extensions/engines/helpers/sse.ts @@ -38,7 +38,7 @@ export function requestInference( const data = await response.json() let errorCode = ErrorCode.Unknown; if (data.error) { - errorCode = data.error.code ?? data.error.type; + errorCode = data.error.code ?? data.error.type ?? ErrorCode.Unknown } else if (response.status === 401) { errorCode = ErrorCode.InvalidApiKey; }