refactor: remove anyhow dependency, replace with thiserror in db.rs and auth_factory.rs
This commit is contained in:
Generated
-1
@@ -1815,7 +1815,6 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
name = "oxicloud"
|
||||
version = "0.5.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"argon2",
|
||||
"async-compression",
|
||||
"async-stream",
|
||||
|
||||
@@ -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"] }
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use anyhow::Result;
|
||||
use sqlx::PgPool;
|
||||
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::services::auth_application_service::AuthApplicationService;
|
||||
use crate::application::services::folder_service::FolderService;
|
||||
|
||||
+16
-10
@@ -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<T> = std::result::Result<T, DbError>;
|
||||
|
||||
/// 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<DbPools> {
|
||||
// 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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user