Tweak AsyncConnection.transation
to return custom error types
#21
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently,
AsyncConnection.transaction
is hard-coded to only allow a closure that returns anAsyncResult
. This PR takes a cue fromdiesel::Connection.transaction
and allows it to return aResult
with any error type-- as long as it implementsFrom<diesel::result::Error>
.To implement it, I added a small utility type (
TransactionError<E>
) that simply wraps an error type, the unwraps it after the transaction completes. We need this so we can go fromdiesel::result::Error
toAsyncError
. There may be better ways to solve this, but this was the first solution that came to mind.I found that allowing any error type made it a lot easier to work with transactions (especially when using
anyhow
), and this seems like a net-positive from an ergonomics perspective. I'm open to suggestions for other possible improvements for this PR though!