Skip to content

Instantly share code, notes, and snippets.

@supermanue
Created March 20, 2022 15:55
Show Gist options
  • Save supermanue/ccaa8f5850ce67f589658184114c079a to your computer and use it in GitHub Desktop.
Save supermanue/ccaa8f5850ce67f589658184114c079a to your computer and use it in GitHub Desktop.
Better error handling
#BEFORE
def get(id: Int): Task[User] =
SQL
.get(id)
.option
.transact(tnx)
.foldM(
err => Task.fail(err),
maybeUser => Task.require(UserNotFound(id))(Task.succeed(maybeUser))
)
#NOW
def get(id: Int): IO[AppError, User] =
SQL
.get(id)
.option
.transact(tnx)
.foldM(
err => IO.fail(DBError(err.getMessage)),
maybeUser => IO.require(UserNotFound(id))(Task.succeed(maybeUser))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment