mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
update axioserror
This commit is contained in:
@@ -9,6 +9,8 @@ import * as AuthenticationApi from './api/authentication';
|
|||||||
import { PROJECT_NAME } from './api/env';
|
import { PROJECT_NAME } from './api/env';
|
||||||
import { AuthenticationContext } from './contexts/authentication';
|
import { AuthenticationContext } from './contexts/authentication';
|
||||||
|
|
||||||
|
import { AxiosError } from 'axios';
|
||||||
|
|
||||||
import { extractErrorMessage, onEnterCallback, updateValue } from './utils';
|
import { extractErrorMessage, onEnterCallback, updateValue } from './utils';
|
||||||
import { SignInRequest } from './types';
|
import { SignInRequest } from './types';
|
||||||
import { ValidatedTextField } from './components';
|
import { ValidatedTextField } from './components';
|
||||||
@@ -42,9 +44,11 @@ const SignIn: FC = () => {
|
|||||||
try {
|
try {
|
||||||
const { data: loginResponse } = await AuthenticationApi.signIn(signInRequest);
|
const { data: loginResponse } = await AuthenticationApi.signIn(signInRequest);
|
||||||
authenticationContext.signIn(loginResponse.access_token);
|
authenticationContext.signIn(loginResponse.access_token);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
if (error.response?.status === 401) {
|
if (error instanceof AxiosError) {
|
||||||
enqueueSnackbar('Invalid login details', { variant: 'warning' });
|
if (error.response?.status === 401) {
|
||||||
|
enqueueSnackbar('Invalid login details', { variant: 'warning' });
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
enqueueSnackbar(extractErrorMessage(error, 'Unexpected error, please try again'), { variant: 'error' });
|
enqueueSnackbar(extractErrorMessage(error, 'Unexpected error, please try again'), { variant: 'error' });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,13 +24,19 @@ const getBorderColor = (theme: Theme, props: DropzoneState) => {
|
|||||||
export interface SingleUploadProps {
|
export interface SingleUploadProps {
|
||||||
onDrop: (acceptedFiles: File[]) => void;
|
onDrop: (acceptedFiles: File[]) => void;
|
||||||
onCancel: () => void;
|
onCancel: () => void;
|
||||||
accept?: string | string[];
|
|
||||||
uploading: boolean;
|
uploading: boolean;
|
||||||
progress?: ProgressEvent;
|
progress?: ProgressEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SingleUpload: FC<SingleUploadProps> = ({ onDrop, onCancel, accept, uploading, progress }) => {
|
const SingleUpload: FC<SingleUploadProps> = ({ onDrop, onCancel, uploading, progress }) => {
|
||||||
const dropzoneState = useDropzone({ onDrop, accept, disabled: uploading, multiple: false });
|
const dropzoneState = useDropzone({
|
||||||
|
onDrop,
|
||||||
|
accept: {
|
||||||
|
'application/octet-stream': ['.bin']
|
||||||
|
},
|
||||||
|
disabled: uploading,
|
||||||
|
multiple: false
|
||||||
|
});
|
||||||
const { getRootProps, getInputProps } = dropzoneState;
|
const { getRootProps, getInputProps } = dropzoneState;
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ const useFileUpload = ({ upload }: MediaUploadOptions) => {
|
|||||||
});
|
});
|
||||||
resetUploadingStates();
|
resetUploadingStates();
|
||||||
enqueueSnackbar('Upload successful', { variant: 'success' });
|
enqueueSnackbar('Upload successful', { variant: 'success' });
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
if (axios.isCancel(error)) {
|
if (axios.isCancel(error)) {
|
||||||
enqueueSnackbar('Upload aborted', { variant: 'warning' });
|
enqueueSnackbar('Upload aborted', { variant: 'warning' });
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ const Authentication: FC = ({ children }) => {
|
|||||||
const decodedMe = AuthenticationApi.decodeMeJWT(accessToken);
|
const decodedMe = AuthenticationApi.decodeMeJWT(accessToken);
|
||||||
setMe(decodedMe);
|
setMe(decodedMe);
|
||||||
enqueueSnackbar(`Logged in as ${decodedMe.username}`, { variant: 'success' });
|
enqueueSnackbar(`Logged in as ${decodedMe.username}`, { variant: 'success' });
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
setMe(undefined);
|
setMe(undefined);
|
||||||
throw new Error('Failed to parse JWT ' + error.message);
|
throw new Error('Failed to parse JWT');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ const Authentication: FC = ({ children }) => {
|
|||||||
await AuthenticationApi.verifyAuthorization();
|
await AuthenticationApi.verifyAuthorization();
|
||||||
setMe(AuthenticationApi.decodeMeJWT(accessToken));
|
setMe(AuthenticationApi.decodeMeJWT(accessToken));
|
||||||
setInitialized(true);
|
setInitialized(true);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
setMe(undefined);
|
setMe(undefined);
|
||||||
setInitialized(true);
|
setInitialized(true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ const FeaturesLoader: FC = (props) => {
|
|||||||
try {
|
try {
|
||||||
const response = await FeaturesApi.readFeatures();
|
const response = await FeaturesApi.readFeatures();
|
||||||
setFeatures(response.data);
|
setFeatures(response.data);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
setErrorMessage(extractErrorMessage(error, 'Failed to fetch application details.'));
|
setErrorMessage(extractErrorMessage(error, 'Failed to fetch application details.'));
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import { useEffect, FC, useState, useCallback, useRef } from 'react';
|
import { useEffect, FC, useState, useCallback, useRef } from 'react';
|
||||||
import { useSnackbar } from 'notistack';
|
import { useSnackbar } from 'notistack';
|
||||||
|
|
||||||
|
import { AxiosError } from 'axios';
|
||||||
|
|
||||||
import { Button } from '@mui/material';
|
import { Button } from '@mui/material';
|
||||||
import PermScanWifiIcon from '@mui/icons-material/PermScanWifi';
|
import PermScanWifiIcon from '@mui/icons-material/PermScanWifi';
|
||||||
|
|
||||||
import * as NetworkApi from '../../api/network';
|
import * as NetworkApi from '../../api/network';
|
||||||
import { WiFiNetwork, WiFiNetworkList } from '../../types';
|
import { WiFiNetwork, WiFiNetworkList } from '../../types';
|
||||||
import { ButtonRow, FormLoader, SectionContent } from '../../components';
|
import { ButtonRow, FormLoader, SectionContent } from '../../components';
|
||||||
import { extractErrorMessage } from '../../utils';
|
|
||||||
|
|
||||||
import WiFiNetworkSelector from './WiFiNetworkSelector';
|
import WiFiNetworkSelector from './WiFiNetworkSelector';
|
||||||
|
|
||||||
@@ -52,8 +53,12 @@ const WiFiNetworkScanner: FC = () => {
|
|||||||
newNetworkList.networks.sort(compareNetworks);
|
newNetworkList.networks.sort(compareNetworks);
|
||||||
setNetworkList(newNetworkList);
|
setNetworkList(newNetworkList);
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
finishedWithError(extractErrorMessage(error, 'Problem listing WiFi networks'));
|
if (error instanceof AxiosError) {
|
||||||
|
finishedWithError('Problem listing WiFi networks ' + error.response?.data.message);
|
||||||
|
} else {
|
||||||
|
finishedWithError('Problem listing WiFi networks');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [finishedWithError]);
|
}, [finishedWithError]);
|
||||||
|
|
||||||
@@ -64,8 +69,12 @@ const WiFiNetworkScanner: FC = () => {
|
|||||||
try {
|
try {
|
||||||
await NetworkApi.scanNetworks();
|
await NetworkApi.scanNetworks();
|
||||||
setTimeout(pollNetworkList, POLLING_FREQUENCY);
|
setTimeout(pollNetworkList, POLLING_FREQUENCY);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
finishedWithError(extractErrorMessage(error, 'Problem scanning for WiFi networks'));
|
if (error instanceof AxiosError) {
|
||||||
|
finishedWithError('Problem scanning for WiFi networks ' + error.response?.data.message);
|
||||||
|
} else {
|
||||||
|
finishedWithError('Problem scanning for WiFi networks');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [finishedWithError, pollNetworkList]);
|
}, [finishedWithError, pollNetworkList]);
|
||||||
|
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ const NTPStatusForm: FC = () => {
|
|||||||
enqueueSnackbar('Time set', { variant: 'success' });
|
enqueueSnackbar('Time set', { variant: 'success' });
|
||||||
setSettingTime(false);
|
setSettingTime(false);
|
||||||
loadData();
|
loadData();
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
enqueueSnackbar(extractErrorMessage(error, 'Problem updating time'), { variant: 'error' });
|
enqueueSnackbar(extractErrorMessage(error, 'Problem updating time'), { variant: 'error' });
|
||||||
} finally {
|
} finally {
|
||||||
setProcessing(false);
|
setProcessing(false);
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ const GenerateToken: FC<GenerateTokenProps> = ({ username, onClose }) => {
|
|||||||
const getToken = useCallback(async () => {
|
const getToken = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
setToken((await SecurityApi.generateToken(username)).data);
|
setToken((await SecurityApi.generateToken(username)).data);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
enqueueSnackbar(extractErrorMessage(error, 'Problem generating token'), { variant: 'error' });
|
enqueueSnackbar(extractErrorMessage(error, 'Problem generating token'), { variant: 'error' });
|
||||||
}
|
}
|
||||||
}, [username, enqueueSnackbar]);
|
}, [username, enqueueSnackbar]);
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ const FirmwareFileUpload: FC<UploadFirmwareProps> = ({ uploadFirmware }) => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<SingleUpload
|
<SingleUpload
|
||||||
accept=".bin"
|
|
||||||
onDrop={uploadFile}
|
onDrop={uploadFile}
|
||||||
onCancel={cancelUpload}
|
onCancel={cancelUpload}
|
||||||
uploading={uploading}
|
uploading={uploading}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const FirmwareRestartMonitor: FC = () => {
|
|||||||
try {
|
try {
|
||||||
await SystemApi.readSystemStatus(POLL_TIMEOUT);
|
await SystemApi.readSystemStatus(POLL_TIMEOUT);
|
||||||
document.location.href = '/firmwareUpdated';
|
document.location.href = '/firmwareUpdated';
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
if (new Date().getTime() < timeoutAt.current) {
|
if (new Date().getTime() < timeoutAt.current) {
|
||||||
setTimeoutId(setTimeout(poll.current, POLL_INTERVAL));
|
setTimeoutId(setTimeout(poll.current, POLL_INTERVAL));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ const SystemLog: FC = () => {
|
|||||||
if (response.status !== 200) {
|
if (response.status !== 200) {
|
||||||
enqueueSnackbar('Problem applying log settings', { variant: 'error' });
|
enqueueSnackbar('Problem applying log settings', { variant: 'error' });
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
enqueueSnackbar(extractErrorMessage(error, 'Problem applying log settings'), { variant: 'error' });
|
enqueueSnackbar(extractErrorMessage(error, 'Problem applying log settings'), { variant: 'error' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -158,7 +158,7 @@ const SystemLog: FC = () => {
|
|||||||
const fetchLog = useCallback(async () => {
|
const fetchLog = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
setLogEntries((await SystemApi.readLogEntries()).data);
|
setLogEntries((await SystemApi.readLogEntries()).data);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
setErrorMessage(extractErrorMessage(error, 'Failed to fetch log'));
|
setErrorMessage(extractErrorMessage(error, 'Failed to fetch log'));
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ const SystemStatusForm: FC = () => {
|
|||||||
try {
|
try {
|
||||||
await SystemApi.restart();
|
await SystemApi.restart();
|
||||||
enqueueSnackbar('EMS-ESP is restarting...', { variant: 'info' });
|
enqueueSnackbar('EMS-ESP is restarting...', { variant: 'info' });
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
enqueueSnackbar(extractErrorMessage(error, 'Problem restarting device'), { variant: 'error' });
|
enqueueSnackbar(extractErrorMessage(error, 'Problem restarting device'), { variant: 'error' });
|
||||||
} finally {
|
} finally {
|
||||||
setConfirmRestart(false);
|
setConfirmRestart(false);
|
||||||
@@ -179,7 +179,7 @@ const SystemStatusForm: FC = () => {
|
|||||||
try {
|
try {
|
||||||
await SystemApi.factoryReset();
|
await SystemApi.factoryReset();
|
||||||
enqueueSnackbar('Device has been factory reset and will now restart', { variant: 'info' });
|
enqueueSnackbar('Device has been factory reset and will now restart', { variant: 'info' });
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
enqueueSnackbar(extractErrorMessage(error, 'Problem factory resetting the device'), { variant: 'error' });
|
enqueueSnackbar(extractErrorMessage(error, 'Problem factory resetting the device'), { variant: 'error' });
|
||||||
} finally {
|
} finally {
|
||||||
setConfirmFactoryReset(false);
|
setConfirmFactoryReset(false);
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ const HelpInformation: FC = () => {
|
|||||||
document.body.removeChild(a);
|
document.body.removeChild(a);
|
||||||
enqueueSnackbar('File downloaded', { variant: 'info' });
|
enqueueSnackbar('File downloaded', { variant: 'info' });
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
enqueueSnackbar(extractErrorMessage(error, 'Problem with downloading'), { variant: 'error' });
|
enqueueSnackbar(extractErrorMessage(error, 'Problem with downloading'), { variant: 'error' });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ const SettingsApplication: FC = () => {
|
|||||||
eth_clock_mode: response.data.eth_clock_mode
|
eth_clock_mode: response.data.eth_clock_mode
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
enqueueSnackbar(extractErrorMessage(error, 'Problem fetching board profile'), { variant: 'error' });
|
enqueueSnackbar(extractErrorMessage(error, 'Problem fetching board profile'), { variant: 'error' });
|
||||||
} finally {
|
} finally {
|
||||||
setProcessingBoard(false);
|
setProcessingBoard(false);
|
||||||
@@ -103,7 +103,7 @@ const SettingsApplication: FC = () => {
|
|||||||
try {
|
try {
|
||||||
await EMSESP.restart();
|
await EMSESP.restart();
|
||||||
enqueueSnackbar('EMS-ESP is restarting...', { variant: 'info' });
|
enqueueSnackbar('EMS-ESP is restarting...', { variant: 'info' });
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
enqueueSnackbar(extractErrorMessage(error, 'Problem restarting device'), { variant: 'error' });
|
enqueueSnackbar(extractErrorMessage(error, 'Problem restarting device'), { variant: 'error' });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
import { AxiosError } from 'axios';
|
import { AxiosError } from 'axios';
|
||||||
|
|
||||||
export const extractErrorMessage = (error: AxiosError, defaultMessage: string) =>
|
export const extractErrorMessage = (error: unknown, defaultMessage: string) => {
|
||||||
(error.response && error.response.data ? error.response.data.message : error.message) || defaultMessage;
|
if (error instanceof AxiosError) {
|
||||||
|
return error.response && error.response.data && error?.response?.data?.message;
|
||||||
|
} else if (error instanceof Error) {
|
||||||
|
return error.message;
|
||||||
|
}
|
||||||
|
return defaultMessage;
|
||||||
|
};
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ export const useRest = <D>({ read, update }: RestRequestOptions<D>) => {
|
|||||||
setErrorMessage(undefined);
|
setErrorMessage(undefined);
|
||||||
try {
|
try {
|
||||||
setData((await read()).data);
|
setData((await read()).data);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
const message = extractErrorMessage(error, 'Problem loading data');
|
const message = extractErrorMessage(error, 'Problem loading data');
|
||||||
enqueueSnackbar(message, { variant: 'error' });
|
enqueueSnackbar(message, { variant: 'error' });
|
||||||
setErrorMessage(message);
|
setErrorMessage(message);
|
||||||
@@ -45,7 +45,7 @@ export const useRest = <D>({ read, update }: RestRequestOptions<D>) => {
|
|||||||
} else {
|
} else {
|
||||||
enqueueSnackbar('Settings saved', { variant: 'success' });
|
enqueueSnackbar('Settings saved', { variant: 'success' });
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
const message = extractErrorMessage(error, 'Problem saving data');
|
const message = extractErrorMessage(error, 'Problem saving data');
|
||||||
enqueueSnackbar(message, { variant: 'error' });
|
enqueueSnackbar(message, { variant: 'error' });
|
||||||
setErrorMessage(message);
|
setErrorMessage(message);
|
||||||
|
|||||||
Reference in New Issue
Block a user