Skip to Content
Configuring the client

Errors

Where Session kit's errors appear, how to show them to users, and the storage failures that need your decision.

Sooner or later a refresh fails because your backend is down, or the browser refuses to save the session. Your app needs to notice, and tell the user something useful. Every error from Session kit's own logic is an AuthError, with a code you can match on.

This page shows where errors appear, how to turn them into messages, and the failures that need a decision from you.

Where errors appear

A failure shows up in one of two places, or in both:

  • The call rejects when you asked for something the client couldn't do, such as signIn with a user your schema rejects.
  • state.error shows it when the session itself is affected, such as a refresh that failed. Your components read it with useAuth, so it reaches the screen even when no call of yours was waiting.

Here's where each common failure appears:

When…The call rejectsstate.error shows it
signIn or updateUser gets a user the schema rejectsYesNo
unlock() fails with UNLOCK_FAILEDYesNo
An early refresh fails in the backgroundNo call is waitingYes
A refresh the session needed failsYes, every call waiting for it, such as getSession() or authFetchYes
The saved session can't be readYes, every call that needs it, such as getSession() or retry()Yes
The session can't be savedYes, the call that saved it, such as signInYes
The saved session can't be cleared on sign-outYes, signOut()Yes
revoke fails after a sign-outNo: signOut() resolvesYes, REVOKE_FAILED
Another tab saves a user this tab's schema rejectsNo call is waitingYes

state.error holds the latest failure. The next successful change clears it, such as a refresh that works, a sign-in or a sign-out. While a retry runs, the error stays until the retry succeeds.

Show errors to users

Match on an error's code, not its message, which can change in any release. Error codes lists every code and what it means. To keep wording out of your components, map codes to your own messages:

src/error-message.ts
import { , type  } from "@monarcode/session-kit";

const : <<, string>> = {
  : "We can't reach the server. Check your connection.",
  : "We couldn't save or clear your session.",
  : "Sign in to continue.",
};

export function (: unknown): string {
  if ( instanceof ) return [.] ?? .;
  return  instanceof  ? . : "Something went wrong";
}

Then show state.error near the top of your app, with a way to try again when access is unavailable:

src/session-alert.tsx
import { ,  } from "@monarcode/session-kit/react";

import {  } from "./error-message.js";

export function () {
  const { ,  } = ();
  const  = ();
  if (!) return null;
  return (
    < ="alert">
      <>{()}</>
      { === "unavailable" && (
        < ={() => void .().(() => {})}>
          Try again
        </>
      )}
    </>
  );
}

retry() rejects again if it fails, and the new failure shows in state.error, so the button can ignore the rejection.

An error's cause holds whatever failed underneath, such as the error your refresh callback threw. Log it, or send it to your error reporting.

A few failures pass through unchanged, so they usually aren't AuthErrors, and errorMessage shows their own message:

  • authFetch passes on the failures of fetch and Request, such as a network error.
  • With server rendering, the browser client's signIn passes on what your sign-in function threw.

Handle storage failures

Storage can fail: a full disk, a browser that blocks storage, or an iOS keychain that's locked. Each of these three failures needs a decision from you:

  • Storage can't be read. status becomes unavailable, with the error in state.error. The saved session stays as it is, and the next getSession() or retry() reads it again. Show a Try again button that calls retry(), as SessionAlert does.
  • Storage can't be written. The client signs the user out, clears the saved session, and publishes the error. That way, no one stays signed in to a session that would vanish on reload. This happens on a refresh too, when its new tokens can't be saved: on iOS, a refresh while the keychain is locked signs the user out. Show the error, and let the user sign in again.
  • Storage can't be cleared. Sign-out still takes effect in this tab, but signOut() rejects and state.error keeps the error. The saved credentials could restore the session on reload, so call signOut() again after storage works again.

Saved sessions the client can't use

Sometimes storage works, but the session it holds can't be used. When the client restores such a session, it signs the user out and clears it:

  • A malformed saved session, such as one edited by hand, gives INVALID_SESSION in state.error.
  • A saved user your schema rejects, such as after a deploy that changed the schema, gives USER_VALIDATION_FAILED in state.error. See User schemas to avoid it.
  • A session past maxAge, or saved in another format by an older or newer version of Session kit, signs out with no error. Running out is routine, as Storage explains.

In each case, the user signs in again.

Next steps

  • Error codes: what each code means, and which calls give it.
  • Refresh: what happens when a refresh fails, and how long the failure lasts.
  • Debugging: log what the client did before an error appeared.

On this page