fix: parse simple string error message values (#14360)

* fix parsing error messages that are formatted as single strings

* fix other instances of error typing
This commit is contained in:
Sam Faber-Manning 2021-04-30 09:30:53 -07:00 committed by GitHub
parent 11260b3117
commit b147fa84e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

View File

@ -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');
};
@ -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>(
@ -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',
@ -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',
@ -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 =>
@ -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)}`));
}),
);

View File

@ -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);