refactor: remove anyhow dependency, replace with thiserror in db.rs and auth_factory.rs

This commit is contained in:
Dionisio
2026-03-03 11:11:39 +01:00
parent efcf88c4d7
commit 77fc9bba47
4 changed files with 18 additions and 13 deletions
Generated
-1
View File
@@ -1815,7 +1815,6 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
name = "oxicloud" name = "oxicloud"
version = "0.5.0" version = "0.5.0"
dependencies = [ dependencies = [
"anyhow",
"argon2", "argon2",
"async-compression", "async-compression",
"async-stream", "async-stream",
-1
View File
@@ -30,7 +30,6 @@ async-trait = "0.1.89"
thiserror = "2.0.18" thiserror = "2.0.18"
mockall = { version = "0.14.0", optional = true } mockall = { version = "0.14.0", optional = true }
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio", "tls-rustls", "chrono", "uuid", "json"] } 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"] } jsonwebtoken = { version = "10.3.0", features = ["rust_crypto"] }
argon2 = "0.5.3" argon2 = "0.5.3"
rand_core = { version = "0.6", features = ["std", "getrandom"] } rand_core = { version = "0.6", features = ["std", "getrandom"] }
+2 -1
View File
@@ -1,7 +1,8 @@
use anyhow::Result;
use sqlx::PgPool; use sqlx::PgPool;
use std::sync::Arc; use std::sync::Arc;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
use crate::application::ports::auth_ports::TokenServicePort; use crate::application::ports::auth_ports::TokenServicePort;
use crate::application::services::auth_application_service::AuthApplicationService; use crate::application::services::auth_application_service::AuthApplicationService;
use crate::application::services::folder_service::FolderService; use crate::application::services::folder_service::FolderService;
+16 -10
View File
@@ -1,8 +1,14 @@
use crate::common::config::AppConfig; use crate::common::config::AppConfig;
use anyhow::Result;
use sqlx::{PgPool, postgres::PgPoolOptions}; use sqlx::{PgPool, postgres::PgPoolOptions};
use std::time::Duration; use std::time::Duration;
/// Database initialization error.
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct DbError(String);
type Result<T> = std::result::Result<T, DbError>;
/// Segmented database pools. /// Segmented database pools.
/// ///
/// `primary` is used for all user-facing request paths (REST, WebDAV, CalDAV, /// `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<DbPools> {
// Apply schema through the primary pool (idempotent) // Apply schema through the primary pool (idempotent)
tracing::info!("Applying database schema..."); tracing::info!("Applying database schema...");
if let Err(e) = apply_schema(&primary).await { if let Err(e) = apply_schema(&primary).await {
return Err(anyhow::anyhow!( return Err(DbError(format!(
"Database schema could not be applied: {}. \ "Database schema could not be applied: {}. \
Run manually: psql -f db/schema.sql", Run manually: psql -f db/schema.sql",
e e
)); )));
} }
tracing::info!("Database schema applied successfully"); tracing::info!("Database schema applied successfully");
@@ -118,11 +124,11 @@ async fn create_pool_with_retries(
Err(e) => { Err(e) => {
tracing::error!("Error verifying {} pool connection: {}", label, e); tracing::error!("Error verifying {} pool connection: {}", label, e);
if attempt >= MAX_ATTEMPTS { if attempt >= MAX_ATTEMPTS {
return Err(anyhow::anyhow!( return Err(DbError(format!(
"Error verifying PostgreSQL {} pool connection: {}", "Error verifying PostgreSQL {} pool connection: {}",
label, label,
e e
)); )));
} }
} }
}, },
@@ -135,22 +141,22 @@ async fn create_pool_with_retries(
e e
); );
if attempt >= MAX_ATTEMPTS { if attempt >= MAX_ATTEMPTS {
return Err(anyhow::anyhow!( return Err(DbError(format!(
"Error in PostgreSQL {} pool connection: {}", "Error in PostgreSQL {} pool connection: {}",
label, label,
e e
)); )));
} }
tokio::time::sleep(Duration::from_secs(2)).await; tokio::time::sleep(Duration::from_secs(2)).await;
} }
} }
} }
Err(anyhow::anyhow!( Err(DbError(format!(
"Could not establish PostgreSQL {} pool connection after {} attempts", "Could not establish PostgreSQL {} pool connection after {} attempts",
label, label,
MAX_ATTEMPTS MAX_ATTEMPTS
)) )))
} }
/// Apply the embedded schema.sql to the database. /// Apply the embedded schema.sql to the database.
@@ -189,7 +195,7 @@ async fn apply_schema(pool: &PgPool) -> Result<()> {
e, e,
preview preview
); );
return Err(anyhow::anyhow!("Schema statement {} failed: {}", i + 1, e)); return Err(DbError(format!("Schema statement {} failed: {}", i + 1, e)));
} }
} }