Facet
DeutschInstall
Team & product

Get the next free error code from FACET instead of grepping

Name your error codes in FACET recordA FACET record is a short, checkable description of one feature, kept as a YAML file next to the code. It states why the feature exists, the behaviour it promises as scenarios, and the tests that check it.In the glossary, get the next free code of each band from one command, and let the check warn in every pull request when two areas use the same code.

  • 9 min read
  • 6 steps
  • npmGitHub Actions
  • Your coding agent

Two pull requests land on the same afternoon. One adds "refund on a cancelled order" with error code E3003. The other adds "new password must differ from the old one", also with E3003. Both authors opened src/errors.ts, looked at the highest number and added one. Review passed both, because each diff on its own was fine. A week later support reads E3003 in a customer's screenshot and nobody knows which of the two errors it was.

FACET keeps a short, checkable description of each feature, called a record, next to your code and tells you when code and description DriftDrift is when code changes and the record describing it does not follow, so the two no longer match. facet drift lists the features whose anchored code changed since a chosen baseline.In the glossary apart. By the end of this guide npx facet errors answers "which code is next in my band?" from your FACET records, and npx facet lint warns in every pull request when two parts of the product write the same code.

The commands below use the example shop from “One answer instead of three”. Its codes are E1001–E3002 in src/errors.ts, one band per area: E1 account, E2 discounts, E3 refunds. In your own project, use your own records and codes.

Step 1See which codes your records already use

Ask FACET for the register of error codes:

sh
  • npx facet errors
Terminal output of npx facet errors: band E2 with 3 codes, next free E2005; E2002, E2003 and E2004 belong to checkout/discounts/redeem-code; 3 identifiers across 1 band.Terminal output of npx facet errors: band E2 with 3 codes, next free E2005; E2002, E2003 and E2004 belong to checkout/discounts/redeem-code; 3 identifiers across 1 band.
The register before any change: one band, three codes.

The register is built from your records, not from the source code. FACET reads every code that matches E plus four digits in a record's intent.summary, its ScenarioA scenario is one behaviour a feature promises, written in a record as Given/When/Then steps. Each scenario has an id that tests and other records can refer to.Read more here statements and knowledge.common_pitfalls. The shop's only record describes discount codes, so only the E2 band appears, and even that one is incomplete: E2001 is in src/errors.ts, but no scenario mentions it yet.

So "next free" is only as good as your records. The next two steps fill them in.

Step 2Add the codes the code already throws

Start with the record you have. Open docs/facets/features/checkout/discounts/redeem-code.yaml and add the missing case as the first scenario:

yaml
contract:  scenarios:    - id: unknown-code-is-refused      statement: |        Given a code the shop does not know        When a customer enters it in the cart        Then the shop refuses it with E2001    # the four scenarios already in the record follow here

The test rejects an unknown code in tests/discount.test.ts checks exactly this, so add unknown-code-is-refused to the proves list under verification.

Then describe the area that owns the E1 band. Create the folder:

sh
  • mkdir -p docs/facets/features/account/password-reset

Save this as docs/facets/features/account/password-reset/reset-by-email.yaml:

yaml
schema_version: "0.1"id: account/password-reset/reset-by-emaillifecycle: activeintent:  summary: A customer who forgot the password asks for a reset link by e-mail and sets a new password with it.contract:  scenarios:    - id: same-answer-for-every-address      statement: |        Given any e-mail address, with or without an account        When someone asks for a reset link        Then the shop gives the same answer, so nobody learns who is a customer    - id: link-works-once-for-an-hour      statement: |        Given a reset link sent less than 60 minutes ago        When the customer uses it a second time, or after 60 minutes        Then the shop refuses it with E1001    - id: three-requests-per-hour      statement: |        Given an address that asked for a reset link three times in the last hour        When someone asks a fourth time        Then the shop refuses it with E1002verification:  unit_tests:    - id: tests/password-reset.test.ts      proves: [same-answer-for-every-address, link-works-once-for-an-hour, three-requests-per-hour]lineage:  owner:    team: accounts  created_at: "2026-11-22"

Connect it to the code. In src/account/password-reset.ts, add two lines directly above the class:

ts
// @facet: account/password-reset/reset-by-email// @facet-role: primary/** Password reset by e-mail link. State is in memory; a real deployment would use the database. */export class PasswordResets {

Run the register again:

sh
  • npx facet errors
Terminal output of npx facet errors: band E1 with 2 codes, next free E1003; band E2 with 4 codes, next free E2005; each code lists the record that mentions it; 6 identifiers across 2 bands.Terminal output of npx facet errors: band E1 with 2 codes, next free E1003; band E2 with 4 codes, next free E2005; each code lists the record that mentions it; 6 identifiers across 2 bands.
Two bands, each with its next free code.

