summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src/helpers/webAuthnError.ts
diff options
context:
space:
mode:
authorMatthew Miller <matthew@millerti.me>2023-03-15 18:46:41 -0700
committerGitHub <noreply@github.com>2023-03-15 18:46:41 -0700
commitcc6b4e83d0c8087f78c1851a5b0f60a1d7223ec6 (patch)
treeca62fa0690780c7dace0d3a956e10110c5d5e277 /packages/browser/src/helpers/webAuthnError.ts
parent70f7e79b24237308398334df834dd4cf2d398512 (diff)
parentac97005f39974154dc2af9514294bbe217a252f6 (diff)
Merge pull request #367 from MasterKale/feat/better-errors
feat/better-errors
Diffstat (limited to 'packages/browser/src/helpers/webAuthnError.ts')
-rw-r--r--packages/browser/src/helpers/webAuthnError.ts56
1 files changed, 56 insertions, 0 deletions
diff --git a/packages/browser/src/helpers/webAuthnError.ts b/packages/browser/src/helpers/webAuthnError.ts
new file mode 100644
index 0000000..1debec0
--- /dev/null
+++ b/packages/browser/src/helpers/webAuthnError.ts
@@ -0,0 +1,56 @@
+/* eslint-disable @typescript-eslint/ban-ts-comment */
+/**
+ * A custom Error used to return a more nuanced error detailing _why_ one of the eight documented
+ * errors in the spec was raised after calling `navigator.credentials.create()` or
+ * `navigator.credentials.get()`:
+ *
+ * - `AbortError`
+ * - `ConstraintError`
+ * - `InvalidStateError`
+ * - `NotAllowedError`
+ * - `NotSupportedError`
+ * - `SecurityError`
+ * - `TypeError`
+ * - `UnknownError`
+ *
+ * Error messages were determined through investigation of the spec to determine under which
+ * scenarios a given error would be raised.
+ */
+export class WebAuthnError extends Error {
+ code: WebAuthnErrorCode;
+
+ constructor({
+ message,
+ code,
+ cause,
+ name,
+ }: {
+ message: string,
+ code: WebAuthnErrorCode,
+ cause: Error,
+ name?: string,
+ }) {
+ /**
+ * `cause` is supported in evergreen browsers, but not IE10, so this ts-ignore is to
+ * help Rollup complete the ES5 build.
+ */
+ // @ts-ignore
+ super(message, { cause })
+ this.name = name ?? cause.name;
+ this.code = code;
+ }
+}
+
+export type WebAuthnErrorCode =
+ 'ERROR_CEREMONY_ABORTED'
+ | 'ERROR_INVALID_DOMAIN'
+ | 'ERROR_INVALID_RP_ID'
+ | 'ERROR_INVALID_USER_ID_LENGTH'
+ | 'ERROR_MALFORMED_PUBKEYCREDPARAMS'
+ | 'ERROR_AUTHENTICATOR_GENERAL_ERROR'
+ | 'ERROR_AUTHENTICATOR_MISSING_DISCOVERABLE_CREDENTIAL_SUPPORT'
+ | 'ERROR_AUTHENTICATOR_MISSING_USER_VERIFICATION_SUPPORT'
+ | 'ERROR_AUTHENTICATOR_PREVIOUSLY_REGISTERED'
+ | 'ERROR_AUTHENTICATOR_NO_SUPPORTED_PUBKEYCREDPARAMS_ALG'
+ | 'ERROR_PASSTHROUGH_SEE_CAUSE_PROPERTY'
+ ;