generated from john/python-template
102 lines
6.3 KiB
Markdown
102 lines
6.3 KiB
Markdown
# Error Handling (Invariant)
|
|
|
|
## 1. Purpose
|
|
|
|
This document defines the non-negotiable failure-handling principles for the transcription application.
|
|
|
|
Error categories, API envelopes, status codes, framework integrations, and persistence fields may change between versions. Failures must nevertheless remain visible, safe, diagnosable, and consistent across every application boundary.
|
|
|
|
## 2. Core Invariants
|
|
|
|
### 2.1 Failures Are Visible
|
|
|
|
1. An operation must not report success when all or part of the requested work failed.
|
|
2. Invalid input, unavailable dependencies, persistence failures, provider failures, and unexpected defects must be surfaced through the application's established error path.
|
|
3. Code must not silently discard an exception, provider response, invalid value, or failed state transition.
|
|
4. When work can partially succeed, the successful and failed portions must be identified separately.
|
|
|
|
### 2.2 Messages Are Actionable
|
|
|
|
1. Operator-facing errors must explain what failed in concise language.
|
|
2. When a safe corrective action is known, the error must state it.
|
|
3. Expected validation or conflict failures must not be presented as unexplained internal defects.
|
|
4. Internal diagnostics must not replace a usable operator-facing message.
|
|
|
|
### 2.3 Errors Have Stable Identity and Classification
|
|
|
|
1. Every surfaced failure must have a stable correlation identifier or equivalent trace identity.
|
|
2. Failures must be classified into a documented, machine-readable category.
|
|
3. Boundary-specific representations must preserve the original category and correlation identity.
|
|
4. Unknown exceptions must be converted at an explicit boundary, retain their causal chain for diagnostics, and be classified as unexpected rather than disguised as an expected failure.
|
|
|
|
### 2.4 Boundary Translation Is Consistent
|
|
|
|
1. UI, API, service, worker, persistence, and provider boundaries must use one shared error model or deterministic translations between documented models.
|
|
2. A boundary may simplify presentation, but it must not change the meaning, retryability, or identity of a failure.
|
|
3. Domain and service code must not depend on UI notifications or HTTP response types.
|
|
4. UI and API layers must not infer error categories by parsing message text.
|
|
|
|
### 2.5 State Changes Are Safe
|
|
|
|
1. A failed atomic operation must leave persisted state unchanged.
|
|
2. Batch operations may preserve successful independent items only when partial success is an explicit part of the workflow contract.
|
|
3. A failed item must retain enough state to identify what was attempted and whether retry is safe.
|
|
4. Error handling must not overwrite earlier successful results or historical execution evidence.
|
|
|
|
### 2.6 Retry Is Explicit and Bounded
|
|
|
|
1. Validation, authorization, policy, conflict, and other deterministic failures must not be retried automatically without a relevant input or state change.
|
|
2. Automatic retry is permitted only for failures classified as transient and only when the operation is idempotent or otherwise protected from duplicate effects.
|
|
3. Retry count, delay, and terminal behavior must be bounded and observable.
|
|
4. Exhausted retries must end in a visible terminal failure rather than an indefinitely pending state.
|
|
|
|
### 2.7 Diagnostics Are Preserved Safely
|
|
|
|
1. Logs and persisted diagnostic evidence must retain enough context to correlate the failure with the affected operation and record.
|
|
2. Provider and infrastructure failures must preserve safe diagnostic evidence at the boundary where it is available.
|
|
3. Credentials, authorization headers, cookies, secret values, and unnecessary personal data must not appear in errors, logs, notifications, or exports.
|
|
4. Diagnostic metadata capture must use explicit safe-field allowlists where unrestricted content could contain secrets.
|
|
5. User-facing messages must not expose stack traces, local filesystem details, database credentials, or raw internal exceptions.
|
|
|
|
AI execution failures also follow the evidence rules in [Digital Evidence and AI Processing Provenance](ai_evidence_and_provenance.md).
|
|
|
|
### 2.8 Cancellation and Timeout Are Distinct Outcomes
|
|
|
|
1. User cancellation, application shutdown, local timeout, remote timeout, and provider rejection must remain distinguishable.
|
|
2. Cancellation must not be converted into success or a generic unexpected error.
|
|
3. Timeout handling must identify whether a provider response was received when that fact is known.
|
|
4. Cleanup after cancellation or timeout must preserve consistency and must not conceal a completed side effect.
|
|
|
|
### 2.9 Logging Must Support Audit Without Becoming the Record
|
|
|
|
1. Structured logs must include correlation identity, operation, category, and relevant non-secret record identifiers.
|
|
2. Expected operator errors may be logged less severely than unexpected defects, but they must remain observable.
|
|
3. Logs are operational diagnostics and do not replace required database state or archival evidence.
|
|
4. Duplicate logging of the same failure at every layer should be avoided; ownership of the authoritative log event must be clear.
|
|
|
|
## 3. Verification Policy
|
|
|
|
Each version must verify:
|
|
|
|
1. Every documented error category reaches the intended UI and API representation.
|
|
2. Failed atomic writes roll back completely.
|
|
3. Partial-success workflows preserve successful independent results and identify failed items.
|
|
4. Retry behavior is bounded and restricted to eligible failures.
|
|
5. Unexpected exceptions retain correlation and causal information without exposing sensitive details.
|
|
6. Logs, persisted evidence, UI messages, and exports contain no credentials.
|
|
7. Cancellation, timeout, provider response failure, and no-response failure remain distinguishable.
|
|
|
|
## 4. Versioned Ownership
|
|
|
|
1. Version-specific error taxonomies, envelopes, HTTP mappings, model fields, and framework behavior belong in the applicable version documentation.
|
|
2. Each versioned error-handling document must state how it satisfies this invariant.
|
|
3. A version may add stricter safeguards but must not weaken these principles without first revising this invariant deliberately.
|
|
4. Implementation and tests must be updated together when a versioned error contract changes.
|
|
|
|
## 5. Related Invariants
|
|
|
|
- [Historical Document Transcription Design Intent](intent.md)
|
|
- [Digital Evidence and AI Processing Provenance](ai_evidence_and_provenance.md)
|
|
- [UI Style Guide](ui_style_guide.md)
|
|
|