User defined error from transactions? #4658
-
It seemed like in #2342 that a change proposal maybe could have been accepted if both Box-ing and NewType-ing the error would have been okay? In general the use case seems valid as it seems like they are performing some "check" in a third-party library. Similar to that is my use case of checking the "etag" or "epoch" of an entity before allowing an update. Short of being able to box an error within a diesel error, it seems like it should be acceptable usage for me to just simply do the following and try extracting the error from something that implements let etag: String = things.find(id).select(etag).first(conn)?;
if !etag.eq(expected_etag) {
return Err(Error::DatabaseError(
// Or use DatabaseErrorKind::Unknown?
// Or should there be a DatabaseErrorKind::Custom?
DatabaseErrorKind::CheckViolation,
// User expected to retry with new etag
Box::new(String::from("Aborted/etag mismatch")),
)); I would however find it nice to be able to do something like the following instead and try downcasting the error after the transaction: let etag: String = things.find(id).select(etag).first(conn)?;
if !etag.eq(expected_etag) {
return Err(Error::DatabaseError(
DatabaseErrorKind::Custom,
// User expected to retry with new etag
Box::new(Status::aborted("etag mismatch"),
)); Perhaps of course this perhaps could be done as a trigger where possible, but the Is something like The complication I see with creating a concrete implementation of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
I'm not sure I can follow you there. The documentation lists the following function signature: fn transaction<F, R, E>(conn: &mut Conn, callback: F) -> Result<R, E>
where
F: FnOnce(&mut Conn) -> Result<R, E>,
E: From<Error>, As can be easily seen there there is no constraint there to return a Finally that all written: Leaving beside the details above, your post only describes a problem. It doesn't propose a solution, it's not even clear from your posting how such a solution could even look like, as it essentially only says: It doesn't work like I want it to work, which is not helpful for anyone but you. For a formal feature request I would expect that you outline the problem, describe how that problem affects a significant part of the diesel user base, outline possible solutions and there advantages/disadvantages. Most of that is missing here. If you don't have such details, please fill such questions as support question in the future. |
Beta Was this translation helpful? Give feedback.
I'm still not able to follow you on why you need to use
diesel::result::Error
as error type there. It is designed to be used as diesel error type, not as general error type for any other application. From this design perspective the first suggested solution doesn't seem meaningful.For me the second solution is completely unclear.
To clarify that: This
E: From<diesel::result::Error>
impl is exactly used for that is case of an error generated by interacting the the transaction (and not the closure) itself. That can also fail due to network errors, etc like any other datab…