Skip to content

Commit

Permalink
refactor errors
Browse files Browse the repository at this point in the history
  • Loading branch information
brunopgalvao committed Apr 18, 2024
1 parent 744c370 commit 2c69229
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
10 changes: 9 additions & 1 deletion crates/pop-contracts/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ pub enum Error {
#[error("Failed to create new contract project: {0}")]
NewContractFailed(String),
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
IO(#[from] std::io::Error),
#[error("Failed to execute test command: {0}")]
TestCommand(String),
#[error("Failed to parse balance: {0}")]
BalanceParsing(String),
#[error("Failed to parse account address: {0}")]
AccountAddressParsing(String),
#[error("Failed to get manifest path: {0}")]
ManifestPath(String),
}
2 changes: 1 addition & 1 deletion crates/pop-contracts/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn create_smart_contract(name: String, target: &Path) -> Result<(), Error> {
let canonicalized_path = target
.canonicalize()
// If an I/O error occurs during canonicalization, convert it into an Error enum variant.
.map_err(|e| Error::IoError(e))?;
.map_err(|e| Error::IO(e))?;

// Retrieve the parent directory of the canonicalized path.
let parent_path = canonicalized_path
Expand Down
20 changes: 5 additions & 15 deletions crates/pop-contracts/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
use duct::cmd;
use std::path::PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to execute test command: {0}")]
TestCommandError(String),
#[error("Failed to set test directory: {0}")]
TestDirError(String),
#[error("Failed to create test environment: {0}")]
TestEnvironmentError(String),
}
use crate::errors::Error;

pub fn test_smart_contract(path: &Option<PathBuf>) -> Result<(), Error> {
// Execute `cargo test` command in the specified directory.
let result = cmd("cargo", vec!["test"])
.dir(path.clone().unwrap_or_else(|| PathBuf::from("./")))
.run()
.map_err(|e| Error::TestCommandError(format!("Cargo test command failed: {}", e)))?;
.map_err(|e| Error::TestCommand(format!("Cargo test command failed: {}", e)))?;

if result.status.success() {
Ok(())
} else {
Err(Error::TestCommandError("Cargo test command failed.".to_string()))
Err(Error::TestCommand("Cargo test command failed.".to_string()))
}
}

Expand All @@ -31,12 +21,12 @@ pub fn test_e2e_smart_contract(path: &Option<PathBuf>) -> Result<(), Error> {
let result = cmd("cargo", vec!["test", "--features=e2e-tests"])
.dir(path.clone().unwrap_or_else(|| PathBuf::from("./")))
.run()
.map_err(|e| Error::TestCommandError(format!("Cargo test command failed: {}", e)))?;
.map_err(|e| Error::TestCommand(format!("Cargo test command failed: {}", e)))?;

if result.status.success() {
Ok(())
} else {
Err(Error::TestCommandError("Cargo test command failed.".to_string()))
Err(Error::TestCommand("Cargo test command failed.".to_string()))
}
}

Expand Down
20 changes: 5 additions & 15 deletions crates/pop-contracts/src/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,28 @@ use contract_extrinsics::BalanceVariant;
use ink_env::{DefaultEnvironment, Environment};
use std::{path::PathBuf, str::FromStr};
use subxt::{Config, PolkadotConfig as DefaultConfig};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to parse balance: {0}")]
BalanceParsingFailed(String),
#[error("Failed to parse account address: {0}")]
AccountAddressParsingFailed(String),
#[error("Failed to get manifest path: {0}")]
ManifestPathError(String),
}
use crate::errors::Error;

pub fn get_manifest_path(path: &Option<PathBuf>) -> Result<ManifestPath, Error> {
if let Some(path) = path {
let full_path = PathBuf::from(path.to_string_lossy().to_string() + "/Cargo.toml");
return ManifestPath::try_from(Some(full_path))
.map_err(|e| Error::ManifestPathError(format!("Failed to get manifest path: {}", e)));
.map_err(|e| Error::ManifestPath(format!("Failed to get manifest path: {}", e)));
} else {
return ManifestPath::try_from(path.as_ref())
.map_err(|e| Error::ManifestPathError(format!("Failed to get manifest path: {}", e)));
.map_err(|e| Error::ManifestPath(format!("Failed to get manifest path: {}", e)));
}
}

pub fn parse_balance(
balance: &str,
) -> Result<BalanceVariant<<DefaultEnvironment as Environment>::Balance>, Error> {
BalanceVariant::from_str(balance).map_err(|e| Error::BalanceParsingFailed(format!("{}", e)))
BalanceVariant::from_str(balance).map_err(|e| Error::BalanceParsing(format!("{}", e)))
}

pub fn parse_account(account: &str) -> Result<<DefaultConfig as Config>::AccountId, Error> {
<DefaultConfig as Config>::AccountId::from_str(account)
.map_err(|e| Error::AccountAddressParsingFailed(format!("{}", e)))
.map_err(|e| Error::AccountAddressParsing(format!("{}", e)))
}

#[cfg(test)]
Expand Down

0 comments on commit 2c69229

Please sign in to comment.