arrow-typed-errors

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Arrow Typed Errors (Kotlin)

Arrow类型化错误处理(Kotlin)

Quick start

快速开始

  • Classify the failure first: logical failure (typed error) vs exceptional failure (exception).
  • Pick the surface type:
    • Raise<E>
      + builder (
      either {}
      /
      option {}
      /
      nullable {}
      ) for composable logic.
    • Wrapper type (
      Either
      /
      Option
      /
      Ior
      /
      Result
      /nullable) for API boundaries.
  • Read
    references/typed-errors.md
    for API details and patterns before coding if unsure.
  • 首先对失败进行分类:逻辑失败(类型化错误) vs 异常失败(Exception)。
  • 选择表层类型:
    • 使用
      Raise<E>
      + 构建器(
      either {}
      /
      option {}
      /
      nullable {}
      )实现可组合逻辑。
    • 在API边界使用包装类型(
      Either
      /
      Option
      /
      Ior
      /
      Result
      /可空类型)。
  • 如果不确定,在编码前先阅读
    references/typed-errors.md
    了解API细节和模式。

Workflow

工作流程

  1. Model errors as sealed types.
  2. Implement happy-path logic with Raise DSL (prefer
    either {}
    or
    nullable {}
    depending on output).
  3. Guard invariants with
    ensure
    /
    ensureNotNull
    .
  4. Interop with external code:
    • Use
      catch
      /
      Either.catchOrThrow
      to convert expected exceptions into typed errors.
  5. Compose with
    .bind()
    and transform errors with
    withError
    or
    recover
    .
  6. For validation across fields or lists, use accumulation (
    zipOrAccumulate
    ,
    mapOrAccumulate
    , or
    accumulate
    ).
  7. Expose wrapper types at module boundaries; keep Raise internally when possible.
  1. 将错误建模为密封类型。
  2. 使用Raise DSL实现主路径逻辑(根据输出结果选择
    either {}
    nullable {}
    )。
  3. 使用
    ensure
    /
    ensureNotNull
    保护不变量。
  4. 与外部代码互操作:
    • 使用
      catch
      /
      Either.catchOrThrow
      将预期异常转换为类型化错误。
  5. 使用
    .bind()
    进行组合,并用
    withError
    recover
    转换错误。
  6. 针对跨字段或列表的验证,使用累积功能(
    zipOrAccumulate
    ,
    mapOrAccumulate
    accumulate
    )。
  7. 在模块边界暴露包装类型;尽可能在内部保留Raise的使用。

Patterns to apply

适用模式

  • Smart constructors (parse, don't validate): make constructors private; expose
    invoke
    returning
    Either
    .
  • Validation accumulation: use
    EitherNel
    and
    zipOrAccumulate
    /
    mapOrAccumulate
    .
  • Wrapper choice:
    • Prefer nullable for optional values unless nested nullability issues exist; then use
      Option
      .
    • Use
      Either
      for disjoint success/failure;
      Ior
      only when success + warning is meaningful.
    • Use
      Result
      only when errors are
      Throwable
      and interop with stdlib APIs is required.
  • Error translation across layers: use
    withError
    to map inner errors to outer domain errors.
  • Drop error types explicitly: wrap
    bind
    with
    ignoreErrors
    when moving from informative error types to
    Option
    /nullable flows.
  • 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
    • 仅当错误为
      Throwable
      且需要与标准库API互操作时使用
      Result
  • 跨层错误转换:使用
    withError
    将内部错误映射为外部领域错误。
  • 显式丢弃错误类型:当从包含信息的错误类型切换到
    Option
    /可空类型流程时,用
    ignoreErrors
    包装
    bind
  • 避免密封类型的继承嵌套:使用组合(包装数据类)而非密封继承链来建模错误层级。

References

参考资料

  • Load
    references/typed-errors.md
    for API details, error accumulation, wrapper-specific guidance, and sample snippets.
  • 查看
    references/typed-errors.md
    获取API细节、错误累积、特定包装类型的指南以及代码示例片段。