Refactor to remove redundency

This commit is contained in:
rtexelm 2024-06-21 23:25:24 -04:00
parent 847dd0342c
commit 66d7416dd9
1 changed files with 15 additions and 27 deletions

View File

@ -104,7 +104,7 @@ export function parseStringResponse(str: string): string {
return str; return str;
} }
export function getErrorFromStatusCode(status: number): string { export function getErrorFromStatusCode(status: number): string | null {
return ERROR_CODE_LOOKUP[status] || null; return ERROR_CODE_LOOKUP[status] || null;
} }
@ -112,16 +112,11 @@ export function retrieveErrorMessage(
str: string, str: string,
response: Response | JsonObject, response: Response | JsonObject,
): string { ): string {
if (checkForHtml(str) && 'status' in response) { const statusError =
// Prefer the error message from the response status if it exists 'status' in response ? getErrorFromStatusCode(response.status) : null;
return getErrorFromStatusCode(response.status)
? getErrorFromStatusCode(response.status) // Prefer status code message over the response text
: parseStringResponse(str); return statusError || parseStringResponse(str);
}
if (checkForHtml(str)) {
return parseStringResponse(str);
}
return str;
} }
export function parseErrorJson(responseObject: JsonObject): ClientErrorObject { export function parseErrorJson(responseObject: JsonObject): ClientErrorObject {
@ -140,7 +135,11 @@ export function parseErrorJson(responseObject: JsonObject): ClientErrorObject {
t('Invalid input'); t('Invalid input');
} }
if (typeof error.message === 'string') { if (typeof error.message === 'string') {
error.error = retrieveErrorMessage(error.message, error); if (checkForHtml(error.message)) {
error.error = t(retrieveErrorMessage(error.message, error));
} else {
error.error = error.message;
}
} }
} }
if (error.stack) { if (error.stack) {
@ -168,8 +167,9 @@ export function getClientErrorObject(
| { response: Response } | { response: Response }
| string, | string,
): Promise<ClientErrorObject> { ): Promise<ClientErrorObject> {
// takes a SupersetClientResponse as input, attempts to read response as Json if possible, // takes a SupersetClientResponse as input, attempts to read response as Json
// and returns a Promise that resolves to a plain object with error key and text value. // if possible, and returns a Promise that resolves to a plain object with
// error key and text value.
return new Promise(resolve => { return new Promise(resolve => {
if (typeof response === 'string') { if (typeof response === 'string') {
resolve({ error: parseStringResponse(response) }); resolve({ error: parseStringResponse(response) });
@ -223,18 +223,6 @@ export function getClientErrorObject(
const responseObject = const responseObject =
response instanceof Response ? response : response.response; response instanceof Response ? response : response.response;
// Check for HTTP status code in ERROR_CODE_LOOKUP
if (responseObject && 'status' in responseObject) {
const errorCode = ERROR_CODE_LOOKUP[responseObject.status];
if (errorCode) {
resolve({
...responseObject,
error: t(errorCode),
});
return;
}
}
if (responseObject && !responseObject.bodyUsed) { if (responseObject && !responseObject.bodyUsed) {
// attempt to read the body as json, and fallback to text. we must clone // attempt to read the body as json, and fallback to text. we must clone
// the response in order to fallback to .text() because Response is // the response in order to fallback to .text() because Response is
@ -251,7 +239,7 @@ export function getClientErrorObject(
responseObject.text().then((errorText: any) => { responseObject.text().then((errorText: any) => {
resolve({ resolve({
...responseObject, ...responseObject,
error: retrieveErrorMessage(errorText, responseObject), error: t(retrieveErrorMessage(errorText, responseObject)),
}); });
}); });
}); });