SDK errors
Every error an SDK throws or returns is an instance of a class below. Branch with the type guard, then on code:
never on message, which is for people. The code of an API error is one of the error codes.
Browser: @zautha/js and @zautha/react
Section titled “Browser: @zautha/js and @zautha/react”@zautha/react and @zautha/nextjs re-export these classes and guards, so you can import them from the package you use.
| Class | Guard | When |
|---|---|---|
ZauthaError |
isZauthaError |
the base of all of them |
ZauthaApiError |
isZauthaApiError(error, code?) |
the API answered with a problem: status, code, fieldErrors, retryAfterSeconds, traceId |
ZauthaNetworkError |
isZauthaNetworkError |
the request never produced a response (DNS, TLS, connection reset, CORS) |
ZauthaOfflineError |
isZauthaOfflineError |
getToken() needed a fresh token and the network was unavailable |
ZauthaCaptchaError |
isZauthaCaptchaError |
the bot check could not run: code is timeout, unsupported, closed, replaced or Turnstile’s own |
ZauthaPasskeyError |
isZauthaPasskeyError(error, code?) |
a passkey ceremony ended without a credential: passkey_cancelled, passkey_failed, passkey_expired |
ReverificationRequiredError |
isReverificationRequiredError |
a sensitive action needs a recent identity check: requirement.level and requirement.afterMinutes |
ReverificationCancelledError |
isReverificationCancelledError |
the user closed the reverification modal |
import { isZauthaApiError, isZauthaNetworkError, isZauthaOfflineError, type ZauthaError } from "@zautha/js";
export function describe(error: ZauthaError): string { if (isZauthaApiError(error, "form_password_incorrect")) return "Wrong password."; if (isZauthaApiError(error)) return `${error.code} (trace ${error.traceId ?? "none"})`; if (isZauthaNetworkError(error) || isZauthaOfflineError(error)) return "You appear to be offline."; return error.message;}The sign-in and sign-up resources do not throw: they return { error } and fill errors.fields and errors.global
(see Custom sign-in flow). ZauthaApiError.fieldErrors lists the field problems of
one response.
Server: @zautha/backend and @zautha/nextjs
Section titled “Server: @zautha/backend and @zautha/nextjs”@zautha/nextjs/server re-exports the first three; @zautha/nextjs/webhooks the webhook ones.
| Class | Guard | When |
|---|---|---|
TokenVerificationError |
isTokenVerificationError |
a session token was rejected: code is token_expired, token_invalid, token_azp_invalid, token_iss_invalid or jwks_unavailable |
ZauthaApiError |
isZauthaApiError(error, code?) |
the Backend API answered with an error (this class is @zautha/backend’s own: status, code, errors, traceId, retryAfterSeconds) |
ZauthaConfigurationError |
none | the SDK was set up wrongly (malformed key, empty authorizedParties, missing middleware) |
ZauthaWebhookVerificationError |
isZauthaWebhookVerificationError |
a webhook request was refused: missing_headers, invalid_signature or timestamp_out_of_tolerance |
import { createZauthaClient, isTokenVerificationError, isZauthaApiError, verifyToken } from "@zautha/backend";
export async function userIdOf(token: string): Promise<string | null> { try { const claims = await verifyToken(token, { publishableKey: "pk_test_…", authorizedParties: ["https://app.example.com"], }); return claims.sub; } catch (error) { if (isTokenVerificationError(error)) return null; // signed out throw error; // a ZauthaConfigurationError is a mistake to fix, never a signed-out user }}
export async function userExists(id: string): Promise<boolean> { const zautha = createZauthaClient({ secretKey: "sk_test_…" }); try { await zautha.users.get(id); return true; } catch (error) { if (isZauthaApiError(error, "resource_not_found")) return false; throw error; }}A ZauthaConfigurationError is never turned into a signed-out state: it surfaces as an exception so a mistake is visible
instead of logging everyone out. A network failure or timeout of a Backend API call rejects with the fetch error.