Files
pocket-id-pocket-id-1/frontend/src/lib/utils/error-util.ts

51 lines
1.7 KiB
TypeScript
Raw Normal View History

import { m } from '$lib/paraglide/messages';
2024-08-12 11:00:25 +02:00
import { WebAuthnError } from '@simplewebauthn/browser';
import { AxiosError } from 'axios';
import { toast } from 'svelte-sonner';
2025-01-19 15:30:31 +01:00
export function getAxiosErrorMessage(
e: unknown,
defaultMessage: string = m.an_unknown_error_occurred()
2025-01-19 15:30:31 +01:00
) {
let message = defaultMessage;
2024-08-12 11:00:25 +02:00
if (e instanceof AxiosError) {
message = e.response?.data.error || message;
}
2025-01-19 15:30:31 +01:00
return message;
}
export function axiosErrorToast(
e: unknown,
defaultMessage: string = m.an_unknown_error_occurred()
) {
2025-01-19 15:30:31 +01:00
const message = getAxiosErrorMessage(e, defaultMessage);
2024-08-12 11:00:25 +02:00
toast.error(message);
}
export function getWebauthnErrorMessage(e: unknown) {
const errors = {
ERROR_CEREMONY_ABORTED: m.authentication_process_was_aborted(),
ERROR_AUTHENTICATOR_GENERAL_ERROR: m.error_occurred_with_authenticator(),
2024-08-12 11:00:25 +02:00
ERROR_AUTHENTICATOR_MISSING_DISCOVERABLE_CREDENTIAL_SUPPORT:
m.authenticator_does_not_support_discoverable_credentials(),
2024-08-12 11:00:25 +02:00
ERROR_AUTHENTICATOR_MISSING_RESIDENT_KEY_SUPPORT:
m.authenticator_does_not_support_resident_keys(),
ERROR_AUTHENTICATOR_PREVIOUSLY_REGISTERED: m.passkey_was_previously_registered(),
2024-08-12 11:00:25 +02:00
ERROR_AUTHENTICATOR_NO_SUPPORTED_PUBKEYCREDPARAMS_ALG:
m.authenticator_does_not_support_any_of_the_requested_algorithms(),
ERROR_USER_DISABLED_MSG: m.user_disabled()
2024-08-12 11:00:25 +02:00
};
let message = m.an_unknown_error_occurred();
2024-08-12 11:00:25 +02:00
if (e instanceof WebAuthnError && e.code in errors) {
message = errors[e.code as keyof typeof errors];
} else if (e instanceof WebAuthnError && e?.message.includes('timed out')) {
message = m.authenticator_timed_out();
2024-08-12 11:00:25 +02:00
} else if (e instanceof AxiosError && e.response?.data.error) {
message = e.response?.data.error;
} else {
console.error(e);
}
return message;
}