Replace StoreFactoryCreationError with InternalError
@@ -28,6 +28,8 @@ use self::sqlite::ForeignKeyCustomizer;
#[cfg(feature = "diesel")]
use diesel::r2d2::{ConnectionManager, Pool};
+use crate::error::InternalError;
+
/// An abstract factory for creating Splinter stores backed by the same storage
pub trait StoreFactory {
/// Get a new `CredentialsStore`
@@ -61,14 +63,17 @@ pub trait StoreFactory {
/// created by the resulting factory
pub fn create_store_factory(
connection_uri: ConnectionUri,
-) -> Result<Box<dyn StoreFactory>, StoreFactoryCreationError> {
+) -> Result<Box<dyn StoreFactory>, InternalError> {
match connection_uri {
ConnectionUri::Memory => Ok(Box::new(memory::MemoryStoreFactory::new())),
#[cfg(feature = "postgres")]
ConnectionUri::Postgres(url) => {
let connection_manager = ConnectionManager::<diesel::pg::PgConnection>::new(url);
let pool = Pool::builder().build(connection_manager).map_err(|err| {
- StoreFactoryCreationError(format!("Failed to build connection pool: {}", err))
+ InternalError::from_source_with_prefix(
+ Box::new(err),
+ "Failed to build connection pool".to_string(),
+ )
})?;
Ok(Box::new(postgres::PgStoreFactory::new(pool)))
}
@@ -85,25 +90,16 @@ pub fn create_store_factory(
pool_builder = pool_builder.max_size(1);
}
let pool = pool_builder.build(connection_manager).map_err(|err| {
- StoreFactoryCreationError(format!("Failed to build connection pool: {}", err))
+ InternalError::from_source_with_prefix(
+ Box::new(err),
+ "Failed to build connection pool".to_string(),
+ )
})?;
Ok(Box::new(sqlite::SqliteStoreFactory::new(pool)))
}
}
}
-/// Errors raised by trying to create a `StoreFactory`
-#[derive(Debug)]
-pub struct StoreFactoryCreationError(pub String);
-
-impl std::error::Error for StoreFactoryCreationError {}
-
-impl std::fmt::Display for StoreFactoryCreationError {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- write!(f, "Unable to create store factory: {}", self.0)
- }
-}
-
/// The possible connection types and identifiers for a `StoreFactory`
pub enum ConnectionUri {
Memory,
