Skip to content

Commit 894bf6b

Browse files
authored
Merge pull request #1162 from input-output-hk/djo/798/stdresult-as-anyhow-alias
StdResult and StdError as anyhow alias
2 parents c07f297 + bbf4ba9 commit 894bf6b

File tree

78 files changed

+697
-834
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+697
-834
lines changed

Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ license = { workspace = true }
1010
repository = { workspace = true }
1111

1212
[dependencies]
13+
anyhow = "1.0.71"
1314
async-trait = "0.1.52"
1415
chrono = { version = "0.4", features = ["serde"] }
1516
clap = { version = "4.0", features = ["derive", "env", "cargo"] }

mithril-aggregator/src/artifact_builder/cardano_immutable_files_full.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ impl ArtifactBuilder<Beacon, Snapshot> for CardanoImmutableFilesFullArtifactBuil
142142

143143
#[cfg(test)]
144144
mod tests {
145+
use anyhow::anyhow;
145146
use std::path::Path;
147+
use tempfile::NamedTempFile;
146148

147149
use mithril_common::test_utils::fake_data;
148-
use tempfile::NamedTempFile;
149150

150151
use super::*;
151152

@@ -253,7 +254,7 @@ mod tests {
253254
let mut snapshot_uploader = MockSnapshotUploader::new();
254255
snapshot_uploader
255256
.expect_upload_snapshot()
256-
.return_once(|_| Err("an error".to_string()))
257+
.return_once(|_| Err(anyhow!("an error")))
257258
.once();
258259

259260
let cardano_immutable_files_full_artifact_builder =

mithril-aggregator/src/commands/era_command.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use anyhow::anyhow;
12
use clap::{Parser, Subcommand};
23
use config::{builder::DefaultState, ConfigBuilder};
34
use mithril_common::{
45
crypto_helper::{key_decode_hex, EraMarkersSigner},
56
entities::{Epoch, HexEncodedEraMarkersSecretKey},
7+
StdResult,
68
};
79
use slog_scope::debug;
8-
use std::error::Error;
910

1011
use crate::tools::EraTools;
1112

@@ -18,10 +19,7 @@ pub struct EraCommand {
1819
}
1920

2021
impl EraCommand {
21-
pub async fn execute(
22-
&self,
23-
config_builder: ConfigBuilder<DefaultState>,
24-
) -> Result<(), Box<dyn Error>> {
22+
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
2523
self.era_subcommand.execute(config_builder).await
2624
}
2725
}
@@ -37,10 +35,7 @@ pub enum EraSubCommand {
3735
}
3836

3937
impl EraSubCommand {
40-
pub async fn execute(
41-
&self,
42-
config_builder: ConfigBuilder<DefaultState>,
43-
) -> Result<(), Box<dyn Error>> {
38+
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
4439
match self {
4540
Self::List(cmd) => cmd.execute(config_builder).await,
4641
Self::GenerateTxDatum(cmd) => cmd.execute(config_builder).await,
@@ -57,10 +52,7 @@ pub struct ListEraSubCommand {
5752
}
5853

5954
impl ListEraSubCommand {
60-
pub async fn execute(
61-
&self,
62-
_config_builder: ConfigBuilder<DefaultState>,
63-
) -> Result<(), Box<dyn Error>> {
55+
pub async fn execute(&self, _config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
6456
debug!("LIST ERA command");
6557
let era_tools = EraTools::new();
6658
let eras = era_tools.get_supported_eras_list()?;
@@ -93,14 +85,12 @@ pub struct GenerateTxDatumEraSubCommand {
9385
}
9486

9587
impl GenerateTxDatumEraSubCommand {
96-
pub async fn execute(
97-
&self,
98-
_config_builder: ConfigBuilder<DefaultState>,
99-
) -> Result<(), Box<dyn Error>> {
88+
pub async fn execute(&self, _config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
10089
debug!("GENERATETXDATUM ERA command");
10190
let era_tools = EraTools::new();
10291

103-
let era_markers_secret_key = key_decode_hex(&self.era_markers_secret_key)?;
92+
let era_markers_secret_key = key_decode_hex(&self.era_markers_secret_key)
93+
.map_err(|e| anyhow!(e).context("json hex decode of era markers secret key failure"))?;
10494
let era_markers_signer = EraMarkersSigner::from_secret_key(era_markers_secret_key);
10595
print!(
10696
"{}",

mithril-aggregator/src/commands/genesis_command.rs

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use anyhow::{anyhow, Context};
12
use clap::{Parser, Subcommand};
23
use config::{builder::DefaultState, ConfigBuilder};
34
use mithril_common::{
45
crypto_helper::{key_decode_hex, ProtocolGenesisSigner},
56
entities::HexEncodedGenesisSecretKey,
7+
StdResult,
68
};
79
use slog_scope::debug;
8-
use std::{error::Error, path::PathBuf};
10+
use std::path::PathBuf;
911

1012
use crate::{dependency_injection::DependenciesBuilder, tools::GenesisTools, Configuration};
1113

@@ -18,10 +20,7 @@ pub struct GenesisCommand {
1820
}
1921

2022
impl GenesisCommand {
21-
pub async fn execute(
22-
&self,
23-
config_builder: ConfigBuilder<DefaultState>,
24-
) -> Result<(), Box<dyn Error>> {
23+
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
2524
self.genesis_subcommand.execute(config_builder).await
2625
}
2726
}
@@ -43,10 +42,7 @@ pub enum GenesisSubCommand {
4342
}
4443

4544
impl GenesisSubCommand {
46-
pub async fn execute(
47-
&self,
48-
config_builder: ConfigBuilder<DefaultState>,
49-
) -> Result<(), Box<dyn Error>> {
45+
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
5046
match self {
5147
Self::Bootstrap(cmd) => cmd.execute(config_builder).await,
5248
Self::Export(cmd) => cmd.execute(config_builder).await,
@@ -65,15 +61,12 @@ pub struct ExportGenesisSubCommand {
6561
}
6662

6763
impl ExportGenesisSubCommand {
68-
pub async fn execute(
69-
&self,
70-
config_builder: ConfigBuilder<DefaultState>,
71-
) -> Result<(), Box<dyn Error>> {
64+
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
7265
let config: Configuration = config_builder
7366
.build()
74-
.map_err(|e| format!("configuration build error: {e}"))?
67+
.with_context(|| "configuration build error")?
7568
.try_deserialize()
76-
.map_err(|e| format!("configuration deserialize error: {e}"))?;
69+
.with_context(|| "configuration deserialize error")?;
7770
debug!("EXPORT GENESIS command"; "config" => format!("{config:?}"));
7871
println!(
7972
"Genesis export payload to sign to {}",
@@ -84,10 +77,10 @@ impl ExportGenesisSubCommand {
8477

8578
let genesis_tools = GenesisTools::from_dependencies(dependencies)
8679
.await
87-
.map_err(|err| format!("genesis-tools: initialization error: {err}"))?;
80+
.with_context(|| "genesis-tools: initialization error")?;
8881
genesis_tools
8982
.export_payload_to_sign(&self.target_path)
90-
.map_err(|err| format!("genesis-tools: export error: {err}"))?;
83+
.with_context(|| "genesis-tools: export error")?;
9184
Ok(())
9285
}
9386
}
@@ -100,15 +93,12 @@ pub struct ImportGenesisSubCommand {
10093
}
10194

10295
impl ImportGenesisSubCommand {
103-
pub async fn execute(
104-
&self,
105-
config_builder: ConfigBuilder<DefaultState>,
106-
) -> Result<(), Box<dyn Error>> {
96+
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
10797
let config: Configuration = config_builder
10898
.build()
109-
.map_err(|e| format!("configuration build error: {e}"))?
99+
.with_context(|| "configuration build error")?
110100
.try_deserialize()
111-
.map_err(|e| format!("configuration deserialize error: {e}"))?;
101+
.with_context(|| "configuration deserialize error")?;
112102
debug!("IMPORT GENESIS command"; "config" => format!("{config:?}"));
113103
println!(
114104
"Genesis import signed payload from {}",
@@ -119,11 +109,11 @@ impl ImportGenesisSubCommand {
119109

120110
let genesis_tools = GenesisTools::from_dependencies(dependencies)
121111
.await
122-
.map_err(|err| format!("genesis-tools: initialization error: {err}"))?;
112+
.with_context(|| "genesis-tools: initialization error")?;
123113
genesis_tools
124114
.import_payload_signature(&self.signed_payload_path)
125115
.await
126-
.map_err(|err| format!("genesis-tools: import error: {err}"))?;
116+
.with_context(|| "genesis-tools: import error")?;
127117
Ok(())
128118
}
129119
}
@@ -144,10 +134,7 @@ pub struct SignGenesisSubCommand {
144134
}
145135

146136
impl SignGenesisSubCommand {
147-
pub async fn execute(
148-
&self,
149-
_config_builder: ConfigBuilder<DefaultState>,
150-
) -> Result<(), Box<dyn Error>> {
137+
pub async fn execute(&self, _config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
151138
debug!("SIGN GENESIS command");
152139
println!(
153140
"Genesis sign payload from {} to {}",
@@ -161,7 +148,7 @@ impl SignGenesisSubCommand {
161148
&self.genesis_secret_key_path,
162149
)
163150
.await
164-
.map_err(|err| format!("genesis-tools: sign error: {err}"))?;
151+
.with_context(|| "genesis-tools: sign error")?;
165152

166153
Ok(())
167154
}
@@ -174,29 +161,27 @@ pub struct BootstrapGenesisSubCommand {
174161
}
175162

176163
impl BootstrapGenesisSubCommand {
177-
pub async fn execute(
178-
&self,
179-
config_builder: ConfigBuilder<DefaultState>,
180-
) -> Result<(), Box<dyn Error>> {
164+
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
181165
let config: Configuration = config_builder
182166
.build()
183-
.map_err(|e| format!("configuration build error: {e}"))?
167+
.with_context(|| "configuration build error")?
184168
.try_deserialize()
185-
.map_err(|e| format!("configuration deserialize error: {e}"))?;
169+
.with_context(|| "configuration deserialize error")?;
186170
debug!("BOOTSTRAP GENESIS command"; "config" => format!("{config:?}"));
187171
println!("Genesis bootstrap for test only!");
188172
let mut dependencies_builder = DependenciesBuilder::new(config.clone());
189173
let dependencies = dependencies_builder.create_genesis_container().await?;
190174

191175
let genesis_tools = GenesisTools::from_dependencies(dependencies)
192176
.await
193-
.map_err(|err| format!("genesis-tools: initialization error: {err}"))?;
194-
let genesis_secret_key = key_decode_hex(&self.genesis_secret_key)?;
177+
.with_context(|| "genesis-tools: initialization error")?;
178+
let genesis_secret_key = key_decode_hex(&self.genesis_secret_key)
179+
.map_err(|e| anyhow!(e).context("json hex decode of genesis secret key failure"))?;
195180
let genesis_signer = ProtocolGenesisSigner::from_secret_key(genesis_secret_key);
196181
genesis_tools
197182
.bootstrap_test_genesis_certificate(genesis_signer)
198183
.await
199-
.map_err(|err| format!("genesis-tools: bootstrap error: {err}"))?;
184+
.with_context(|| "genesis-tools: bootstrap error")?;
200185
Ok(())
201186
}
202187
}

mithril-aggregator/src/commands/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ mod tools_command;
55

66
use clap::{Parser, Subcommand};
77
use config::{builder::DefaultState, ConfigBuilder, Map, Source, Value, ValueKind};
8+
use mithril_common::StdResult;
89
use slog::Level;
910
use slog_scope::debug;
10-
use std::{error::Error, path::PathBuf};
11+
use std::path::PathBuf;
1112

1213
use crate::DefaultConfiguration;
1314

@@ -21,10 +22,7 @@ pub enum MainCommand {
2122
}
2223

2324
impl MainCommand {
24-
pub async fn execute(
25-
&self,
26-
config_builder: ConfigBuilder<DefaultState>,
27-
) -> Result<(), Box<dyn Error>> {
25+
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
2826
match self {
2927
Self::Genesis(cmd) => cmd.execute(config_builder).await,
3028
Self::Era(cmd) => cmd.execute(config_builder).await,
@@ -84,7 +82,7 @@ impl Source for MainOpts {
8482

8583
impl MainOpts {
8684
/// execute command
87-
pub async fn execute(&self) -> Result<(), Box<dyn Error>> {
85+
pub async fn execute(&self) -> StdResult<()> {
8886
let config_file_path = self
8987
.config_directory
9088
.join(format!("{}.json", self.run_mode));

mithril-aggregator/src/commands/serve_command.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
use anyhow::Context;
12
use clap::Parser;
23
use config::{builder::DefaultState, ConfigBuilder, Map, Source, Value, ValueKind};
4+
use mithril_common::StdResult;
35
use slog_scope::{crit, debug, info};
4-
use std::{error::Error, net::IpAddr, path::PathBuf};
6+
use std::{net::IpAddr, path::PathBuf};
57
use tokio::{sync::oneshot, task::JoinSet};
68

79
use crate::{dependency_injection::DependenciesBuilder, Configuration};
@@ -71,16 +73,13 @@ impl Source for ServeCommand {
7173
}
7274

7375
impl ServeCommand {
74-
pub async fn execute(
75-
&self,
76-
mut config_builder: ConfigBuilder<DefaultState>,
77-
) -> Result<(), Box<dyn Error>> {
76+
pub async fn execute(&self, mut config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
7877
config_builder = config_builder.add_source(self.clone());
7978
let config: Configuration = config_builder
8079
.build()
81-
.map_err(|e| format!("configuration build error: {e}"))?
80+
.with_context(|| "configuration build error")?
8281
.try_deserialize()
83-
.map_err(|e| format!("configuration deserialize error: {e}"))?;
82+
.with_context(|| "configuration deserialize error")?;
8483
debug!("SERVE command"; "config" => format!("{config:?}"));
8584
let mut dependencies_builder = DependenciesBuilder::new(config.clone());
8685

0 commit comments

Comments
 (0)