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
signInwith a user your schema rejects. state.errorshows it when the session itself is affected, such as a refresh that failed. Your components read it withuseAuth, so it reaches the screen even when no call of yours was waiting.
Here's where each common failure appears:
| When… | The call rejects | state.error shows it |
|---|---|---|
signIn or updateUser gets a user the schema rejects | Yes | No |
unlock() fails with UNLOCK_FAILED | Yes | No |
| An early refresh fails in the background | No call is waiting | Yes |
| A refresh the session needed fails | Yes, every call waiting for it, such as getSession() or authFetch | Yes |
| The saved session can't be read | Yes, every call that needs it, such as getSession() or retry() | Yes |
| The session can't be saved | Yes, the call that saved it, such as signIn | Yes |
| The saved session can't be cleared on sign-out | Yes, signOut() | Yes |
revoke fails after a sign-out | No: signOut() resolves | Yes, REVOKE_FAILED |
| Another tab saves a user this tab's schema rejects | No call is waiting | Yes |
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:
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:
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:
authFetchpasses on the failures offetchandRequest, such as a network error.- With server rendering, the browser client's
signInpasses 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.
statusbecomesunavailable, with the error instate.error. The saved session stays as it is, and the nextgetSession()orretry()reads it again. Show a Try again button that callsretry(), asSessionAlertdoes. - 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 andstate.errorkeeps the error. The saved credentials could restore the session on reload, so callsignOut()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_SESSIONinstate.error. - A saved user your schema rejects, such as after a deploy that changed
the schema, gives
USER_VALIDATION_FAILEDinstate.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.