aeat.core.errors._registry module¶
Structured error-code registry and CLI rendering helpers.
Centralises AEAT’s stable CLI error taxonomy. Every
core.errors.AeatError subclass binds to a predeclared
ErrorCode row through bind_error_code(), so the public
contract stays explicit, reviewable, and grep-stable. Rendering helpers
render_error_text() and render_error_json() produce the
human-readable and machine-readable stderr payloads that downstream tools
consume; build_error_envelope() constructs the underlying
ErrorEnvelope.
Secret-looking context keys (matching _SECRET_FIELD_PATTERN) are
redacted before they ever reach stderr — see scrub_error_context().
Non-secret context values are also passed through
core.redaction.redact_for_log() so NIF, URL, and bearer-token
shapes share the same rule vocabulary as logs and observability.
- class ErrorCategory(*values)[source]¶
Bases:
StrEnumClosed catalogue of stable CLI error categories.
- ERROR¶
- REFUSED¶
- AUTH¶
- INTEGRITY¶
- FAIL¶
- INTERNAL¶
- LOCKED¶
- class ErrorCode(**data)[source]¶
Bases:
BaseModelStable metadata attached to an
core.errors.AeatErrortype.- Parameters:
- code: str¶
- category: ErrorCategory¶
- message_key: str¶
- default_suggestion: str | None¶
- retryable: bool¶
- runbook_id: str | None¶
- class ErrorEnvelope(**data)[source]¶
Bases:
BaseModelMachine-readable error body nested under the shared envelope spine.
Rendered as the
errormember of the stderr error document. The document-level spine (schema_version,command,status,notices) is added byrender_error_json()so the error document and the successcore.json_contract.SchemaEnvelopeshare one outer shape.- Parameters:
- code: str¶
- category: str¶
- message: str¶
- suggestion: str | None¶
- retryable: bool¶
- runbook_id: str | None¶
- context: dict[str, str] | None¶
- trace_id: str | None¶
- register(code)[source]¶
Register
codein the global catalogue.- Parameters:
- Return type:
- Returns:
The same
ErrorCodeobject for fluent use at declaration sites.- Raises:
ValueError – If a duplicate code identifier is encountered.
- declared_error_codes()[source]¶
Return declared
(qualified class name, :class:`ErrorCode`)registry rows.
- bind_error_code(error_type)[source]¶
Bind a stable
ErrorCodetoerror_type.Called from
AeatError.__init_subclass__at class-creation time. If the global_DECLARED_CODE_BY_QUALNAMEmapping is not yet available (the module is still initialising due to a circular import) the class is added to_DEFERRED_BINDand bound lazily on first use viaget_registered_error_code().- Parameters:
error_type (
type[BaseException]) – Error class being declared.- Return type:
- Returns:
The registered
ErrorCodeforerror_type.- Raises:
ValueError – When the mapping is available but contains no entry for this class.
- get_registered_error_code(error)[source]¶
Return the registered
ErrorCodeforerror.Drains any deferred binds accumulated during the circular-import window before attempting the lookup, so classes defined before
_DECLARED_CODE_BY_QUALNAMEwas populated are bound here on first runtime use.- Return type:
- Parameters:
error (BaseException | type[BaseException])
- resolve_output_language()[source]¶
Resolve the configured output language, defaulting to
es.- Return type:
- scrub_error_context(context)[source]¶
Redact secret-looking keys and strip internal keys from
context.Keys matching
_SECRET_FIELD_PATTERNare replaced with"<redacted>". Keys in_INTERNAL_CONTEXT_KEYSare dropped entirely — they are implementation detail (e.g. widget prompt identifiers) and must not appear in operator-facing output.
- build_error_envelope(error, *, context=None, trace_id=None)[source]¶
Build the deterministic JSON stderr envelope for
error.- Return type:
- Returns:
A frozen
ErrorEnvelopesuitable for serialisation to the machine-readable stderr payload.- Parameters:
error (BaseException)
trace_id (str | None)
- render_error_text(error, *, context=None)[source]¶
Render the human-readable stderr payload for
error.- Return type:
- Parameters:
error (BaseException)
- render_error_json(error, *, context=None, trace_id=None, active_profile=None)[source]¶
Serialize
errorto a deterministic single-line JSON document.The document carries the shared envelope spine (
schema_version,command,active_profile,status,notices) so it is shape-compatible with the successcore.json_contract.SchemaEnvelope. The error detail is nested undererror.commandisNone: the CLI error boundary terminates before the dotted command path is resolvable, so the field is present-but-null for spine uniformity.active_profileis the human label of the active taxpayer profile (the identity anchor),Nonefor a non-profile-bound failure or when the CLI error boundary cannot resolve it; thecorelayer never scans profile manifests, so the CLI boundary resolves the label and passes it here. Thecore.json_contract.ENVELOPE_SCHEMA_VERSIONimport is function-local to avoid thejson_contract<->errorsimport cycle (json_contractimportsAeatError).
- get_error_exit_code(category)[source]¶
Return the canonical process exit code for
category.The exit-code family is the operator’s coarse outcome signal:
ERROR-> 1: an expected, operator-actionable failure or refusal of a domain outcome. A modelo verification that resolves not-granted — whetherBLOCKED(a blocking-rule finding) orINCOMPLETE(missing required casillas) — exits 1: both are expected verification verdicts, surfaced throughtyper.Exit(code=1)in the verify handler, never as anINTERNALcrash.REFUSED-> 2,AUTH-> 3,INTEGRITY-> 4,FAIL-> 5,LOCKED-> 7: the remaining expected, registered refusal classes.INTERNAL-> 6 is reserved exclusively for an unexpected internal crash (theINTERNAL_*registry codes:INTERNAL_CLI_UNEXPECTED_BOUNDARY,INTERNAL_WORKFLOW_UNHANDLED, etc.). An expected domain outcome MUST NOT map toINTERNAL— a not-granted verification verdict is a result the operator must act on, not a program defect, so it never exits 6.
- Return type:
- Parameters:
category (ErrorCategory)
- resolve_error_message(error, code=None)[source]¶
Resolve the user-facing message for
error.translated_messageis a translation key (e.g."profile.errors.not_configured") by convention; it is rendered through the i18n backend, which falls back to the key itself when no matching translation exists.- Return type:
- Parameters:
error (BaseException)
code (ErrorCode | None)