# Standard error code catalogue

The values that appear in `CallError.code`: the standard hierarchy adapters emit codes under, the
contract on adapter authors, and the leaf codes the reference
adapters use. The matching semantics that consume these codes are
in [LCCO.8.3]; this
doc owns the namespace.

Paired with:
- [LCRS] for the `CallError`
  fact schema.
- [LCCO] for the
  hierarchical pattern-match grammar.
- [LADP] for adapter-
  side obligations more broadly.
- [LERR] for the meta-policy that tells
  feature designers when to surface errors as `CallError`
  vs other channels.

## What this is not

- Not a duplicate of the matching semantics. Pattern-match
  grammar lives in [LCCO.8.3]; this doc lists the values
  patterns match against.
- Not a runtime error catalogue. DS's own internal errors
  (compile-time, lowering, runtime invariant violations)
  are not `CallError.code` values; they live in
  [LERR].

---

(LECC-1)=
## Wire format [LECC.1]

(LECC-1-1)=
[LECC.1.1] `CallError.code` is a `String` per [LCRS.4.2.1].
Adapters must emit values matching the lexical shape below; the
engine treats codes as opaque strings on the wire and applies the
matching contract per [LCCO.8.3] at the rule-language layer.

*see also:* [LCCO.8.3](#LCCO-8-3), [LCRS.4.2.1](#LCRS-4-2-1)

(LECC-1-2)=
[LECC.1.2] A code value must match the regex
`[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)*`. Each dot-separated segment
is lower-snake-case and starts with a letter. Empty segments,
leading or trailing dots, uppercase letters, and hyphens are
forbidden.

*ratified*

(LECC-1-3)=
[LECC.1.3] The leftmost segment is the **family**. Subsequent
segments narrow within the family. A code with one segment is
malformed for adapter-emitted codes; adapters must always emit at
least `<family>.<leaf>` form so authors can match either by
family or by leaf per [LCCO.8.3.2].

*checks:* `alloy×2`  —  *see also:* [LCCO.8.3.2](#LCCO-8-3-2)

(LECC-1-5)=
[LECC.1.5] **The engine rewrites a malformed code it is handed.** When the host
hands the engine a `CallError` whose `code` does not match [LECC.1.2]'s pattern,
the engine must place it in working memory with `code` set to
`internal.invariant` ([LECC.3.7.2]) and every other slot as given, and must
count the rewrite in `ds.errors.code_invariant_total` ([NMET.4.1]). No rule sees
the malformed code.

The engine checks [LECC.1.2]'s pattern and nothing more. A code with one
segment, which [LECC.1.3] forbids an adapter to send, matches the pattern and
enters as given.

*ratified*  —  *see also:* [LECC.1.2](#LECC-1-2), [LECC.1.3](#LECC-1-3), [LECC.3.7.2](#LECC-3-7-2), [NMET.4.1](#NMET-4-1)

(LECC-1-4)=
[LECC.1.4] The maximum segment count is four. Codes deeper than
`<family>.<a>.<b>.<c>` are forbidden, and the compiler must refuse an `error`
pattern of more segments with `DS_VALIDATE_ERROR_PATTERN_TOO_DEEP`. Four levels suffice for the
families catalogued in §3 and bound the matching cost in
[LCCO.8.3.2].

---

*ratified*  —  *checks:* `alloy`  —  *see also:* [LCCO.8.3.2](#LCCO-8-3-2)

(LECC-2)=
## Standard families [LECC.2]

(LECC-2-1)=
[LECC.2.1] Adapters must emit `CallError.code` values whose
family is one of the families of [LECC.2.2], the one whose semantics
the failure matches.

*see also:* [LECC.2.2](#LECC-2-2)

(LECC-2-2)=
[LECC.2.2] The standard families are:

- `request.*` — the request was rejected at validation or
  dispatch, before the adapter attempted to contact the backend.
- `internal.*` — adapter or runtime bug. The failure is the
  adapter's fault, not the upstream's.
- `adapter.*` — the system the adapter fronts failed: it was
  unreachable, refused the request, throttled it or answered
  wrongly. The adapter chooses the leaf ([LECC.4.3]).

There are no other families.

*see also:* [LECC.4.3](#LECC-4-3)

(LECC-2-3)=
[LECC.2.3] An adapter must pick the most specific family that
applies. Arguments DS accepted but the adapter cannot use are
`request.invalid_args`, not `internal.bug` — the request was
well-formed at the DS boundary and wrong at the adapter's.
Anything about the upstream the adapter fronts belongs in
`adapter.*` ([LECC.4.3]), not stretched into `request` or
`internal` because one nearly fits.

*see also:* [LECC.4.3](#LECC-4-3)

(LECC-2-4)=
[LECC.2.4] When two codes could apply, adapters must prefer
the one that names the *cause* the rule author would want to
react to. A throttling response belongs under a single stable
`adapter.*` code ([LECC.4.3]) rather than being split
across several, because an author writing one `error` clause
wants to catch all throttling however it arrives.

---

*see also:* [LECC.4.3](#LECC-4-3)

(LECC-3)=
## Standard leaf codes [LECC.3]

The leaves below are normative. Adapters must use these names
when the failure matches the listed semantics. Adapters may add
new leaves under a standard family when the existing leaves do
not cover the case; new leaves must follow §1's lexical shape.

(LECC-3-1)=
### `request.*` [LECC.3.1]

(LECC-3-1-1)=
- [LECC.3.1.1] `request.unsupported` — the adapter does not
  offer the named service.

(LECC-3-1-2)=
- [LECC.3.1.2] `request.invalid_args` — the request's kwargs
  failed schema validation per [LADP.8].

*see also:* [LADP.8](#LADP-8)

(LECC-3-1-3)=
- [LECC.3.1.3] `request.malformed` — the adapter could not
  construct an outbound request from the call (e.g., a required
  slot was missing after substitution). Distinct from
  `request.invalid_args`, which is schema validation.

(LECC-3-1-4)=
- [LECC.3.1.4] `request.limit_exceeded` — the request exceeded
  an adapter-enforced cap (a result size, a query window).

(LECC-3-7)=
### `internal.*` [LECC.3.7]

(LECC-3-7-1)=
- [LECC.3.7.1] `internal.crash` — the adapter raised an
  unhandled exception while servicing the call.

(LECC-3-7-2)=
- [LECC.3.7.2] `internal.invariant` — the adapter detected a
  violated invariant during the call's lifecycle.

(LECC-3-7-3)=
- [LECC.3.7.3] `internal.bug` — catch-all for adapter logic
  errors that do not fit the prior two leaves. Adapters should
  prefer the specific leaves; `internal.bug` is the fallback.

---

(LECC-4)=
## The `adapter` family [LECC.4]

(LECC-4-3)=
[LECC.4.3] The adapter names the leaf under `adapter`
(`adapter.throttled`), in [LECC.1.2]'s shape. The rule runs both ways. An
adapter must not stretch `request` or `internal` to cover something their
semantics do not: `request.*` is about the request as it crossed the DS
boundary and `internal.*` about a bug in the adapter or the runtime, so
any failure of the system the adapter fronts is neither and
belongs under `adapter.*`. Equally, an adapter must not
mint an `adapter.*` name for a condition `request` or `internal` already
covers — malformed arguments are `request.malformed`, not
`adapter.bad_args`. Two names for one condition is what
this rule exists to prevent.

*see also:* [LECC.1.2](#LECC-1-2)

(LECC-5)=
## Engine-emitted codes [LECC.5]

(LECC-5-1)=
[LECC.5.1] Some codes are emitted by the engine itself rather
than by an adapter. These describe failures in the engine's own
dispatch path before or after the adapter is involved.

(LECC-5-2)=
[LECC.5.2] The engine-emitted codes in v1 are:

(LECC-5-2-2)=
- [LECC.5.2.2] `request.unsupported` — no adapter is registered
  for the call's target. Distinct from the adapter-emitted form
  in [LECC.3.1.1] only by source; the wire shape and matching
  semantics are identical.

*see also:* [LECC.3.1.1](#LECC-3-1-1)

(LECC-5-3)=
[LECC.5.3] Authors must not distinguish engine-emitted from
adapter-emitted codes by code value. Both surface as `CallError`
facts with the same matching contract; the `message` field may
carry source detail for human inspection.

---

(LECC-6)=
## Pattern-match contract [LECC.6]

(LECC-6-1)=
[LECC.6.1] The dot-prefix matching semantics for `error
<pattern>:` clauses are defined in [LCCO.8.3]. This doc commits
the namespace those patterns match against; the matching engine
is normative there.

*see also:* [LCCO.8.3](#LCCO-8-3)

(LECC-6-2)=
[LECC.6.2] An author writing `error request:` matches by family and
catches every leaf under `request` ([LECC.3.1]). An author writing
`error request.malformed:` pins to that one leaf.

*see also:* [LECC.3.1](#LECC-3-1)

(LECC-6-4)=
[LECC.6.4] **A pattern names a family.** The first segment of an `error
<pattern>:` clause's pattern must be `request`, `internal` or `adapter`
([LECC.2.2]). The compiler must refuse any other with
`DS_VALIDATE_ERROR_PATTERN_UNKNOWN_FAMILY`: no code a conforming adapter or the
engine sends can match it.

*ratified*  —  *see also:* [LECC.2.2](#LECC-2-2)

(LECC-7)=
## Author guidance [LECC.7]

(LECC-7-1)=
[LECC.7.1] Match by family when the rule's response is
family-uniform: any `internal` failure warrants the same alert.
Match by leaf when the rule's response depends on the specific
cause: `request.limit_exceeded` retries later,
`request.malformed` never will.

(LECC-7-2)=
[LECC.7.2] An `error:` catch-all should be the last clause in
source. The compiler does not enforce ordering; this is style.

(LECC-7-3)=
[LECC.7.3] Authors should not match `internal.*`. An
`internal.*` code is a bug report, not a state the rule should
react to. The catch-all `error:` already covers the case for
authors who want a single fallback.
