Skip to content
Flare 0.1.x is pre-release. Expect breaking changes before 1.0 — see the changelog.

Errors

Flare models application failures with FlareError: stable symbolic names, category keys with default HTTP status codes, optional numeric codes, and optional typed detail payloads that can be serialized to JSON on client-safe paths when expose is true.

Import registry helpers and types from @flare-ts/core/errors. FlareError and FlareValidationError are also exported from @flare-ts/core when you only throw or catch instances and do not need the full errors subpath.

SymbolKindRole
FlareErrorclassThrown instance with name, category, optional code, and optional detail
flareErrorCodesfunctionBuilds a frozen, branded registry from category-grouped descriptors
errorSchemafunctionDeclares the JSON shape of an optional detail payload
FlareErrorCategoriesconstCategory keys and default HTTP status codes
FlareErrorCategorytypeUnion of valid category keys (keyof typeof FlareErrorCategories)
ErrorCodesTokentypeBranded marker on values returned by flareErrorCodes
CodeDescriptortypeShape of a registry entry (and FlareError constructor token)
ErrorSchematypePhantom marker returned by errorSchema<T>()
FlareValidationErrorclassThrown by host.build() when validators report severity: "error" entries
ValidationErrortypeShape of each entry on FlareValidationError.errors (code, message, optional hint, severity)
ValidationSeveritytype"error" | "warning" on build validation entries

Category keys are the keys of FlareErrorCategories (for example not_found, conflict). Use FlareErrorCategory when you need a category-typed parameter or variable. Values from flareErrorCodes(...) satisfy ErrorCodesToken; use that type when a helper accepts any registry without caring about its specific codes.

FlareError extends Error. The constructor takes a registry entry from flareErrorCodes (stamped with name and category when the registry is built) and, when that entry includes detail: errorSchema<T>(), a matching detail value as the second argument.

Thrown instances expose:

MemberDescription
nameSymbolic code name (also Error.message)
messageSame string as name (from Error)
categoryCategory key from the registry group
exposeWhen true, detail may leave the server on client-safe paths; when false, detail is undefined but exposedDetail still holds the value for logging
codeOptional stable numeric code from the descriptor
detailAttached detail when expose is true; otherwise undefined
exposedDetailAttached detail regardless of expose (logging and diagnostics)

FlareErrorCategories maps category keys to default status codes:

CategoryStatus
invalid400
too_large413
rejected422
unauthorized401
forbidden403
not_found404
conflict409
throttled429
unavailable503
fault500

Create a registry with flareErrorCodes(...) and throw new FlareError(...) from entries in that registry:

import {
FlareError,
errorSchema,
flareErrorCodes,
} from "@flare-ts/core/errors";
const UserErrors = flareErrorCodes({
not_found: {
UserNotFound: { expose: true, code: 1001 },
},
conflict: {
EmailTaken: {
expose: true,
code: 1002,
detail: errorSchema<{ email: string; }>(),
},
},
});
throw new FlareError(UserErrors.conflict.EmailTaken, { email: "a@b.com" });

Each registry entry declares expose, optional code, and optional detail. flareErrorCodes stamps each entry with its symbolic name (the object key) and category (the group key). Returned registries and stamped entries are frozen.

When you call flareErrorCodes, it validates the descriptor at runtime:

  • Unknown category keys → TypeError
  • Non-object entries, missing boolean expose, or non-safe-integer codeTypeError
  • Duplicate numeric code values across the registry → Error

errorSchema<T>() requires T to be JSON-serializable (JsonValue from @flare-ts/lib/schema). When a descriptor includes detail: errorSchema<T>(), the FlareError constructor requires a matching detail value as the second argument. Omit the second argument when the descriptor has no detail schema.

On entries returned from flareErrorCodes:

FieldDescription
nameSymbolic name (also used as Error.message when thrown)
categoryCategory key from the enclosing group
exposeWhether detail may leave the server in client-safe paths
codeOptional stable numeric code
detailOptional errorSchema<T>() marker (not the runtime payload)

CodeDescriptor describes this stamped shape for typing helpers and wrappers.

In HTTP execution, FlareError categories map to status codes through the category table above. For registration, default fallback bodies, and paths that skip custom handlers, see HTTP errors.

  • Failure modes: where errors surface across registration/build/compile/request
  • HTTP errors: pipeline error handling behavior