Skip to content

Commit a70a0d9

Browse files
jpraynaudAlenar
andcommitted
refactor(dmq): better anyhow usage
Co-authored-by: DJO <[email protected]>
1 parent 61a586a commit a70a0d9

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

internal/mithril-dmq-node/src/consumer/pallas.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ impl<M: TryFromBytes + Debug> DmqConsumerPallas<M> {
4747
);
4848
DmqClient::connect(&self.socket, self.network.code())
4949
.await
50-
.map_err(|err| anyhow!(err))
5150
.with_context(|| "DmqConsumerPallas failed to create a new client")
5251
}
5352

@@ -101,13 +100,13 @@ impl<M: TryFromBytes + Debug> DmqConsumerPallas<M> {
101100
.msg_notification()
102101
.send_request_messages_blocking()
103102
.await
104-
.map_err(|err| anyhow!("Failed to request notifications from DMQ server: {}", err))?;
103+
.with_context(|| "Failed to request notifications from DMQ server: {}")?;
105104

106105
let reply = client
107106
.msg_notification()
108107
.recv_next_reply()
109108
.await
110-
.map_err(|err| anyhow!("Failed to receive notifications from DMQ server: {}", err))?;
109+
.with_context(|| "Failed to receive notifications from DMQ server")?;
111110
debug!(self.logger, "Received single signatures from DMQ"; "messages" => ?reply);
112111
if let Err(e) = client.msg_notification().send_done().await {
113112
error!(self.logger, "Failed to send Done"; "error" => ?e);
@@ -118,10 +117,10 @@ impl<M: TryFromBytes + Debug> DmqConsumerPallas<M> {
118117
.into_iter()
119118
.map(|dmq_message| {
120119
let opcert = OpCert::try_from_bytes(&dmq_message.operational_certificate)
121-
.map_err(|e| anyhow!("Failed to parse operational certificate: {}", e))?;
120+
.with_context(|| "Failed to parse operational certificate")?;
122121
let party_id = opcert.compute_protocol_party_id()?;
123122
let payload = M::try_from_bytes(&dmq_message.msg_body)
124-
.map_err(|e| anyhow!("Failed to parse DMQ message body: {}", e))?;
123+
.with_context(|| "Failed to parse DMQ message body")?;
125124

126125
Ok((payload, party_id))
127126
})

internal/mithril-dmq-node/src/message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl DmqMessageBuilder {
7070
.block_number;
7171
let block_number = (*block_number)
7272
.try_into()
73-
.map_err(|_| anyhow!("Failed to convert block number to u32"))?;
73+
.with_context(|| "Failed to convert block number to u32")?;
7474
let (kes_signature, operational_certificate) = self
7575
.kes_signer
7676
.sign(message_bytes, block_number)

internal/mithril-dmq-node/src/publisher/pallas.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{fmt::Debug, marker::PhantomData, path::PathBuf};
22

3-
use anyhow::{anyhow, Context};
3+
use anyhow::Context;
44
use pallas_network::{facades::DmqClient, miniprotocols::localtxsubmission::Response};
55
use slog::{debug, error, Logger};
66

@@ -43,7 +43,6 @@ impl<M: TryToBytes + Debug> DmqPublisherPallas<M> {
4343
let magic = self.network.code();
4444
DmqClient::connect(&self.socket, magic)
4545
.await
46-
.map_err(|err| anyhow!(err))
4746
.with_context(|| "DmqPublisherPallas failed to create a new client")
4847
}
4948
}
@@ -67,14 +66,14 @@ impl<M: TryToBytes + Debug + Sync + Send> DmqPublisher<M> for DmqPublisherPallas
6766
.msg_submission()
6867
.send_submit_tx(dmq_message)
6968
.await
70-
.map_err(|err| anyhow!("Failed to submit DMQ message: {}", err))?;
69+
.with_context(|| "Failed to submit DMQ message")?;
7170
let response = client.msg_submission().recv_submit_tx_response().await?;
7271
if let Err(e) = client.msg_submission().terminate_gracefully().await {
7372
error!(self.logger, "Failed to send Done"; "error" => ?e);
7473
}
7574

7675
if response != Response::Accepted {
77-
return Err(anyhow!("Failed to publish DMQ message: {:?}", response));
76+
anyhow::bail!("Failed to publish DMQ message: {:?}", response);
7877
}
7978

8079
Ok(())

0 commit comments

Comments
 (0)