Skip to content

Commit

Permalink
fix: parse simple string error message values (apache#14360)
Browse files Browse the repository at this point in the history
* fix parsing error messages that are formatted as single strings

* fix other instances of error typing
  • Loading branch information
samtfm authored and cccs-RyanS committed Dec 17, 2021
1 parent 6b49b11 commit 344abfa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 16 additions & 9 deletions superset-frontend/src/views/CRUD/hooks.ts
Expand Up @@ -39,13 +39,18 @@ interface ListViewResourceState<D extends object = any> {
}

const parsedErrorMessage = (
errorMessage: Record<string, string[]> | string,
errorMessage: Record<string, string[] | string> | string,
) => {
if (typeof errorMessage === 'string') {
return errorMessage;
}
return Object.entries(errorMessage)
.map(([key, value]) => `(${key}) ${value.join(', ')}`)
.map(([key, value]) => {
if (Array.isArray(value)) {
return `(${key}) ${value.join(', ')}`;
}
return `(${key}) ${value}`;
})
.join('\n');
};

Expand Down Expand Up @@ -200,7 +205,7 @@ export function useListViewResource<D extends object = any>(
interface SingleViewResourceState<D extends object = any> {
loading: boolean;
resource: D | null;
error: string | Record<string, string[]> | null;
error: string | Record<string, string[] | string> | null;
}

export function useSingleViewResource<D extends object = any>(
Expand Down Expand Up @@ -236,7 +241,7 @@ export function useSingleViewResource<D extends object = any>(
});
return json.result;
},
createErrorHandler((errMsg: Record<string, string[]>) => {
createErrorHandler((errMsg: Record<string, string[] | string>) => {
handleErrorMsg(
t(
'An error occurred while fetching %ss: %s',
Expand Down Expand Up @@ -277,7 +282,7 @@ export function useSingleViewResource<D extends object = any>(
});
return json.id;
},
createErrorHandler((errMsg: Record<string, string[]>) => {
createErrorHandler((errMsg: Record<string, string[] | string>) => {
handleErrorMsg(
t(
'An error occurred while creating %ss: %s',
Expand Down Expand Up @@ -391,19 +396,21 @@ export function useImportResource(
payload.includes('already exists and `overwrite=true` was not passed');

const getPasswordsNeeded = (
errMsg: Record<string, Record<string, string[]>>,
errMsg: Record<string, Record<string, string[] | string>>,
) =>
Object.entries(errMsg)
.filter(([, validationErrors]) => isNeedsPassword(validationErrors))
.map(([fileName]) => fileName);

const getAlreadyExists = (errMsg: Record<string, Record<string, string[]>>) =>
const getAlreadyExists = (
errMsg: Record<string, Record<string, string[] | string>>,
) =>
Object.entries(errMsg)
.filter(([, validationErrors]) => isAlreadyExists(validationErrors))
.map(([fileName]) => fileName);

const hasTerminalValidation = (
errMsg: Record<string, Record<string, string[]>>,
errMsg: Record<string, Record<string, string[] | string>>,
) =>
Object.values(errMsg).some(
validationErrors =>
Expand Down Expand Up @@ -631,7 +638,7 @@ export const testDatabaseConnection = (
() => {
addSuccessToast(t('Connection looks good!'));
},
createErrorHandler((errMsg: Record<string, string[]> | string) => {
createErrorHandler((errMsg: Record<string, string[] | string> | string) => {
handleErrorMsg(t(`${t('ERROR: ')}${parsedErrorMessage(errMsg)}`));
}),
);
Expand Down
4 changes: 3 additions & 1 deletion superset-frontend/src/views/CRUD/utils.tsx
Expand Up @@ -156,7 +156,9 @@ export const createFetchRelated = createFetchResourceMethod('related');
export const createFetchDistinct = createFetchResourceMethod('distinct');

export function createErrorHandler(
handleErrorFunc: (errMsg?: string | Record<string, string[]>) => void,
handleErrorFunc: (
errMsg?: string | Record<string, string[] | string>,
) => void,
) {
return async (e: SupersetClientResponse | string) => {
const parsedError = await getClientErrorObject(e);
Expand Down

0 comments on commit 344abfa

Please sign in to comment.