cats-mtl-typed-errors

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cats MTL Typed Errors (Scala)

Cats MTL 类型化错误(Scala)

Quick start

快速入门

  • Define a domain error type; it may or may not extend Throwable depending on context.
  • Use Cats MTL
    Raise[F, E]
    in functions that can raise errors.
  • Use
    Handle.allow
    /
    rescue
    (Scala 3) or
    Handle.allowF
    (Scala 2) to introduce a scoped error capability and handle it like try/catch.
  • Prefer Cats MTL over
    IO[Either[E, A]]
    and avoid
    EitherT[IO, E, A]
    ; pure functions returning
    Either[E, A]
    are fine at API boundaries.
  • F[_]
    is optional: you can write
    IO
    -specific code or keep
    F[_]
    for polymorphism, depending on the project.
  • 定义领域错误类型;可根据上下文决定是否继承Throwable。
  • 在可能抛出错误的函数中使用Cats MTL的
    Raise[F, E]
  • 使用
    Handle.allow
    /
    rescue
    (Scala 3)或
    Handle.allowF
    (Scala 2)引入作用域化错误处理能力,类似try/catch的方式进行处理。
  • 优先使用Cats MTL而非
    IO[Either[E, A]]
    ,避免使用
    EitherT[IO, E, A]
    ;在API边界处,返回
    Either[E, A]
    的纯函数是合适的。
  • F[_]
    为可选项:可根据项目需求编写仅针对
    IO
    的代码,或保留
    F[_]
    以实现多态性。

Workflow

工作流程

  1. Model domain errors as sealed ADTs (Scala 2) or enums (Scala 3)
  2. For effectful code that can raise errors, require
    Raise[F, E]
    (and
    Monad[F]
    or
    Applicative[F]
    ).
  3. Raise errors with
    .raise
    and return successful values with
    pure
    .
  4. At a boundary, use
    Handle.allow
    (Scala 3) or
    Handle.allowF
    (Scala 2) to create a scope where raises are valid.
  5. Close the scope with
    .rescue
    to handle each error case explicitly.
  6. Keep Cats Effect resource and concurrency semantics intact by staying in the monofunctor error channel.
  1. 将领域错误建模为密封ADT(Scala 2)或枚举(Scala 3)
  2. 对于可能抛出错误的有副作用代码,需要
    Raise[F, E]
    (以及
    Monad[F]
    Applicative[F]
    )。
  3. 使用
    .raise
    抛出错误,使用
    pure
    返回成功值。
  4. 在边界处,使用
    Handle.allow
    (Scala 3)或
    Handle.allowF
    (Scala 2)创建一个允许抛出错误的作用域。
  5. 使用
    .rescue
    关闭作用域,显式处理每个错误场景。
  6. 保持在单子函子错误通道内,以保留Cats Effect的资源与并发语义。

Patterns to apply

适用模式

  • Typed errors in signatures: treat the error type parameter
    E
    as the checked-exception channel in the function signature.
  • Scoped error capabilities: require
    Raise[F, E]
    in functions that can fail; use
    Handle[F, E]
    when you also need to recover.
  • Scala 3 ergonomics: prefer
    using
    and context functions with
    allow
    ; type inference is significantly better.
  • Scala 2 compatibility: use
    allowF
    and explicit implicit parameters; expect more braces and explicit types.
  • Interop with pure code: use pure
    Either[E, A]
    for parsing/validation and lift into
    F
    where needed.
  • Avoid transformer stacks: do not reach for
    EitherT
    just to get a typed error channel; Cats MTL provides the capability without the stack.
  • Avoid sealed-on-sealed inheritance: model error hierarchies with composition (wrapper case classes), not sealed inheritance chains.
  • 签名中的类型化错误:将错误类型参数
    E
    视为函数签名中的受检异常通道。
  • 作用域化错误处理能力:在可能失败的函数中要求
    Raise[F, E]
    ;当需要恢复时使用
    Handle[F, E]
  • Scala 3 易用性:优先使用
    using
    及结合
    allow
    的上下文函数;类型推断能力显著提升。
  • Scala 2 兼容性:使用
    allowF
    及显式隐式参数;会需要更多大括号与显式类型声明。
  • 与纯代码互操作:使用纯
    Either[E, A]
    进行解析/验证,并在需要时提升至
    F
    中。
  • 避免转换器栈:不要仅为了获取类型化错误通道而使用
    EitherT
    ;Cats MTL无需栈即可提供该能力。
  • 避免密封类嵌套继承:使用组合(包装样例类)建模错误层级,而非密封继承链。

References

参考资料

  • Load
    references/custom-error-types.md
    for detailed guidance, Scala 2/3 syntax, and rationale from the Typelevel article.
  • 加载
    references/custom-error-types.md
    以获取详细指南、Scala 2/3语法示例,以及Typelevel文章中的设计原理。