improve error handling for getAuthCodes flow

This commit is contained in:
Abhinav 2023-04-05 17:34:18 +05:30
parent 863752eb56
commit fafaf90a19
3 changed files with 22 additions and 10 deletions

View file

@ -22,8 +22,6 @@ const AuthenticatorCodesPage = () => {
if (err.message === CustomError.KEY_MISSING) {
appContext.setRedirectURL(PAGES.AUTH);
router.push(PAGES.ROOT);
} else {
// do not log errors
}
}
};

View file

@ -4,6 +4,7 @@ import { Code } from 'types/authenticator/code';
import ComlinkCryptoWorker from 'utils/comlink/ComlinkCryptoWorker';
import { getEndpoint } from 'utils/common/apiUtil';
import { getActualKey, getToken } from 'utils/common/key';
import { CustomError } from 'utils/error';
import { logError } from 'utils/sentry';
const ENDPOINT = getEndpoint();
@ -12,11 +13,16 @@ export const getAuthCodes = async (): Promise<Code[]> => {
try {
const authKeyData = await getAuthKey();
const cryptoWorker = await ComlinkCryptoWorker.getInstance();
const authenticatorKey = await cryptoWorker.decryptB64(
authKeyData.encryptedKey,
authKeyData.header,
masterKey
);
let authenticatorKey: string;
try {
authenticatorKey = await cryptoWorker.decryptB64(
authKeyData.encryptedKey,
authKeyData.header,
masterKey
);
} catch (e) {
throw Error(CustomError.DECRYPTION_FAILED);
}
// always fetch all data from server for now
const authEntity: AuthEntity[] = await getDiff(0);
const authCodes = await Promise.all(
@ -56,7 +62,9 @@ export const getAuthCodes = async (): Promise<Code[]> => {
});
return authCodes;
} catch (e) {
logError(e, 'get authenticator entities failed');
if (e.message !== CustomError.AUTH_KEY_NOT_FOUND) {
logError(e, 'get authenticator entities failed');
}
throw e;
}
};
@ -72,8 +80,12 @@ export const getAuthKey = async (): Promise<AuthKey> => {
);
return resp.data;
} catch (e) {
logError(e, 'Get key failed');
throw e;
if (e.status === 404) {
throw Error(CustomError.AUTH_KEY_NOT_FOUND);
} else {
logError(e, 'Get key failed');
throw e;
}
}
};

View file

@ -58,6 +58,8 @@ export const CustomError = {
NO_EXPORT_FOLDER_SELECTED: 'no export folder selected',
EXPORT_FOLDER_DOES_NOT_EXIST: 'export folder does not exist',
NO_INTERNET_CONNECTION: 'no internet connection',
AUTH_KEY_NOT_FOUND: 'auth key not found',
DECRYPTION_FAILED: 'decryption failed',
};
export function parseUploadErrorCodes(error) {