E2001 now counts, and the E1 band appears with E1003 as its next free code. The refund codes E3001 and E3002 are still missing, because no record describes refunds yet. Describe that area the same way before anyone takes a code from its band.

Step 3Watch the check catch two teams on the same code

Now replay the afternoon from the start of this guide. The orders team plans its new case as a record with lifecycle: planned, before writing any code. Create the folder:

sh
  • mkdir -p docs/facets/features/orders/refunds

Save this as docs/facets/features/orders/refunds/cancelled-order.yaml:

yaml
schema_version: "0.1"id: orders/refunds/cancelled-orderlifecycle: plannedintent:  summary: A cancelled order is refunded in full automatically, so a manual refund request on it is refused.contract:  scenarios:    - id: manual-refund-on-cancelled-order-is-refused      statement: |        Given an order that was cancelled and refunded automatically        When a customer requests a refund on it        Then the shop refuses it with E3003lineage:  owner:    team: orders  created_at: "2026-11-22"

On the same day the accounts team looks at src/errors.ts, sees E3002 as the highest number and plans its case with E3003 too. Save this as docs/facets/features/account/password-reset/new-password-differs.yaml:

yaml
schema_version: "0.1"id: account/password-reset/new-password-differslifecycle: plannedintent:  summary: A reset link cannot set the password the account already has.contract:  scenarios:    - id: same-password-is-refused      statement: |        Given a valid reset link        When the customer enters the password the account already has        Then the shop refuses it with E3003 and the link stays usablelineage:  owner:    team: accounts  created_at: "2026-11-22"

Run the check:

sh
  • npx facet lint
Terminal output of npx facet lint: among four warnings, one says E3003 appears in records from more than one domain, account (account/password-reset/new-password-differs) and orders (orders/refunds/cancelled-order); summary 0 errors, 4 warnings.Terminal output of npx facet lint: among four warnings, one says E3003 appears in records from more than one domain, account (account/password-reset/new-password-differs) and orders (orders/refunds/cancelled-order); summary 0 errors, 4 warnings.
The same code in two DomainA domain is the first part of a feature id: a product area such as checkout, never a team or a service.In the glossary is reported by name.

The last warning is the collision: "E3003" appears in records from more than one domain and it names both records. The two other kinds of warnings are expected here: the planned cases have no tests yet, and two test files belong to no record.

Step 4Take the next free code from the register

The accounts team's band is E1. Ask the register for its next free code:

sh
  • npx facet errors

It says E1 — 2 code(s), next free: E1003. Change E3003 to E1003 in new-password-differs.yaml and run both commands again:

sh
  • npx facet lint
  • npx facet errors
Terminal output of npx facet errors: E1 has 3 codes, next free E1004; E2 has 4 codes, next free E2005; E3 has 1 code, next free E3004; E1003 belongs to account/password-reset/new-password-differs and E3003 to orders/refunds/cancelled-order; 8 identifiers across 3 bands.Terminal output of npx facet errors: E1 has 3 codes, next free E1004; E2 has 4 codes, next free E2005; E3 has 1 code, next free E3004; E1003 belongs to account/password-reset/new-password-differs and E3003 to orders/refunds/cancelled-order; 8 identifiers across 3 bands.
Each new code belongs to exactly one record.

The LintLint is FACET's check that records and code still point at each other: every record matches its schema, every anchor names an existing record, and every active record has an anchor. It runs locally and in CI on every pull request.In the glossary summary no longer lists error-codes, and the register shows E1003 for the accounts team and E3003 for the orders team. The planned code is taken as soon as the record is merged, before a line of code uses it.

Step 5Commit the records so every pull request sees the register

sh
  • git add docs src/account/password-reset.ts
  • git commit -m "Describe password reset and reserve E1003 and E3003 in records"

The workflow from “One answer instead of three” runs facet lint on every pull request, so the next code collision appears as a warning in the check output, with both record ids.

Your codes look different, for example PAY-101? Set your own pattern in a file .facetrc.yaml at the project root. Both facet errors and the lint check use it:

yaml
error_codes:  pattern: "PAY-[0-9]{3}"

With that pattern, a record that mentions PAY-101 shows up as PAY-1 — 1 code(s), next free: PAY-102.

Step 6If the register says something else

  • A code from your source is missing: no record mentions it in intent.summary, a scenario statement or knowledge.common_pitfalls. Write the scenario where the code is returned, as in step 2.
  • A whole band is missing: no record describes that area yet. Describe it before taking a number from it, or "next free" starts after the highest code your records happen to mention.
  • appears in records from more than one domain, but the code really is shared: if one error is deliberately returned by two areas, keep it and note the decision in the review; the warning stays visible. If not, give one of the records the next free code of its own band.
  • contract scenario(s) with no verification proof: a planned case has no test yet. The warning goes away once a test proves the scenario and the record names it under verification.

For more on teams that change the same product side by side, read Your release was green. Ours is broken now.

Sources