From d98ecd58af9f6d850461b8bef430dfef70111692 Mon Sep 17 00:00:00 2001 From: John Oxley Date: Thu, 14 May 2026 22:53:32 +0100 Subject: refactor: Implement From and clean up fix_error (#3484) In the database crates for atuin-server, there is `fn fix_error`. This PR implements `From` on `DbError` which makes it possible to mostly use `?` to bubble up the errors. There are cases where `?` is not being used e.g. ```rust async fn get_session(&self, token: &str) -> DbResult { sqlx::query_as("select id, user_id, token from sessions where token = $1") .bind(token) .fetch_one(&self.pool) .await .map_err(fix_error) .map(|DbSession(session)| session) } ``` There are two options ## 1. Use `Into::into` ```rust async fn get_session(&self, token: &str) -> DbResult { sqlx::query_as("select id, user_id, token from sessions where token = $1") .bind(token) .fetch_one(&self.pool) .await .map_err(fix_error) .map(|DbSession(session)| session) } ``` ## 2. Create a variable and use `?` ```rust async fn get_session(&self, token: &str) -> DbResult { let session = sqlx::query_as("select id, user_id, token from sessions where token = $1") .bind(token) .fetch_one(&self.pool) .await .map(|DbSession(session)| session)?; Ok(session) } ``` I chose to do option 1 as it was just a find/replace but say the word and I'll convert them all to option 2 ## Checks - [X] I am happy for maintainers to push small adjustments to this PR, to speed up the review cycle - [X] I have checked that there are no existing pull requests for the same thing --- crates/atuin-server-database/Cargo.toml | 7 ++++--- crates/atuin-server-database/src/lib.rs | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'crates/atuin-server-database') diff --git a/crates/atuin-server-database/Cargo.toml b/crates/atuin-server-database/Cargo.toml index e361c68b..52ccbf97 100644 --- a/crates/atuin-server-database/Cargo.toml +++ b/crates/atuin-server-database/Cargo.toml @@ -12,9 +12,10 @@ repository = { workspace = true } [dependencies] atuin-common = { path = "../atuin-common", version = "18.16.1" } -tracing = { workspace = true } -time = { workspace = true } +async-trait = { workspace = true } eyre = { workspace = true } serde = { workspace = true } -async-trait = { workspace = true } +sqlx = { workspace = true } +time = { workspace = true } +tracing = { workspace = true } url = "2.5.2" diff --git a/crates/atuin-server-database/src/lib.rs b/crates/atuin-server-database/src/lib.rs index 6000a530..9dd95eef 100644 --- a/crates/atuin-server-database/src/lib.rs +++ b/crates/atuin-server-database/src/lib.rs @@ -31,9 +31,24 @@ impl Display for DbError { } } -impl> From for DbError { - fn from(value: T) -> Self { - DbError::Other(value.into().into()) +impl From for DbError { + fn from(error: time::error::ComponentRange) -> Self { + DbError::Other(error.into()) + } +} + +impl From for DbError { + fn from(error: time::error::Error) -> Self { + DbError::Other(error.into()) + } +} + +impl From for DbError { + fn from(error: sqlx::Error) -> Self { + match error { + sqlx::Error::RowNotFound => DbError::NotFound, + error => DbError::Other(error.into()), + } } } -- cgit v1.3.1