Skip to content

Commit a723304

Browse files
prestwichclaude
andcommitted
refactor(storage): use connector-specific errors, remove Config variants
Creates dedicated error types for connector initialization: - MdbxConnectorError in cold-mdbx crate - SqlConnectorError in cold-sql crate Removes Config(String) variants from MdbxError and SqlColdError, which were only used for from_env() methods. The new connector error types are more specific and properly handle missing environment variables. ConfigError now uses From implementations to convert from connector errors, simplifying error handling in the builder. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 868d8e1 commit a723304

File tree

8 files changed

+51
-35
lines changed

8 files changed

+51
-35
lines changed

crates/cold-mdbx/src/connector.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ use signet_hot::HotConnect;
88
use signet_hot_mdbx::{DatabaseArguments, DatabaseEnv, MdbxError};
99
use std::path::PathBuf;
1010

11+
/// Errors that can occur when initializing MDBX connectors.
12+
#[derive(Debug, thiserror::Error)]
13+
pub enum MdbxConnectorError {
14+
/// Missing environment variable.
15+
#[error("missing environment variable: {0}")]
16+
MissingEnvVar(&'static str),
17+
18+
/// Hot storage initialization failed.
19+
#[error("hot storage initialization failed: {0}")]
20+
HotInit(#[from] MdbxError),
21+
22+
/// Cold storage initialization failed.
23+
#[error("cold storage initialization failed: {0}")]
24+
ColdInit(#[from] MdbxColdError),
25+
}
26+
1127
/// Connector for MDBX storage (both hot and cold).
1228
///
1329
/// This unified connector can open MDBX databases for both hot and cold storage.
@@ -65,15 +81,14 @@ impl MdbxConnector {
6581
/// # Example
6682
///
6783
/// ```ignore
68-
/// use signet_hot_mdbx::MdbxConnector;
84+
/// use signet_cold_mdbx::MdbxConnector;
6985
///
7086
/// let hot = MdbxConnector::from_env("SIGNET_HOT_PATH")?;
7187
/// let cold = MdbxConnector::from_env("SIGNET_COLD_PATH")?;
7288
/// ```
73-
pub fn from_env(env_var: &str) -> Result<Self, MdbxError> {
74-
let path: PathBuf = std::env::var(env_var)
75-
.map_err(|_| MdbxError::Config(format!("missing environment variable: {env_var}")))?
76-
.into();
89+
pub fn from_env(env_var: &'static str) -> Result<Self, MdbxConnectorError> {
90+
let path: PathBuf =
91+
std::env::var(env_var).map_err(|_| MdbxConnectorError::MissingEnvVar(env_var))?.into();
7792
Ok(Self::new(path))
7893
}
7994
}

crates/cold-mdbx/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ mod backend;
4747
pub use backend::MdbxColdBackend;
4848

4949
mod connector;
50-
pub use connector::MdbxConnector;
50+
pub use connector::{MdbxConnector, MdbxConnectorError};
5151

5252
pub use signet_hot_mdbx::{DatabaseArguments, DatabaseEnvKind};

crates/cold-sql/src/connector.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
use crate::{SqlColdBackend, SqlColdError};
44
use signet_cold::ColdConnect;
55

6+
/// Errors that can occur when initializing SQL connectors.
7+
#[derive(Debug, thiserror::Error)]
8+
pub enum SqlConnectorError {
9+
/// Missing environment variable.
10+
#[error("missing environment variable: {0}")]
11+
MissingEnvVar(&'static str),
12+
13+
/// Cold storage initialization failed.
14+
#[error("cold storage initialization failed: {0}")]
15+
ColdInit(#[from] SqlColdError),
16+
}
17+
618
/// Connector for SQL cold storage (PostgreSQL or SQLite).
719
///
820
/// Automatically detects the database type from the URL:
@@ -53,10 +65,8 @@ impl SqlConnector {
5365
///
5466
/// let cold = SqlConnector::from_env("SIGNET_COLD_SQL_URL")?;
5567
/// ```
56-
pub fn from_env(env_var: &str) -> Result<Self, SqlColdError> {
57-
let url = std::env::var(env_var).map_err(|_| {
58-
SqlColdError::Config(format!("missing environment variable: {env_var}"))
59-
})?;
68+
pub fn from_env(env_var: &'static str) -> Result<Self, SqlConnectorError> {
69+
let url = std::env::var(env_var).map_err(|_| SqlConnectorError::MissingEnvVar(env_var))?;
6070
Ok(Self::new(url))
6171
}
6272
}

crates/cold-sql/src/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ pub enum SqlColdError {
1010
/// A data conversion error occurred.
1111
#[error("conversion error: {0}")]
1212
Convert(String),
13-
14-
/// Configuration error.
15-
#[error("configuration error: {0}")]
16-
Config(String),
1713
}
1814

1915
impl From<SqlColdError> for signet_cold::ColdStorageError {

crates/cold-sql/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub use backend::SqlColdBackend;
5050
#[cfg(any(feature = "sqlite", feature = "postgres"))]
5151
mod connector;
5252
#[cfg(any(feature = "sqlite", feature = "postgres"))]
53-
pub use connector::SqlConnector;
53+
pub use connector::{SqlConnector, SqlConnectorError};
5454

5555
/// Backward-compatible alias for [`SqlColdBackend`] when using SQLite.
5656
#[cfg(feature = "sqlite")]

crates/hot-mdbx/src/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ pub enum MdbxError {
5555
/// Deser.
5656
#[error(transparent)]
5757
Deser(#[from] DeserError),
58-
59-
/// Configuration error.
60-
#[error("configuration error: {0}")]
61-
Config(String),
6258
}
6359

6460
impl trevm::revm::database::DBErrorMarker for MdbxError {}

crates/storage/src/builder.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,21 @@ impl StorageBuilder<MdbxConnector, EnvColdConnector> {
123123
/// Exactly one cold backend must be specified.
124124
pub fn from_env() -> Result<Self, ConfigError> {
125125
// Hot connector from environment (always MDBX)
126-
let hot_connector = MdbxConnector::from_env(ENV_HOT_PATH)
127-
.map_err(|e| ConfigError::ConnectorError { connector: "hot", error: e.to_string() })?;
126+
let hot_connector = MdbxConnector::from_env(ENV_HOT_PATH)?;
128127

129128
// Determine cold backend from environment
130129
let has_mdbx = env::var(ENV_COLD_PATH).is_ok();
131130
let has_sql = env::var(ENV_COLD_SQL_URL).is_ok();
132131

133132
let cold_connector = match (has_mdbx, has_sql) {
134133
(true, false) => {
135-
let mdbx = MdbxConnector::from_env(ENV_COLD_PATH).map_err(|e| {
136-
ConfigError::ConnectorError { connector: "cold MDBX", error: e.to_string() }
137-
})?;
134+
let mdbx = MdbxConnector::from_env(ENV_COLD_PATH)?;
138135
Either::left(mdbx)
139136
}
140137
(false, true) => {
141138
#[cfg(any(feature = "postgres", feature = "sqlite"))]
142139
{
143-
let sql = SqlConnector::from_env(ENV_COLD_SQL_URL).map_err(|e| {
144-
ConfigError::ConnectorError { connector: "cold SQL", error: e.to_string() }
145-
})?;
140+
let sql = SqlConnector::from_env(ENV_COLD_SQL_URL)?;
146141
Either::right(sql)
147142
}
148143
#[cfg(not(any(feature = "postgres", feature = "sqlite")))]

crates/storage/src/config.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
//!
1414
//! Exactly one of `SIGNET_COLD_PATH` or `SIGNET_COLD_SQL_URL` must be set.
1515
16+
use signet_cold_mdbx::MdbxConnectorError;
1617
use thiserror::Error;
1718

19+
#[cfg(any(feature = "postgres", feature = "sqlite"))]
20+
use signet_cold_sql::SqlConnectorError;
21+
1822
/// Environment variable name for hot storage path.
1923
pub const ENV_HOT_PATH: &str = "SIGNET_HOT_PATH";
2024

@@ -48,12 +52,12 @@ pub enum ConfigError {
4852
env_var: &'static str,
4953
},
5054

51-
/// Connector initialization error.
52-
#[error("{connector} connector error: {error}")]
53-
ConnectorError {
54-
/// The connector type (e.g., "hot", "cold MDBX", "cold SQL").
55-
connector: &'static str,
56-
/// The underlying error message.
57-
error: String,
58-
},
55+
/// MDBX connector error.
56+
#[error("MDBX connector error: {0}")]
57+
MdbxConnector(#[from] MdbxConnectorError),
58+
59+
/// SQL connector error.
60+
#[cfg(any(feature = "postgres", feature = "sqlite"))]
61+
#[error("SQL connector error: {0}")]
62+
SqlConnector(#[from] SqlConnectorError),
5963
}

0 commit comments

Comments
 (0)