From 3793d2e7511f2fd4a5b3350e874c6593dbb3fad9 Mon Sep 17 00:00:00 2001 From: Flavien David Date: Tue, 23 Jan 2024 16:56:51 +0100 Subject: [PATCH] Improve Dust API error handling for tokenize endpoint (#3397) * Improve Dust API error handling for tokenize endpoint * :sparkles: * :back: * :sparkles: --- types/src/front/lib/core_api.ts | 99 +++++++++++++++++++-------------- types/src/front/lib/dust_api.ts | 22 ++++++-- 2 files changed, 74 insertions(+), 47 deletions(-) diff --git a/types/src/front/lib/core_api.ts b/types/src/front/lib/core_api.ts index 63d6a596c60c..9d132f38be64 100644 --- a/types/src/front/lib/core_api.ts +++ b/types/src/front/lib/core_api.ts @@ -116,7 +116,7 @@ export class CoreAPI { const response = await fetch(`${CORE_API}/projects`, { method: "POST", }); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async deleteProject({ @@ -128,7 +128,7 @@ export class CoreAPI { method: "DELETE", }); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getDatasets({ @@ -143,7 +143,7 @@ export class CoreAPI { }, }); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getDataset({ @@ -165,7 +165,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async createDataset({ @@ -189,7 +189,7 @@ export class CoreAPI { }), }); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async cloneProject({ @@ -201,7 +201,7 @@ export class CoreAPI { method: "POST", }); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async createRun({ @@ -232,7 +232,7 @@ export class CoreAPI { }), }); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async createRunStream({ @@ -272,7 +272,7 @@ export class CoreAPI { ); if (!response.ok || !response.body) { - return _resultFromResponse(response); + return this._resultFromResponse(response); } let hasRunId = false; @@ -351,7 +351,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getRunsBatch({ @@ -374,7 +374,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getRun({ @@ -391,7 +391,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getRunStatus({ @@ -408,7 +408,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getSpecification({ @@ -427,7 +427,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getRunBlock({ @@ -448,7 +448,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async createDataSource({ @@ -477,7 +477,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getDataSource({ @@ -496,7 +496,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async deleteDataSource({ @@ -513,7 +513,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async searchDataSource( @@ -559,7 +559,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getDataSourceDocuments({ @@ -586,7 +586,7 @@ export class CoreAPI { method: "GET", } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getDataSourceDocument({ @@ -615,7 +615,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getDataSourceDocumentVersions({ @@ -658,7 +658,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async upsertDataSourceDocument({ @@ -713,7 +713,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async updateDataSourceDocumentTags({ @@ -749,7 +749,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async updateDataSourceDocumentParents({ @@ -782,7 +782,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async deleteDataSourceDocument({ @@ -803,7 +803,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async tokenize({ @@ -829,7 +829,7 @@ export class CoreAPI { }), }); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async dataSourceTokenize({ @@ -851,7 +851,7 @@ export class CoreAPI { body: JSON.stringify({ text }), } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async upsertTable({ @@ -882,7 +882,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getTable({ @@ -901,7 +901,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getTables({ @@ -922,7 +922,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async deleteTable({ @@ -941,7 +941,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async upsertTableRows({ @@ -971,7 +971,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getTableRow({ @@ -992,7 +992,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async getTableRows({ @@ -1022,7 +1022,7 @@ export class CoreAPI { } ); - return _resultFromResponse(response); + return this._resultFromResponse(response); } async queryDatabase({ @@ -1052,16 +1052,29 @@ export class CoreAPI { }), }); - return _resultFromResponse(response); + return this._resultFromResponse(response); } -} -async function _resultFromResponse( - response: Response -): Promise> { - const jsonResponse = await response.json(); - if (jsonResponse.error) { - return new Err(jsonResponse.error); + private async _resultFromResponse( + response: Response + ): Promise> { + try { + const jsonResponse = await response.json(); + + if (jsonResponse.error) { + return new Err(jsonResponse.error); + } + return new Ok(jsonResponse.response); + } catch (err) { + const rawResponse = await response.text(); + + const message = `Dust Core API responded with status: ${response.status}.`; + this._logger.error( + { status: response.status, response: rawResponse }, + message + ); + + return new Err({ code: response.status.toString(), message }); + } } - return new Ok(jsonResponse.response); } diff --git a/types/src/front/lib/dust_api.ts b/types/src/front/lib/dust_api.ts index a7e51154ff2c..c38a7ab27bf6 100644 --- a/types/src/front/lib/dust_api.ts +++ b/types/src/front/lib/dust_api.ts @@ -717,10 +717,24 @@ export class DustAPI { text, }), }); - const dustRequestResult = await res.json(); - if (dustRequestResult.error) { - return new Err(dustRequestResult.error as DustAPIErrorResponse); + + try { + const dustRequestResult = await res.json(); + + if (dustRequestResult.error) { + return new Err(dustRequestResult.error as DustAPIErrorResponse); + } + return new Ok(dustRequestResult.tokens as CoreAPITokenType[]); + } catch (err) { + const rawResponse = await res.text(); + + const message = `Dust API /tokenize responded with status: ${res.status}.`; + this._logger.error( + { status: res.status, response: rawResponse }, + message + ); + + return new Err({ type: "bad_request", message }); } - return new Ok(dustRequestResult.tokens as CoreAPITokenType[]); } }