feat(backend): generate the SDK's error catalogue bindings

Merged
OG
2mo ago
+1431-10

Why

The Python SDK has no way to tell one server failure from another without reading
message strings. Infrahub already publishes an error catalogue and generates
TypeScript bindings for the frontend from it; the SDK was left to string-match.

This PR makes Infrahub generate the SDK's error bindings from that same catalogue,
so a caller can branch on a typed exception instead of a message.

Non-goals: the SDK-side changes that consume these bindings (IHS-295), and the
submodule pointer bump.

Part of IFC-3034. Covers T052-T062.

Once this is approved I'll merge the SDK PR https://github.com/opsmill/infrahub-sdk-python/pull/1266 and update the commit of the SDK submodule in this repo to the latest commit from infrahub-develop.

What changed

Behavioral changes:

  • uv run invoke backend.generate now also renders
    infrahub_sdk/exceptions/catalogue.py into the python_sdk submodule, alongside
    the schema models and protocols it already produces there.
  • uv run invoke backend.validate-generated fails when those bindings are stale
    or were never committed. git diff reports nothing for an untracked file, so
    a diff-only check would pass a freshly generated artefact that nobody committed.
  • backend-validate-generated now also triggers on a change to
    schema/error-catalogue.json alone, so a hand-edit of the JSON cannot slip past.

Implementation notes:

  • Everything derives from schema/error-catalogue.json; there is no per-code table
    on either side. Classes the SDK already ships are adopted by parsing their CODE
    declarations out of exceptions/base.py with ast, so adopting a further code
    needs no generator change.
  • Generation aborts rather than emitting a guess when the catalogue would shadow a
    name, produce an unusable identifier, declare a payload field that collides with
    the exception's own members, use an unsupported schema construct, or omit a
    status, title or version. Two of these were found to fail silently before the
    guards existed: a payload field named code compiled cleanly and overwrote the
    class's catalogue code.
  • The derivation logic lives in infrahub/errors/sdk_bindings.py, beside the
    exporter that produces its input, so tasks/backend.py stays the thin invoke
    wrapper this pipeline already uses.

What stayed the same: no schema changes, no API contract changes. The SDK submodule
pointer is untouched; that bump belongs with the SDK merge.

Suggested review order

  1. python_sdk/specs/ifc-3034-error-catalogue/contracts/generator-contract.md - the contract
  2. backend/infrahub/errors/sdk_bindings.py - the derivation rules and refusals
  3. backend/templates/generate_sdk_errors.j2 - what gets emitted
  4. tasks/backend.py - invoke plumbing only
  5. Tests

How to review

[!WARNING]
backend-validate-generated is expected to fail on this PR. It reports
catalogue.py is generated but is not committed in the SDK. That is this PR's own
gate working correctly: the artefact is committed by IHS-295 in the SDK repo, which
merges first. This PR then bumps the pointer and the job goes green.

uv run invoke backend.generate          # writes the bindings into python_sdk/
uv run pytest backend/tests/unit/errors/ -q

Generating twice is byte-identical. The output is 9 generated classes, 3 adopted
imports, 15 payload models, and no class for the three 401/403 codes.

Documentation Updates

  • dev/knowledge/backend/code-generation.md - the pipeline diagram, the
    regeneration table and the validate-generated list all enumerate what is
    generated into the submodule, so all three were stale.

No changelog fragment: the generator, the CI trigger and the validation are
internal, and the artefact ships from the SDK repo, whose own fragment covers what
users see.

Test Plan

  • 74 new unit tests (backend/tests/unit/errors/test_sdk_bindings_generator.py)
    covering name derivation, the JSON Schema vocabulary, the adoption walk, every
    abort condition, and the staleness gate's decision.
  • Verified by mutation: disabling the adopted-code precedence guard, and making an
    untracked artefact read as committed, each fail the suite.
  • Full backend unit suite: 2766 pass. mypy: 1694 files, 0 issues.

Assisted-by: opsmill-dev-commit 0.1.0
Assisted-by: opsmill-dev-pr 0.2.0