协议契约
更新时间:2026-04-23
本文档描述 orion-error 当前已经落地的协议层设计。
这里说的“协议层”不是新的 runtime 传播模型,而是对外稳定消费接口。
1. 三层结构
当前协议层由三层组成:
- 稳定身份:
ErrorIdentity - exposure 决策:
ExposureDecision - 出口投影:HTTP / CLI / log / RPC / user debug
推荐把它理解为:
StructError<R>负责运行时传播ErrorIdentity负责稳定识别DiagnosticReport负责人类诊断ErrorProtocolSnapshot负责把 identity + decision + report 组装成统一消费输入
2. 稳定身份
稳定身份结构是 ErrorIdentity。
字段:
codecategoryreasondetailpositionpath
语义:
code:稳定机器主键category:稳定分类reason:稳定的人类摘要detail:可变补充说明,不是主键path:稳定导出的路径投影- 当前 runtime 主语义仍应优先理解为
action/locator/ path segments
入口:
StructError::identity_snapshot()assert_err_code(...)assert_err_category(...)assert_err_identity(...)
注意:当前 assert_err_code(...) 断言的是 stable code 字符串,不是数值 error_code()。
3. Exposure
exposure 决策结构是 protocol::ExposureDecision。
字段:
http_statusvisibilitydefault_hintsretryable
默认 exposure 策略实现是 protocol::DefaultExposurePolicy。
当前默认规则:
Biz -> 400 + PublicConf / Logic / Sys -> 500 + Internalsys.network_error/sys.timeout->retryable = true- 其他 stable code 默认
retryable = false
说明:
- 当前文档中的运行时主路径仍然是
doing(...) - top-level
want已从 identity / snapshot / protocol 投影中移除 - 兼容残留主要收在 context frame 的
target
主要入口:
ExposurePolicy::decide(...)StructError::exposure(...)StructError::into_exposure(...)- 完整 projection 数据以
StructError::exposure(...)为主路径
4. ErrorProtocolSnapshot
ErrorProtocolSnapshot 是当前统一协议输入。
结构:
identitydecision- 内嵌诊断 report,可通过
report()只读访问
入口:
StructError::exposure(...)StructError::into_exposure(...)
适用场景:
- 测试快照
- 网关二次投影
- 协议统一出口
- 用户调试摘要
5. HTTP Projection
JSON 字段:
statuscodecategorymessagevisibilityhints
规则:
Public时,message优先使用detailInternal时,message使用稳定reason
入口(需要 serde_json feature):
ErrorProtocolSnapshot::to_http_error_json()
6. CLI Projection
JSON 字段:
codecategorysummarydetailvisibilityhints
规则:
summary使用 compact renderdetail使用 verbose render
入口(需要 serde_json feature):
ErrorProtocolSnapshot::to_cli_error_json()
7. Log Projection
JSON 字段:
codecategoryreasondetailoperationpathvisibilityhintsroot_metadatacontextsource_frames
规则:
- 保留完整
context - 保留
root_metadata - 保留
source_frames
入口(需要 serde_json feature):
ErrorProtocolSnapshot::to_log_error_json()
8. RPC Projection
JSON 字段:
statuscodecategoryreasondetailvisibilityhintsretryable
规则:
detail只在Public可见时保留retryable完全来自 exposure decision
入口(需要 serde_json feature):
ErrorProtocolSnapshot::to_rpc_error_json()
9. User Debug Summary
render_user_debug(...) 是给人看的调试摘要,不是机器协议。
入口:
ErrorProtocolSnapshot::render_user_debug()ErrorProtocolSnapshot::render_user_debug_redacted(...)
它的定位是:
- 本地调试
- 示例输出
- 人工排障
它不是:
- public HTTP message
- 稳定 JSON schema
10. DiagnosticReport
DiagnosticReport 是 report 层对象。
它不依赖 ErrorIdentityProvider,因此更适合:
- 文本渲染
- redaction
- 人类诊断
常用入口:
StructError::report()StructError::into_report()StructError::report()/into_report()
如果启用了 serde_json feature,还可以使用:
- 协议投影应改走
StructError::exposure(...) - 然后使用
ErrorProtocolSnapshot::to_*_json()
11. 建议的消费路径
推荐顺序:
- 运行时传播用
StructError<R> - 要稳定识别时取
identity_snapshot() - 要统一出口规则时取
exposure(...) - 要协议出口时使用 projection API
- 要人类摘要时使用
render_user_debug(...)
不建议:
- 直接把
Display文本当协议主键 - 直接把 CLI 文本当机器协议
- 用
detail全文本做唯一稳定断言