fix(graphql): normalize non-str node_type in NodeNotFoundError (closes #9926)
Analyst's findings (summary)
Root cause: NodeNotFoundError.node_type can hold a schema/protocol class (type[SchemaProtocol]) rather than a string. The GraphQL error formatter feeds it straight into NodeNotFoundData(node_kind=...), whose node_kind field is typed str, so pydantic raises a ValidationError inside the error-formatting path — turning a would-be NODE_NOT_FOUND (404) response into an uncaught ASGI 500.
Affected files:
backend/infrahub/graphql/error_formatter.py — _build_payload() builds NodeNotFoundData(node_kind=exc.node_type, ...) with no coercion (the crash site).
backend/infrahub/errors/payloads.py — NodeNotFoundData.node_kind: str (extra="forbid", frozen=True).
backend/infrahub/exceptions.py — NodeNotFoundError.__init__ annotates node_type: str but stores it verbatim with no coercion.
backend/infrahub/core/manager.py — get_one and get_one_by_id_or_default_filter raise with an unresolved class kind, unlike sibling sites that resolve to kind_str first.
Replication test
Test file: backend/tests/unit/graphql/test_error_formatter.py
Test name: test_build_catalogue_extensions_handles_class_node_type
What it tests: Formatting a NodeNotFoundError whose node_type is a class produces a normal catalogued NODE_NOT_FOUND / 404 payload with a string node_kind, instead of raising.
Verification: Test confirmed FAILING on the pre-fix code.
Failure reason: build_catalogue_extensions raised a pydantic ValidationError on node_kind inside _build_payload, proving the formatter can itself crash while formatting an error.
Failure output (last 20 lines)
backend/tests/unit/graphql/test_error_formatter.py:140: in test_build_catalogue_extensions_handles_class_node_type
extensions = build_catalogue_extensions(exc)
backend/infrahub/graphql/error_formatter.py:127: in build_catalogue_extensions
"data": _build_payload(exc, code),
backend/infrahub/graphql/error_formatter.py:52: in _build_payload
payload = NodeNotFoundData(node_kind=exc.node_type, identifier=exc.identifier)
E pydantic_core._pydantic_core.ValidationError: 1 validation error for NodeNotFoundData
E node_kind
E Input should be a valid string [type=string_type, input_value=<class 'infrahub.core.protocols.CoreMenuItem'>, input_type=type]
E For further information visit https://errors.pydantic.dev/2.12/v/string_type
Test expectations
The test asserts the observable behavior of the error formatter for a class-valued node_type: it must not raise, and it must return code == "NODE_NOT_FOUND", http_status == 404, data["identifier"] == "x", and a string data["node_kind"] that conveys the kind name. The node_kind assertion is deliberately tolerant of the exact coercion strategy (kind name vs. repr) so it validates the contract without over-constraining the fix.
Summary by cubic
Fixes a crash in the GraphQL error formatter when NodeNotFoundError carried a non-string node_type, which returned a 500 instead of a 404. The formatter now always returns a NODE_NOT_FOUND payload with a string node_kind.
- Bug Fixes
- Normalize kinds at
NodeManagerraise sites (get_one,get_one_by_id_or_default_filter) using a newget_kind_strhelper to always pass a string intoNodeNotFoundError. - Coerce
node_kindwithstr()in_build_payloadso formatting never raises; added unit tests for class-valuednode_typeand forget_kind_str.
- Normalize kinds at