Skip to content

Commit

Permalink
Improve error messages for passkey errors (#2445)
Browse files Browse the repository at this point in the history
We have users complaining about errors that are hard to diagnose because
we don't know the source (e.g. "internal error").

This PR adds helpful context to errors thrown out of passkey interactions.
  • Loading branch information
frederikrothenberger committed Apr 25, 2024
1 parent e01fbd5 commit e05a47a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/frontend/src/utils/iiConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from "$generated/internet_identity_types";
import { fromMnemonicWithoutValidation } from "$src/crypto/ed25519";
import { features } from "$src/features";
import { diagnosticInfo, unknownToString } from "$src/utils/utils";
import {
Actor,
ActorSubclass,
Expand Down Expand Up @@ -235,7 +236,12 @@ export class Connection {
return { kind: "webAuthnFailed" };
}

throw e;
throw new Error(
`Failed to authenticate using passkey: ${unknownToString(
e,
"unknown error"
)}, ${await diagnosticInfo()}`
);
}

const actor = await this.createActor(delegationIdentity);
Expand Down
7 changes: 7 additions & 0 deletions src/frontend/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export function unknownToString(obj: unknown, def: string): string {
return def;
}

/** Collect information helpful to diagnose errors */
export async function diagnosticInfo(): Promise<string> {
return `user-agent: "${
navigator.userAgent
}", is platform auth available: ${await window?.PublicKeyCredential?.isUserVerifyingPlatformAuthenticatorAvailable()}`;
}

// Helper to gain access to the event's target
export const withInputElement = <E extends Event>(
evnt: E,
Expand Down
12 changes: 11 additions & 1 deletion src/frontend/src/utils/webAuthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DummyIdentity,
IIWebAuthnIdentity,
} from "$src/utils/iiConnection";
import { diagnosticInfo, unknownToString } from "$src/utils/utils";
import { WebAuthnIdentity } from "@dfinity/identity";
import { isNullish } from "@dfinity/utils";

Expand All @@ -24,5 +25,14 @@ export const constructIdentity = async ({
? () => Promise.resolve(new DummyIdentity())
: () => WebAuthnIdentity.create({ publicKey: opts });

return createIdentity();
try {
return createIdentity();
} catch (e: unknown) {
throw new Error(
`Failed to create passkey: ${unknownToString(
e,
"unknown error"
)}, ${await diagnosticInfo()}`
);
}
};

0 comments on commit e05a47a

Please sign in to comment.