From 77fc9bba470a808df6395c9557f26d9555ba83a2 Mon Sep 17 00:00:00 2001 From: Dionisio Date: Tue, 3 Mar 2026 11:11:39 +0100 Subject: [PATCH] refactor: remove anyhow dependency, replace with thiserror in db.rs and auth_factory.rs --- Cargo.lock | 1 - Cargo.toml | 1 - src/infrastructure/auth_factory.rs | 3 ++- src/infrastructure/db.rs | 26 ++++++++++++++++---------- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 61150d9d..9c068ca5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1815,7 +1815,6 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" name = "oxicloud" version = "0.5.0" dependencies = [ - "anyhow", "argon2", "async-compression", "async-stream", diff --git a/Cargo.toml b/Cargo.toml index 768ddf46..9b228692 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,6 @@ async-trait = "0.1.89" thiserror = "2.0.18" mockall = { version = "0.14.0", optional = true } sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio", "tls-rustls", "chrono", "uuid", "json"] } -anyhow = "1.0.102" jsonwebtoken = { version = "10.3.0", features = ["rust_crypto"] } argon2 = "0.5.3" rand_core = { version = "0.6", features = ["std", "getrandom"] } diff --git a/src/infrastructure/auth_factory.rs b/src/infrastructure/auth_factory.rs index 489a3932..30b135f6 100644 --- a/src/infrastructure/auth_factory.rs +++ b/src/infrastructure/auth_factory.rs @@ -1,7 +1,8 @@ -use anyhow::Result; use sqlx::PgPool; use std::sync::Arc; +type Result = std::result::Result>; + use crate::application::ports::auth_ports::TokenServicePort; use crate::application::services::auth_application_service::AuthApplicationService; use crate::application::services::folder_service::FolderService; diff --git a/src/infrastructure/db.rs b/src/infrastructure/db.rs index f153caae..af88b24d 100644 --- a/src/infrastructure/db.rs +++ b/src/infrastructure/db.rs @@ -1,8 +1,14 @@ use crate::common::config::AppConfig; -use anyhow::Result; use sqlx::{PgPool, postgres::PgPoolOptions}; use std::time::Duration; +/// Database initialization error. +#[derive(Debug, thiserror::Error)] +#[error("{0}")] +pub struct DbError(String); + +type Result = std::result::Result; + /// Segmented database pools. /// /// `primary` is used for all user-facing request paths (REST, WebDAV, CalDAV, @@ -45,11 +51,11 @@ pub async fn create_database_pools(config: &AppConfig) -> Result { // Apply schema through the primary pool (idempotent) tracing::info!("Applying database schema..."); if let Err(e) = apply_schema(&primary).await { - return Err(anyhow::anyhow!( + return Err(DbError(format!( "Database schema could not be applied: {}. \ Run manually: psql -f db/schema.sql", e - )); + ))); } tracing::info!("Database schema applied successfully"); @@ -118,11 +124,11 @@ async fn create_pool_with_retries( Err(e) => { tracing::error!("Error verifying {} pool connection: {}", label, e); if attempt >= MAX_ATTEMPTS { - return Err(anyhow::anyhow!( + return Err(DbError(format!( "Error verifying PostgreSQL {} pool connection: {}", label, e - )); + ))); } } }, @@ -135,22 +141,22 @@ async fn create_pool_with_retries( e ); if attempt >= MAX_ATTEMPTS { - return Err(anyhow::anyhow!( + return Err(DbError(format!( "Error in PostgreSQL {} pool connection: {}", label, e - )); + ))); } tokio::time::sleep(Duration::from_secs(2)).await; } } } - Err(anyhow::anyhow!( + Err(DbError(format!( "Could not establish PostgreSQL {} pool connection after {} attempts", label, MAX_ATTEMPTS - )) + ))) } /// Apply the embedded schema.sql to the database. @@ -189,7 +195,7 @@ async fn apply_schema(pool: &PgPool) -> Result<()> { e, preview ); - return Err(anyhow::anyhow!("Schema statement {} failed: {}", i + 1, e)); + return Err(DbError(format!("Schema statement {} failed: {}", i + 1, e))); } }