Error Codes and Error Handling
How errors reach users (UserFacingError, DB-code masking, formatError) and the reference catalog of error codes.
5 min read
Runtime error handling (web/src/lib/errors.ts)
This is what actually runs today. The catalog further down is separate reference data.

UserFacingError
throw new UserFacingError("...") marks a message that was written on purpose for the end user
(auth failures, "X not found", validation). Server actions and routes pass message through
for these and replace every other error with a generic one (SALO-1993). It replaced brittle
regex allow-lists that silently masked any newly thrown legitimate message. Example call sites:
requireCurrentUserId()/requireCallerOrgId() in lib/auth/tenant.ts, and the
err instanceof UserFacingError ? err.message : fallback pattern in lib/audit/actions.ts.
formatError(err): masking database errors
Raw Supabase/Postgres/PostgREST messages can carry table, column, constraint, enum and
RLS-policy names, so formatError converts anything that looks like schema detail into a
generic message and logs the original server-side (console.error("[DB ERROR]", ...), with
code, details and hint). It fails closed:
- If the error has a Postgres SQLSTATE or a PostgREST
PGRST*code, it is masked, using a friendly message where one is known (CODE_MESSAGES) or "An unexpected database error occurred." otherwise. - The one exception is SQLSTATE
P0001(raise exceptionin our own SQL). Those messages are written for users and pass through unchanged. - Errors that lost their
code(for examplethrow new Error(dbError.message)) are matched by message shape (MESSAGE_RULES, e.g. "violates unique constraint") and then by schema-shaped patterns (relation "...",column "...",schema cache,syntax error). - Anything else is returned as is. An empty or non-descriptive error becomes "An unexpected error occurred."
| Code | User sees |
|---|---|
23505 |
This record already exists or conflicts. |
23503 |
Reference not found. |
23502 |
A required value is missing. |
23514, 22P02 |
Invalid data provided. |
23P01 |
This conflicts with an existing record. |
22001 |
A value is too long. |
22003 |
A number is out of range. |
42501 |
Permission denied. |
PGRST116 |
Record not found. |
Use formatError anywhere an action or route hands error.message back to the client. Never
return a raw error.message from Supabase. errorMessage(e) returns a best-effort message
from anything thrown but does not mask, so it is for logging only.
formatActionError and ErrorCode (SALO-445)
formatActionError(err, defaultCode) returns { error, code }, where code is one of
UNAUTHENTICATED, UNAUTHORIZED, NOT_FOUND, VALIDATION_FAILED, CONFLICT, RATE_LIMITED
or INTERNAL_ERROR. The code is inferred from keywords in the already-masked message, so it
is a heuristic. At the time of writing only errors.ts and its test reference it; no action or
route uses it yet. These ErrorCode values are unrelated to the error_codes catalog below.
Error code catalog (SALO-444)
A DB-backed reference catalog of error codes: public.error_codes (migration
0135_error_codes.sql). One row per distinct error condition — code,
category, http_status, message, is_dynamic, severity.
This is a reference catalog only — there is no occurrence log (no per-request logging table) in v1. The table just answers "what does code X mean, what category is it, what status/message go with it."
Status: what's actually done vs. not
- Done: the table + an initial seed of codes (231 per the original ticket; row count not re-verified), generated by scanning
every distinct
return { error: "..." },throw new Error("..."), andNextResponse.json({ error: "..." })string literal acrossweb/src/lib/**andweb/src/app/api/**. - Not done: none of the ~530 call sites that produce those messages have
been changed to use a code yet. They still throw/return the raw string
they always did. The catalog exists, but nothing in the app references it
yet — that's deliberately separate follow-up work (see below), because it
means touching ~150 files across every business domain (billing, staff,
bookings, inventory, ...), which deserves its own reviewed, staged effort
rather than a single sweep. (Re-checked: still true; nothing in
web/srcreadserror_codes.)
Categories
| Category | HTTP status | Meaning |
|---|---|---|
AUTH |
401 | Not signed in / bad session |
PERMISSION |
403 | Signed in, but not allowed to do this |
VALIDATION |
422 | Bad input — missing/invalid field, out-of-range value |
NOT_FOUND |
404 | Referenced record doesn't exist |
CONFLICT |
409 | Duplicate / already exists / already booked |
RATE_LIMIT |
429 | Too many attempts |
BUSINESS_RULE |
422 | Domain-specific rule violation (insufficient balance, invoice already paid, etc.) — the catch-all for messages that don't fit the categories above |
SYSTEM |
500 | Internal failure — misconfiguration, external API failure |
Code naming
<CATEGORY>_<SLUG_OF_MESSAGE>, e.g. NOT_FOUND_GIFT_CARD_NOT_FOUND. Slugs
are derived from the message text and truncated to the first few
significant words. Where two different exact-string messages collapse to
the same slug (e.g. differing only in trailing punctuation or case), a
numeric suffix (_2, _3, ...) disambiguates.
Messages that are template literals with interpolated values (e.g.
`Insufficient balance: ₹${available} available, ₹${requested} requested`)
have their ${...} expressions collapsed to a {value} placeholder and are
flagged is_dynamic = true. The message column holds the template, not a
live instance.
Known limitations of the v1 seed
This was generated by regex extraction, not a full read of every file, so:
- Categorization is heuristic, not hand-verified per row. Expect some
misclassified rows, especially in
BUSINESS_RULE(the catch-all) — correcting them is a directUPDATEon the table, no code change needed. - Only string-literal messages were captured. A minority of error sites build messages through variables/helpers rather than an inline string/template literal and were not captured by the sweep.
- Case/punctuation variants produce separate rows (e.g.
"Brand not found"vs"brand not found") rather than being merged. - Table is editable directly — this is meant to be curated over time by whoever owns each domain, not treated as generated/read-only output.
Regenerating the seed
The extraction script is not checked into the repo (it was a one-off audit
tool). To redo the sweep: regex-match return \{ error: (['"])(.?)\1, throw new Error((['"])(.*?)\3\), and
NextResponse\.json\(\{ error: (['"])(.?)\5acrossweb/src/lib//*.tsandweb/src/app/api//.ts(excluding.test.ts), dedupe by message text (after stripping trailing periods), collapse ${...}to{value}, categorize by keyword heuristics (see table above), and emit INSERT ... ON CONFLICT (code) DO NOTHING` so re-running is idempotent
against manually-curated rows.
Follow-up work (not part of SALO-444's initial pass)
- Thread codes through call sites. Replace the ~530 raw
return { error: "..." }/throw new Error(...)sites with a reference to the matchingerror_codes.code, likely by extendingweb/src/lib/errors.ts(anAppErrorhelper does not exist yet;UserFacingErrorandformatActionErrorabove are the closest existing pieces). Do this staged, by domain — not as one PR. - Wire codes to UI states. Once server actions/API routes return a
codeinstead of (or alongside) a raw message, the frontend can mapAUTH_*→SessionExpiredState,PERMISSION_*→PermissionDeniedState,VALIDATION_*→FieldError, etc. (the components from SALO-442/443). - Decide whether an occurrence log is actually needed later — deliberately out of scope for v1 per the "reference catalog only" decision.