Skip to content

Commit

Permalink
review(onur): structure the errors instead of returning strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mariocynicys committed Mar 6, 2025
1 parent 01b7085 commit d4b9084
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mm2src/mm2_core/src/mm_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ impl MmCtx {
/// Use this directory for data related to a specific address and only that specific address (e.g. swap data, order data, etc...).
/// This makes sure that when this address is activated using a different technique, this data is still accessible.
#[cfg(all(feature = "new-db-arch", not(target_arch = "wasm32")))]
pub fn address_dir(&self, address: &str) -> Result<PathBuf, String> {
pub fn address_dir(&self, address: &str) -> Result<PathBuf, AddressDataError> {
let path = self.db_root().join("addresses").join(address);
if !path.exists() {
std::fs::create_dir_all(&path).map_err(|e| format!("Failed to create address directory: {}", e))?;
std::fs::create_dir_all(&path).map_err(AddressDataError::CreateAddressDirFailure)?;
}
Ok(path)
}
Expand All @@ -389,10 +389,10 @@ impl MmCtx {

/// Returns a SQL connection to the address database.
#[cfg(all(feature = "new-db-arch", not(target_arch = "wasm32")))]
pub fn address_db(&self, address: &str) -> Result<Connection, String> {
pub fn address_db(&self, address: &str) -> Result<Connection, AddressDataError> {
let path = self.address_dir(address)?.join("MM2.db");
log_sqlite_file_open_attempt(&path);
let connection = try_s!(Connection::open(path));
let connection = Connection::open(path).map_err(AddressDataError::SqliteConnectionFailure)?;
Ok(connection)
}

Expand Down Expand Up @@ -505,6 +505,12 @@ impl Drop for MmCtx {
}
}

#[cfg(all(feature = "new-db-arch", not(target_arch = "wasm32")))]
pub enum AddressDataError {
CreateAddressDirFailure(std::io::Error),
SqliteConnectionFailure(db_common::sqlite::rusqlite::Error),
}

/// Returns the path to the MM database root.
///
/// Path priority:
Expand Down

0 comments on commit d4b9084

Please sign in to comment.