Are there any strategies for raising an error through a lambda, something like raising through the transaction block of sqldelight?
Kev
10/19/2024, 7:28 AM
Copy code
suspend fun persist(foo: String): Either<Error, Unit> = either {
// Raise context may escape through this lambda
database.transaction {
val id = repository.insertFoo(foo).bind()
if (id == 0) {
rollback()
raise(Error("Failed to insert foo"))
}
}
}
a
Alejandro Serrano.Mena
10/21/2024, 7:26 AM
this depends a lot on what
transaction
actually does. If the transaction is run in place, then doing what you do is fine
otherwise, another option is to hook at some kind of callback (which I don't know if they exist in SQLDelight). Something along the lines of:
Copy code
database.transaction {
val id = repository.insertFoo(foo).bind()
if (id == 0) rollback()
}.onRollback { raise(...) }
k
Kev
10/21/2024, 9:28 AM
Thanks, the former is what I wanted to achieve however i got concerned with the linter raising a warning that raise may escape. Thanks @Alejandro Serrano.Mena
a
Alejandro Serrano.Mena
10/21/2024, 9:29 AM
the linter is a best-effort we've built; if you feel like we should take the case of
transaction
better into account, feel free to open an issue and we can explore it