cats-mtl-typed-errors
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCats 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 in functions that can raise errors.
Raise[F, E] - Use /
Handle.allow(Scala 3) orrescue(Scala 2) to introduce a scoped error capability and handle it like try/catch.Handle.allowF - Prefer Cats MTL over and avoid
IO[Either[E, A]]; pure functions returningEitherT[IO, E, A]are fine at API boundaries.Either[E, A] - is optional: you can write
F[_]-specific code or keepIOfor polymorphism, depending on the project.F[_]
- 定义领域错误类型;可根据上下文决定是否继承Throwable。
- 在可能抛出错误的函数中使用Cats MTL的。
Raise[F, E] - 使用/
Handle.allow(Scala 3)或rescue(Scala 2)引入作用域化错误处理能力,类似try/catch的方式进行处理。Handle.allowF - 优先使用Cats MTL而非,避免使用
IO[Either[E, A]];在API边界处,返回EitherT[IO, E, A]的纯函数是合适的。Either[E, A] - 为可选项:可根据项目需求编写仅针对
F[_]的代码,或保留IO以实现多态性。F[_]
Workflow
工作流程
- Model domain errors as sealed ADTs (Scala 2) or enums (Scala 3)
- For effectful code that can raise errors, require (and
Raise[F, E]orMonad[F]).Applicative[F] - Raise errors with and return successful values with
.raise.pure - At a boundary, use (Scala 3) or
Handle.allow(Scala 2) to create a scope where raises are valid.Handle.allowF - Close the scope with to handle each error case explicitly.
.rescue - Keep Cats Effect resource and concurrency semantics intact by staying in the monofunctor error channel.
- 将领域错误建模为密封ADT(Scala 2)或枚举(Scala 3)
- 对于可能抛出错误的有副作用代码,需要(以及
Raise[F, E]或Monad[F])。Applicative[F] - 使用抛出错误,使用
.raise返回成功值。pure - 在边界处,使用(Scala 3)或
Handle.allow(Scala 2)创建一个允许抛出错误的作用域。Handle.allowF - 使用关闭作用域,显式处理每个错误场景。
.rescue - 保持在单子函子错误通道内,以保留Cats Effect的资源与并发语义。
Patterns to apply
适用模式
- Typed errors in signatures: treat the error type parameter as the checked-exception channel in the function signature.
E - Scoped error capabilities: require in functions that can fail; use
Raise[F, E]when you also need to recover.Handle[F, E] - Scala 3 ergonomics: prefer and context functions with
using; type inference is significantly better.allow - Scala 2 compatibility: use and explicit implicit parameters; expect more braces and explicit types.
allowF - Interop with pure code: use pure for parsing/validation and lift into
Either[E, A]where needed.F - Avoid transformer stacks: do not reach for just to get a typed error channel; Cats MTL provides the capability without the stack.
EitherT - 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 - 避免转换器栈:不要仅为了获取类型化错误通道而使用;Cats MTL无需栈即可提供该能力。
EitherT - 避免密封类嵌套继承:使用组合(包装样例类)建模错误层级,而非密封继承链。
References
参考资料
- Load for detailed guidance, Scala 2/3 syntax, and rationale from the Typelevel article.
references/custom-error-types.md
- 加载以获取详细指南、Scala 2/3语法示例,以及Typelevel文章中的设计原理。
references/custom-error-types.md