update axioserror

This commit is contained in:
Proddy
2022-05-01 13:12:40 +02:00
parent 0143e89e27
commit 6b315d52a8
16 changed files with 55 additions and 31 deletions

View File

@@ -1,4 +1,10 @@
import { AxiosError } from 'axios';
export const extractErrorMessage = (error: AxiosError, defaultMessage: string) =>
(error.response && error.response.data ? error.response.data.message : error.message) || defaultMessage;
export const extractErrorMessage = (error: unknown, defaultMessage: string) => {
if (error instanceof AxiosError) {
return error.response && error.response.data && error?.response?.data?.message;
} else if (error instanceof Error) {
return error.message;
}
return defaultMessage;
};

View File

@@ -22,7 +22,7 @@ export const useRest = <D>({ read, update }: RestRequestOptions<D>) => {
setErrorMessage(undefined);
try {
setData((await read()).data);
} catch (error: any) {
} catch (error: unknown) {
const message = extractErrorMessage(error, 'Problem loading data');
enqueueSnackbar(message, { variant: 'error' });
setErrorMessage(message);
@@ -45,7 +45,7 @@ export const useRest = <D>({ read, update }: RestRequestOptions<D>) => {
} else {
enqueueSnackbar('Settings saved', { variant: 'success' });
}
} catch (error: any) {
} catch (error: unknown) {
const message = extractErrorMessage(error, 'Problem saving data');
enqueueSnackbar(message, { variant: 'error' });
setErrorMessage(message);