Skip to content

Commit

Permalink
Fix transaction function error doesn't make operation fail
Browse files Browse the repository at this point in the history
  • Loading branch information
ivnsch committed Jul 17, 2020
1 parent b8af5fd commit ccaff4e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,20 @@ impl Database {
let mut conn = expect_log!(conn_res, "Couldn't lock connection");

let t = conn.transaction()?;
if f(&t).is_ok() {
t.commit()
} else {
t.rollback()
match f(&t) {
Ok(_) => t.commit().map_err(ServicesError::from),
Err(commit_error) => {
let rollback_res = t.rollback();
if rollback_res.is_err() {
// As we're already returning error status, show only a log for rollback error.
error!(
"There was an error committing and rollback failed too with: {:?}",
rollback_res
);
}
Err(commit_error)
}
}
.map_err(ServicesError::from)
}

pub fn new(conn: Connection) -> Database {
Expand Down

0 comments on commit ccaff4e

Please sign in to comment.