feat: raise typed per-code exceptions from the error catalogue
Why
Note: Once this is merged into the feature branch some of the change log entries will be consolidated and I'll run some real world testing once the various components are in place.
The SDK cannot tell one server failure from another without reading message
strings. Infrahub publishes an error catalogue and already generates TypeScript
bindings for the frontend from it; this is the SDK half.
Every catalogued code now reaches the caller as an exception class carrying its
payload as directly typed attributes, so branching on one specific failure never
means matching words in a message.
Non-goals: the generator itself, which lives in opsmill/infrahub PR https://github.com/opsmill/infrahub/pull/10678.
IHS-295. Covers T063-T064 and T066-T077.
What a caller will notice
- A catalogued failure raises its own class.
UNIQUENESS_VIOLATIONraises
UniquenessViolationErrorwith.node_kindand.fieldspopulated from the
payload, and so on per code. All nine descend fromGraphQLError, so no
existingexceptclause stops catching what it catches today. - The three authentication codes deliberately have no class.
AUTHENTICATION_REQUIRED,TOKEN_EXPIREDandPERMISSION_DENIEDare the
codes a server reports for failures it rejects a request on, so they routinely
arrive on either transport, and each transport already has a class existing
code depends on. CatchApiErrorand testexc.codeto handle one whichever
way it arrived. queryandvariablesare readable on everyApiError. Reading either
off anAuthenticationErrorpreviously raisedAttributeError— and that is
the clause recommended for the three codes above, so it is where they had to
exist.Nonemeans the request was not recorded, not that there was none.- Exceptions survive
pickleandcopy.deepcopy. Python rebuilds an
exception by calling its class with the message as a lone positional argument.
The generated classes take keyword-only payload fields, so that raised
TypeErrorand lost the server's failure;GraphQLErrortakes the error list
first, so the message was silently rebuilt as a placeholder. Both are fixed at
the root of the GraphQL branch. http_statusfalls back to the class's declared status where the envelope
omits one, and every degradation is logged at debug level on the
infrahub_sdklogger — including the unknown-code case, which is the one the
cross-version guarantee is actually about.
What changed
infrahub_sdk/exceptions/catalogue.py— generated, committed, not to be
hand-edited. Nine exception classes, fifteen payload models, and a dispatch
resolving a code and its payload to a concrete exception.infrahub_sdk/exceptions/factory.py— resolves a code through that generated
dispatch, which validates each payload against its own model. The hand-rolled
stand-in payload dataclasses it replaces are gone, so the factory assembles no
attribute itself. A payload that violates the catalogue degrades to the
generic class for the transport with the code still readable.infrahub_sdk/exceptions/base.py—__reduce__onGraphQLError,query
andvariablesonApiError, and a shared default-message helper.infrahub_sdk/exceptions/__init__.py— re-exports the nine classes by
name. Notfrom .catalogue import *: a wildcard would also promote the
payload models, both lookup maps and the dispatch helper onto the package
surface, where a name is a stability promise.docs/.../topics/error_handling.mdx— new topic page: the hierarchy, catching
by branch versus by code, the cross-version guarantees, and the two deliberate
broadenings.- Tests: one fixture and one case per catalogue code, checked against the
bindings' own code map so the set cannot fall behind; both clients driven over
both transports; twocatalogue-marked integration cases against a real
server; pickle/deepcopy round trips for all fifteen codes.
The transport rule
The rule worth reviewing closely, because it is what makes the asymmetry work:
which generic class a fallback lands on follows the transport the SDK
observed, never the status the code declares. A code read from an errors
array raises GraphQLError even when it declares 401; a response the SDK
rejected before the query ran raises AuthenticationError whatever code it
carries. Every catalogued class descends from GraphQLError, so raising one on
the authentication branch would put that failure out of reach of
except AuthenticationError. TestTheFallbackFollowsTheObservedTransport pins
both directions.
How to review
catalogue.py is machine output — review the generator in
opsmill/infrahub PR #10678 instead, and read this copy only to confirm its
shape: 9 classes, 15 models, no class for the three 401/403 codes.
The hand-written core is infrahub_sdk/exceptions/factory.py; everything else
follows from it.
One integration case skips where the server's catalogue has no entry for a
uniqueness violation, which released Infrahub does not yet have. It skips only
when the server described nothing; any code it did describe falls through and
fails.