Skip to content

Commit

Permalink
Account for response unpacking, add check for last resort message
Browse files Browse the repository at this point in the history
  • Loading branch information
rtexelm committed Jun 25, 2024
1 parent 66d7416 commit d54c418
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
isJsonString,
} from '@superset-ui/core';

// The response always contains an error attribute, can contain anything from the
// SupersetClientResponse object, and can contain a spread JSON blob
// The response always contains an error attribute, can contain anything from
// the SupersetClientResponse object, and can contain a spread JSON blob
export type ClientErrorObject = {
error: string;
errors?: SupersetError[];
Expand Down Expand Up @@ -110,17 +110,17 @@ export function getErrorFromStatusCode(status: number): string | null {

export function retrieveErrorMessage(
str: string,
response: Response | JsonObject,
errorObject: JsonObject,
): string {
const statusError =
'status' in response ? getErrorFromStatusCode(response.status) : null;
'status' in errorObject ? getErrorFromStatusCode(errorObject.status) : null;

// Prefer status code message over the response text
// Prefer status code message over the response or HTML text
return statusError || parseStringResponse(str);
}

export function parseErrorJson(responseObject: JsonObject): ClientErrorObject {
let error = { ...responseObject };
export function parseErrorJson(responseJson: JsonObject): ClientErrorObject {
let error = { ...responseJson };
// Backwards compatibility for old error renderers with the new error object
if (error.errors && error.errors.length > 0) {
error.error = error.description = error.errors[0].message;
Expand Down Expand Up @@ -231,13 +231,20 @@ export function getClientErrorObject(
.clone()
.json()
.then(errorJson => {
const error = { ...responseObject, ...errorJson };
// Destructuring instead of spreading to avoid loss of sibling properties to the body
const { url, status, statusText, redirected, type } = responseObject;
const responseSummary = { url, status, statusText, redirected, type };
const error = {
...errorJson,
...responseSummary,
};
resolve(parseErrorJson(error));
})
.catch(() => {
// fall back to reading as text
responseObject.text().then((errorText: any) => {
resolve({
// Destructuring not necessary here
...responseObject,
error: t(retrieveErrorMessage(errorText, responseObject)),
});
Expand All @@ -255,7 +262,7 @@ export function getClientErrorObject(
}
resolve({
...responseObject,
error,
error: t(parseStringResponse(error)),
});
});
}
Expand Down

0 comments on commit d54c418

Please sign in to comment.