Skip to content

Commit

Permalink
Improve Dust API error handling for tokenize endpoint (#3397)
Browse files Browse the repository at this point in the history
* Improve Dust API error handling for tokenize endpoint

* ✨

* 🔙

* ✨
  • Loading branch information
flvndvd authored Jan 23, 2024
1 parent a355571 commit 3793d2e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 47 deletions.
99 changes: 56 additions & 43 deletions types/src/front/lib/core_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -128,7 +128,7 @@ export class CoreAPI {
method: "DELETE",
});

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getDatasets({
Expand All @@ -143,7 +143,7 @@ export class CoreAPI {
},
});

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getDataset({
Expand All @@ -165,7 +165,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async createDataset({
Expand All @@ -189,7 +189,7 @@ export class CoreAPI {
}),
});

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async cloneProject({
Expand All @@ -201,7 +201,7 @@ export class CoreAPI {
method: "POST",
});

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async createRun({
Expand Down Expand Up @@ -232,7 +232,7 @@ export class CoreAPI {
}),
});

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async createRunStream({
Expand Down Expand Up @@ -272,7 +272,7 @@ export class CoreAPI {
);

if (!response.ok || !response.body) {
return _resultFromResponse(response);
return this._resultFromResponse(response);
}

let hasRunId = false;
Expand Down Expand Up @@ -351,7 +351,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getRunsBatch({
Expand All @@ -374,7 +374,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getRun({
Expand All @@ -391,7 +391,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getRunStatus({
Expand All @@ -408,7 +408,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getSpecification({
Expand All @@ -427,7 +427,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getRunBlock({
Expand All @@ -448,7 +448,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async createDataSource({
Expand Down Expand Up @@ -477,7 +477,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getDataSource({
Expand All @@ -496,7 +496,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async deleteDataSource({
Expand All @@ -513,7 +513,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async searchDataSource(
Expand Down Expand Up @@ -559,7 +559,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getDataSourceDocuments({
Expand All @@ -586,7 +586,7 @@ export class CoreAPI {
method: "GET",
}
);
return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getDataSourceDocument({
Expand Down Expand Up @@ -615,7 +615,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getDataSourceDocumentVersions({
Expand Down Expand Up @@ -658,7 +658,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async upsertDataSourceDocument({
Expand Down Expand Up @@ -713,7 +713,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async updateDataSourceDocumentTags({
Expand Down Expand Up @@ -749,7 +749,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async updateDataSourceDocumentParents({
Expand Down Expand Up @@ -782,7 +782,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async deleteDataSourceDocument({
Expand All @@ -803,7 +803,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async tokenize({
Expand All @@ -829,7 +829,7 @@ export class CoreAPI {
}),
});

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async dataSourceTokenize({
Expand All @@ -851,7 +851,7 @@ export class CoreAPI {
body: JSON.stringify({ text }),
}
);
return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async upsertTable({
Expand Down Expand Up @@ -882,7 +882,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getTable({
Expand All @@ -901,7 +901,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getTables({
Expand All @@ -922,7 +922,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async deleteTable({
Expand All @@ -941,7 +941,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async upsertTableRows({
Expand Down Expand Up @@ -971,7 +971,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getTableRow({
Expand All @@ -992,7 +992,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async getTableRows({
Expand Down Expand Up @@ -1022,7 +1022,7 @@ export class CoreAPI {
}
);

return _resultFromResponse(response);
return this._resultFromResponse(response);
}

async queryDatabase({
Expand Down Expand Up @@ -1052,16 +1052,29 @@ export class CoreAPI {
}),
});

return _resultFromResponse(response);
return this._resultFromResponse(response);
}
}

async function _resultFromResponse<T>(
response: Response
): Promise<CoreAPIResponse<T>> {
const jsonResponse = await response.json();
if (jsonResponse.error) {
return new Err(jsonResponse.error);
private async _resultFromResponse<T>(
response: Response
): Promise<CoreAPIResponse<T>> {
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);
}
22 changes: 18 additions & 4 deletions types/src/front/lib/dust_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);
}
}

0 comments on commit 3793d2e

Please sign in to comment.