Skip to content

Commit 71f8029

Browse files
committed
Auto merge of #3887 - Turbo87:thiserror, r=pietroalbini
db: Use `thiserror` crate to derive `Error` for `PoolError` see https://docs.rs/thiserror/
2 parents c00bc1c + 2cc0598 commit 71f8029

File tree

3 files changed

+8
-14
lines changed

3 files changed

+8
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ sha2 = "0.9"
8181
swirl = { git = "https://github.com/sgrif/swirl.git", rev = "e87cf37" }
8282
tar = "0.4.16"
8383
tempfile = "3"
84+
thiserror = "1.0.28"
8485
tokio = { version = "1.5.0", features = ["net", "signal", "io-std", "io-util", "rt-multi-thread", "macros"]}
8586
toml = "0.5"
8687
tracing = "0.1"

src/db.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use parking_lot::{ReentrantMutex, ReentrantMutexGuard};
55
use prometheus::Histogram;
66
use std::sync::Arc;
77
use std::{ops::Deref, time::Duration};
8+
use thiserror::Error;
89
use url::Url;
910

1011
use crate::middleware::app::RequestApp;
@@ -69,7 +70,7 @@ impl DieselPool {
6970
} else if !self.is_healthy() {
7071
Err(PoolError::UnhealthyPool)
7172
} else {
72-
Ok(DieselPooledConn::Pool(pool.get().map_err(PoolError::R2D2)?))
73+
Ok(DieselPooledConn::Pool(pool.get()?))
7374
}
7475
}),
7576
DieselPool::Test(conn) => Ok(DieselPooledConn::Test(conn.lock())),
@@ -200,19 +201,10 @@ pub(crate) fn test_conn() -> PgConnection {
200201
conn
201202
}
202203

203-
#[derive(Debug)]
204+
#[derive(Debug, Error)]
204205
pub enum PoolError {
205-
R2D2(r2d2::PoolError),
206+
#[error(transparent)]
207+
R2D2(#[from] r2d2::PoolError),
208+
#[error("unhealthy database pool")]
206209
UnhealthyPool,
207210
}
208-
209-
impl std::error::Error for PoolError {}
210-
211-
impl std::fmt::Display for PoolError {
212-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
213-
match self {
214-
PoolError::R2D2(err) => write!(f, "{}", err),
215-
PoolError::UnhealthyPool => write!(f, "unhealthy database pool"),
216-
}
217-
}
218-
}

0 commit comments

Comments
 (0)