Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support error type elimination within try-catch clauses #22

Open
rfmejia opened this issue Dec 19, 2022 · 0 comments
Open

Support error type elimination within try-catch clauses #22

rfmejia opened this issue Dec 19, 2022 · 0 comments

Comments

@rfmejia
Copy link

rfmejia commented Dec 19, 2022

To improve the ergonomics of error handling, it would be nice if we could eliminate some union types from the error channel when explicitly handled in a catch block. Maybe we can simulate the symantics of ZIO.catchSome and ZIO.die:

import zio.*
import zio.direct.*

case class ID()
case class Person()
case class ParseError()
case class DBError()

def parseId(s: String): IO[ParseError, ID] = ???
def getPerson(id: ID): IO[DBError, Person] = ???

val t: IO[DBError, Person] = defer {
  try {
    val id = parseId("1").run
    getPerson(id).run
  } // ZIO[Any, DBError | ParseError, Person]
  catch { 
    case v: ParseError => Person()  // eliminate ParseError we return a subtype of Person (simulates ZIO.catchSome)
    case v: ParseError => ZIO.die(???)  // alternatively, terminate the fiber with a defect
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant