# Get the next free error code from FACET instead of grepping

> Name your error codes in FACET records, 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.

Source: https://facet-framework.com/en/guides/next-free-error-code/  
Last updated: 2026-11-26T23:28:23+01:00

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 drift 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”](https://facet-framework.com/en/guides/write-down-what-a-feature-does/). 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.

## See 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.](https://facet-framework.com/guides/next-free-error-code/errors-start-light.png)

*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 scenario 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.

## Add 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-email
lifecycle: active
intent:
  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 E1002
verification:
  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.](https://facet-framework.com/guides/next-free-error-code/errors-claimed-light.png)

*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.

## Watch 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-order
lifecycle: planned
intent:
  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 E3003
lineage:
  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-differs
lifecycle: planned
intent:
  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 usable
lineage:
  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.](https://facet-framework.com/guides/next-free-error-code/lint-collision-light.png)

*The same code in two domains 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.

## Take 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.](https://facet-framework.com/guides/next-free-error-code/errors-final-light.png)

*Each new code belongs to exactly one record.*

The lint 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.

> **What this does not do**
>
> `facet errors` reads records, not source code, and only three fields of them: `intent.summary`, scenario statements
> and `knowledge.common_pitfalls`. A code that only appears in `src/errors.ts`, a scenario id or a comment is invisible,
> so "next free" is a lower bound, as `E3004` shows here while `E3001` and `E3002` have no record. The collision is a
> warning: it shows up in every pull request, and it never fails one.

## Commit 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”](https://facet-framework.com/en/guides/write-down-what-a-feature-does/) 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`.

## If 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.](https://facet-framework.com/en/solutions/distributed-teams/)

### Sources

- [FACET Core on GitHub](https://github.com/facet-framework/core)
- [How to write a record](https://github.com/facet-framework/core/blob/main/docs/AUTHORING.md)
