arrow-typed-errors
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseArrow Typed Errors (Kotlin)
Arrow类型化错误处理(Kotlin)
Quick start
快速开始
- Classify the failure first: logical failure (typed error) vs exceptional failure (exception).
- Pick the surface type:
- + builder (
Raise<E>/either {}/option {}) for composable logic.nullable {} - Wrapper type (/
Either/Option/Ior/nullable) for API boundaries.Result
- Read for API details and patterns before coding if unsure.
references/typed-errors.md
- 首先对失败进行分类:逻辑失败(类型化错误) vs 异常失败(Exception)。
- 选择表层类型:
- 使用+ 构建器(
Raise<E>/either {}/option {})实现可组合逻辑。nullable {} - 在API边界使用包装类型(/
Either/Option/Ior/可空类型)。Result
- 使用
- 如果不确定,在编码前先阅读了解API细节和模式。
references/typed-errors.md
Workflow
工作流程
- Model errors as sealed types.
- Implement happy-path logic with Raise DSL (prefer or
either {}depending on output).nullable {} - Guard invariants with /
ensure.ensureNotNull - Interop with external code:
- Use /
catchto convert expected exceptions into typed errors.Either.catchOrThrow
- Use
- Compose with and transform errors with
.bind()orwithError.recover - For validation across fields or lists, use accumulation (,
zipOrAccumulate, ormapOrAccumulate).accumulate - Expose wrapper types at module boundaries; keep Raise internally when possible.
- 将错误建模为密封类型。
- 使用Raise DSL实现主路径逻辑(根据输出结果选择或
either {})。nullable {} - 使用/
ensure保护不变量。ensureNotNull - 与外部代码互操作:
- 使用/
catch将预期异常转换为类型化错误。Either.catchOrThrow
- 使用
- 使用进行组合,并用
.bind()或withError转换错误。recover - 针对跨字段或列表的验证,使用累积功能(,
zipOrAccumulate或mapOrAccumulate)。accumulate - 在模块边界暴露包装类型;尽可能在内部保留Raise的使用。
Patterns to apply
适用模式
- Smart constructors (parse, don't validate): make constructors private; expose returning
invoke.Either - Validation accumulation: use and
EitherNel/zipOrAccumulate.mapOrAccumulate - Wrapper choice:
- Prefer nullable for optional values unless nested nullability issues exist; then use .
Option - Use for disjoint success/failure;
Eitheronly when success + warning is meaningful.Ior - Use only when errors are
Resultand interop with stdlib APIs is required.Throwable
- Prefer nullable for optional values unless nested nullability issues exist; then use
- Error translation across layers: use to map inner errors to outer domain errors.
withError - Drop error types explicitly: wrap with
bindwhen moving from informative error types toignoreErrors/nullable flows.Option - Avoid sealed-on-sealed inheritance: model error hierarchies with composition (wrapper data classes), not sealed inheritance chains.
- 智能构造函数(解析而非验证):将构造函数设为私有;暴露返回的
Either方法。invoke - 验证累积:使用和
EitherNel/zipOrAccumulate。mapOrAccumulate - 包装类型选择:
- 优先使用可空类型表示可选值,除非存在嵌套可空性问题;此时使用。
Option - 使用表示互斥的成功/失败场景;仅当成功+警告有实际意义时使用
Either。Ior - 仅当错误为且需要与标准库API互操作时使用
Throwable。Result
- 优先使用可空类型表示可选值,除非存在嵌套可空性问题;此时使用
- 跨层错误转换:使用将内部错误映射为外部领域错误。
withError - 显式丢弃错误类型:当从包含信息的错误类型切换到/可空类型流程时,用
Option包装ignoreErrors。bind - 避免密封类型的继承嵌套:使用组合(包装数据类)而非密封继承链来建模错误层级。
References
参考资料
- Load for API details, error accumulation, wrapper-specific guidance, and sample snippets.
references/typed-errors.md
- 查看获取API细节、错误累积、特定包装类型的指南以及代码示例片段。
references/typed-errors.md