Protocol Contract
1. Three-Layer Structure
- Stable identity:
ErrorIdentity - Exposure decision:
ExposureDecision - Output projections: HTTP / CLI / log / RPC / user debug
Roles:
StructError<R>— runtime propagationErrorIdentity— stable identificationDiagnosticReport— human diagnosticsErrorProtocolSnapshot— identity + decision + report assembly
2. Stable Identity
ErrorIdentity
Fields:
code— stable machine keycategory— stable classificationreason— stable human summarydetail— variable description (not a key)position— source locationpath— stable path projection
Entry points:
StructError::identity_snapshot()assert_err_code(…)— asserts stable code string, not numericerror_code()assert_err_category(…)assert_err_identity(…)
3. Exposure
protocol::ExposureDecision
Fields:
http_statusvisibilitydefault_hintsretryable
Default policy (DefaultExposurePolicy):
| Category | http_status | visibility |
|---|---|---|
| Biz | 400 | Public |
| Conf / Logic / Sys | 500 | Internal |
sys.network_error, sys.timeout → retryable = true. All others retryable = false.
Entry points:
ExposurePolicy::decide(…)StructError::exposure(…)StructError::into_exposure(…)
4. ErrorProtocolSnapshot
Fields:
identitydecisionreport(read-only viareport())
Entry points:
StructError::exposure(…)StructError::into_exposure(…)
Use cases: test snapshot, gateway reprojection, unified protocol output, debug summary.
5. HTTP Projection
Requires serde_json feature.
JSON fields: status, code, category, message, visibility, hints
Rules:
Public→messageusesdetailInternal→messageuses stablereason
Entry: ErrorProtocolSnapshot::to_http_error_json()
6. CLI Projection
Requires serde_json feature.
JSON fields: code, category, summary, detail, visibility, hints
Rules:
summaryuses compact renderdetailuses verbose render
Entry: ErrorProtocolSnapshot::to_cli_error_json()
7. Log Projection
Requires serde_json feature.
JSON fields: code, category, reason, detail, path, visibility, hints, root_metadata, context, source_frames
Rules:
- Full
contextpreserved - Full
root_metadatapreserved - Full
source_framespreserved
Entry: ErrorProtocolSnapshot::to_log_error_json()
8. RPC Projection
Requires serde_json feature.
JSON fields: status, code, category, reason, detail, visibility, hints, retryable
Rules:
detailonly visible whenPublicretryablefrom exposure decision
Entry: ErrorProtocolSnapshot::to_rpc_error_json()
9. User Debug Summary
render_user_debug(…) is a human-readable debug summary, not a machine protocol.
Entry: ErrorProtocolSnapshot::render_user_debug(), .render_user_debug_redacted(…)
Use cases: local debugging, sample output, manual troubleshooting.
Not: HTTP message, stable JSON schema.
10. DiagnosticReport
DiagnosticReport does not require ErrorIdentityProvider. Suitable for text rendering, redaction, human diagnostics.
Entry: StructError::report(), StructError::into_report()
11. Recommended Consumption Path
- Runtime propagation →
StructError<R> - Stable identification →
identity_snapshot() - Unified output →
exposure(…) - Protocol output → projection API
- Human summary →
render_user_debug(…)
Avoid:
- Using
Displaytext as protocol key - Using CLI text as machine protocol
- Using raw
detailas stable assertion