cats-mtl-typed-errors
Installation
SKILL.md
Cats MTL Typed Errors (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) orHandle.allowF(Scala 2) to introduce a scoped error capability and handle it like try/catch. - Prefer Cats MTL over
IO[Either[E, A]]and avoidEitherT[IO, E, A]; pure functions returningEither[E, A]are fine at API boundaries. F[_]is optional: you can writeIO-specific code or keepF[_]for polymorphism, depending on the project.
Workflow
- Model domain errors as sealed ADTs (Scala 2) or enums (Scala 3)
- For effectful code that can raise errors, require
Raise[F, E](andMonad[F]orApplicative[F]). - Raise errors with
.raiseand return successful values withpure. - At a boundary, use
Handle.allow(Scala 3) orHandle.allowF(Scala 2) to create a scope where raises are valid. - Close the scope with
.rescueto handle each error case explicitly. - Keep Cats Effect resource and concurrency semantics intact by staying in the monofunctor error channel.