Skip to content

Safe vs unsafe functions

When you import from @passlock/client or @passlock/server you’re pulling in unsafe functions i.e. functions that could throw. You’ll need to catch any errors, then use one of our type guards to narrow down the error:

import { registerPasskey, isDuplicatePasskeyError } from "@passlock/client";
try {
// result is the successful outcome
const result = await registerPasskey({ ... })
console.log("Registration success");
} catch (e) {
if (isDuplicatePasskeyError(e)) {
console.error("Duplicate passkey");
}
}

It’s easy to miss potential errors as they’re not reflected in the type system, only the JSDoc. We therefore expose (and recommend you use) safe variants:

import {
registerPasskey,
isRegistrationSuccess,
isDuplicatePasskeyError
} from "@passlock/client/safe";
// result is the successful outcome OR an error
const result = await registerPasskey({ ... })
if (isRegistrationSuccess(result)) {
console.log("Registration success");
} else if (isDuplicatePasskeyError(result)) {
console.error("Duplicate passkey");
}