Created
March 20, 2022 15:55
-
-
Save supermanue/ccaa8f5850ce67f589658184114c079a to your computer and use it in GitHub Desktop.
Better error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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