Skip to content

Commit

Permalink
Use source attribute on wrapped errors
Browse files Browse the repository at this point in the history
+ remove double error message from them. With the source
attribute the inner error is print without the need to (
see: dtolnay/thiserror#38).
  • Loading branch information
Alenar committed Sep 27, 2023
1 parent 6dfb82b commit 85cbf96
Show file tree
Hide file tree
Showing 23 changed files with 126 additions and 119 deletions.
50 changes: 25 additions & 25 deletions mithril-aggregator/src/multi_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,47 @@ use mockall::automock;
pub enum ProtocolError {
/// Signer is already registered.
#[error("signer already registered")]
ExistingSigner(),
ExistingSigner,

/// Signer was not registered.
#[error("signer did not register")]
UnregisteredParty(),
UnregisteredParty,

/// Signer registration failed.
#[error("signer registration failed")]
FailedSignerRegistration(StdError),
FailedSignerRegistration(#[source] StdError),

/// Single signature already recorded.
#[error("single signature already recorded")]
ExistingSingleSignature(entities::PartyId),

/// Mithril STM library returned an error.
#[error("core error: '{0}'")]
Core(StdError),
#[error("core error")]
Core(#[source] StdError),

/// No message available.
#[error("no message available")]
UnavailableMessage(),
UnavailableMessage,

/// No protocol parameters available.
#[error("no protocol parameters available")]
UnavailableProtocolParameters(),
UnavailableProtocolParameters,

/// No clerk available.
#[error("no clerk available")]
UnavailableClerk(),
UnavailableClerk,

/// No beacon available.
#[error("no beacon available")]
UnavailableBeacon(),
UnavailableBeacon,

/// Store error.
#[error("store error: {0}")]
StoreError(StdError),
#[error("store error")]
StoreError(#[source] StdError),

/// Beacon error.
#[error("beacon error: '{0}'")]
Beacon(StdError),
#[error("beacon error")]
Beacon(#[source] StdError),
}

/// MultiSigner is the cryptographic engine in charge of producing multi signatures from individual signatures
Expand Down Expand Up @@ -124,7 +124,7 @@ pub trait MultiSigner: Sync + Send {
let protocol_parameters = self
.get_protocol_parameters()
.await?
.ok_or_else(ProtocolError::UnavailableProtocolParameters)?;
.ok_or(ProtocolError::UnavailableProtocolParameters)?;
Ok(self
.compute_aggregate_verification_key(&signers_with_stake, &protocol_parameters)
.await?)
Expand All @@ -138,7 +138,7 @@ pub trait MultiSigner: Sync + Send {
let protocol_parameters = self
.get_next_protocol_parameters()
.await?
.ok_or_else(ProtocolError::UnavailableProtocolParameters)?;
.ok_or(ProtocolError::UnavailableProtocolParameters)?;
Ok(self
.compute_aggregate_verification_key(&next_signers_with_stake, &protocol_parameters)
.await?)
Expand Down Expand Up @@ -285,7 +285,7 @@ impl MultiSigner for MultiSignerImpl {
let epoch = self
.current_beacon
.as_ref()
.ok_or_else(ProtocolError::UnavailableBeacon)?
.ok_or(ProtocolError::UnavailableBeacon)?
.epoch
.offset_to_signer_retrieval_epoch()
.with_context(|| "Multi Signer can not offset to signer retrieveal epoch while retrivieving protocol parameters")
Expand All @@ -302,7 +302,7 @@ impl MultiSigner for MultiSignerImpl {
let epoch = self
.current_beacon
.as_ref()
.ok_or_else(ProtocolError::UnavailableBeacon)?
.ok_or(ProtocolError::UnavailableBeacon)?
.epoch
.offset_to_protocol_parameters_recording_epoch();

Expand All @@ -322,7 +322,7 @@ impl MultiSigner for MultiSignerImpl {
let epoch = self
.current_beacon
.as_ref()
.ok_or_else(ProtocolError::UnavailableBeacon)?
.ok_or(ProtocolError::UnavailableBeacon)?
.epoch
.offset_to_next_signer_retrieval_epoch();
self.get_protocol_parameters_at_epoch(epoch).await
Expand All @@ -334,7 +334,7 @@ impl MultiSigner for MultiSignerImpl {
let epoch = self
.current_beacon
.as_ref()
.ok_or_else(ProtocolError::UnavailableBeacon)?
.ok_or(ProtocolError::UnavailableBeacon)?
.epoch
.offset_to_signer_retrieval_epoch()
.with_context(|| "Multi Signer can not offset to signer retrieveal epoch while retrieving stake distribution")
Expand All @@ -350,7 +350,7 @@ impl MultiSigner for MultiSignerImpl {
let epoch = self
.current_beacon
.as_ref()
.ok_or_else(ProtocolError::UnavailableBeacon)?
.ok_or(ProtocolError::UnavailableBeacon)?
.epoch
.offset_to_next_signer_retrieval_epoch();
self.get_stake_distribution_at_epoch(epoch).await
Expand All @@ -365,7 +365,7 @@ impl MultiSigner for MultiSignerImpl {
let epoch = self
.current_beacon
.as_ref()
.ok_or_else(ProtocolError::UnavailableBeacon)?
.ok_or(ProtocolError::UnavailableBeacon)?
.epoch
.offset_to_recording_epoch();
let stakes = StakeDistribution::from_iter(stakes.iter().cloned());
Expand Down Expand Up @@ -397,7 +397,7 @@ impl MultiSigner for MultiSignerImpl {
let epoch = self
.current_beacon
.as_ref()
.ok_or_else(ProtocolError::UnavailableBeacon)?
.ok_or(ProtocolError::UnavailableBeacon)?
.epoch;
let epoch = epoch
.offset_to_signer_retrieval_epoch()
Expand Down Expand Up @@ -439,7 +439,7 @@ impl MultiSigner for MultiSignerImpl {
let epoch = self
.current_beacon
.as_ref()
.ok_or_else(ProtocolError::UnavailableBeacon)?
.ok_or(ProtocolError::UnavailableBeacon)?
.epoch
.offset_to_next_signer_retrieval_epoch();
let signers = self
Expand Down Expand Up @@ -486,7 +486,7 @@ impl MultiSigner for MultiSignerImpl {
let protocol_parameters = self
.get_protocol_parameters()
.await?
.ok_or_else(ProtocolError::UnavailableProtocolParameters)?;
.ok_or(ProtocolError::UnavailableProtocolParameters)?;

let signers_with_stakes = self.get_signers_with_stake().await?;

Expand All @@ -508,7 +508,7 @@ impl MultiSigner for MultiSignerImpl {
let protocol_parameters = self
.get_protocol_parameters()
.await?
.ok_or_else(ProtocolError::UnavailableProtocolParameters)?;
.ok_or(ProtocolError::UnavailableProtocolParameters)?;

let signers_with_stakes = self.get_signers_with_stake().await?;

Expand Down
9 changes: 6 additions & 3 deletions mithril-aggregator/src/runtime/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,34 @@ use thiserror::Error;
#[derive(Error, Debug)]
pub enum RuntimeError {
/// Errors that need the runtime to try again without changing its state.
#[error("An error occured: {message}. This runtime cycle will be skipped. Nested error: {nested_error:#?}.")]
#[error("An error occured: {message}. This runtime cycle will be skipped.")]
KeepState {
/// error message
message: String,

/// Eventual caught error
#[source]
nested_error: Option<StdError>,
},
/// A Critical error means the Runtime stops and the software exits with an
/// error code.
#[error("Critical error:'{message}'. Nested error: {nested_error:#?}.")]
#[error("Critical error:'{message}'.")]
Critical {
/// error message
message: String,

/// Eventual caught error
#[source]
nested_error: Option<StdError>,
},
/// An error that needs to re-initialize the state machine.
#[error("An error occured: {message}. The state machine will be re-initialized. Nested error: {nested_error:#?}")]
#[error("An error occured: {message}. The state machine will be re-initialized.")]
ReInit {
/// error message
message: String,

/// Eventual caught error
#[source]
nested_error: Option<StdError>,
},
}
Expand Down
7 changes: 4 additions & 3 deletions mithril-aggregator/src/signer_registerer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum SignerRegistrationError {
received_epoch: Epoch,
},

// todo: wrap ChainObserverError instead
/// Chain observer error.
#[error("chain observer error: '{0}'")]
ChainObserver(String),
Expand All @@ -41,12 +42,12 @@ pub enum SignerRegistrationError {
ExistingSigner(Box<SignerWithStake>),

/// Store error.
#[error("store error: {0}")]
StoreError(StdError),
#[error("store error")]
StoreError(#[source] StdError),

/// Signer registration failed.
#[error("signer registration failed")]
FailedSignerRegistration(StdError),
FailedSignerRegistration(#[source] StdError),

/// Signer recorder failed.
#[error("signer recorder failed: '{0}'")]
Expand Down
16 changes: 8 additions & 8 deletions mithril-client/src/aggregator_client/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ use crate::utils::{DownloadProgressReporter, SnapshotUnpacker};
#[derive(Error, Debug)]
pub enum AggregatorHTTPClientError {
/// Error raised when querying the aggregator returned a 5XX error.
#[error("remote server technical error: '{0}'")]
RemoteServerTechnical(StdError),
#[error("remote server technical error")]
RemoteServerTechnical(#[source] StdError),

/// Error raised when querying the aggregator returned a 4XX error.
#[error("remote server logical error: '{0}'")]
RemoteServerLogical(StdError),
#[error("remote server logical error")]
RemoteServerLogical(#[source] StdError),

/// Error raised when the server API version mismatch the client API version.
#[error("API version mismatch: {0}")]
ApiVersionMismatch(StdError),
#[error("API version mismatch")]
ApiVersionMismatch(#[source] StdError),

/// HTTP subsystem error
#[error("HTTP subsystem error: {0}")]
SubsystemError(StdError),
#[error("HTTP subsystem error")]
SubsystemError(#[source] StdError),
}

/// API that defines a client for the Aggregator
Expand Down
5 changes: 3 additions & 2 deletions mithril-client/src/services/mithril_stake_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ pub enum MithrilStakeDistributionServiceError {
CertificateNotFound(String),

/// The configuration has invalid or missing parameters
#[error("Missing or invalid parameters: {0:?}")]
InvalidParameters(StdError),
#[error("Missing or invalid parameters")]
InvalidParameters(#[source] StdError),

/// Could not find the given stake distribution
#[error("Could not find stake distribution associated to hash '{0}'.")]
CouldNotFindStakeDistribution(String),
}

/// Definition of the service responsible of Mithril Stake Distribution.
#[async_trait]
pub trait MithrilStakeDistributionService {
Expand Down
7 changes: 4 additions & 3 deletions mithril-client/src/utils/unpacker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ pub enum SnapshotUnpackerError {
UnpackDirectoryAlreadyExists(PathBuf),

/// Cannot write in the given directory.
#[error("Unpack directory '{0}' is not writable. (underlying error: « {1} »).")]
UnpackDirectoryIsNotWritable(PathBuf, StdError),
#[error("Unpack directory '{0}' is not writable.")]
UnpackDirectoryIsNotWritable(PathBuf, #[source] StdError),

/// Unpacking error
#[error("Could not unpack from streamed data snapshot to directory '{dirpath}'. Error: « {error:?} ».")]
#[error("Could not unpack from streamed data snapshot to directory '{dirpath}'")]
UnpackFailed {
/// Location where the archive is to be extracted.
dirpath: PathBuf,

/// Subsystem error
#[source]
error: StdError,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use mockall::automock;
/// [CertificateRetriever] related errors.
#[derive(Debug, Error)]
#[error("Error when retrieving certificate")]
pub struct CertificateRetrieverError(pub StdError);
pub struct CertificateRetrieverError(#[source] pub StdError);

/// CertificateRetriever is in charge of retrieving a [Certificate] given its hash
#[cfg_attr(test, automock)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum CertificateVerifierError {
VerifyMultiSignature(String),

/// Error raised when the Genesis Signature stored in a [Certificate] is invalid.
#[error("certificate genesis error: '{0}'")]
#[error("certificate genesis error")]
CertificateGenesis(#[from] ProtocolGenesisError),

/// Error raised when the hash stored in a [Certificate] doesn't match a recomputed hash.
Expand Down
8 changes: 4 additions & 4 deletions mithril-common/src/chain_observer/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use super::{ChainAddress, TxDatum};
#[derive(Debug, Error)]
pub enum ChainObserverError {
/// Generic [ChainObserver] error.
#[error("general error {0:?}")]
General(StdError),
#[error("general error")]
General(#[source] StdError),

/// Error raised when the content could not be parsed.
#[error("could not parse content: {0:?}")]
InvalidContent(StdError),
#[error("could not parse content")]
InvalidContent(#[source] StdError),
}

/// Retrieve data from the cardano network
Expand Down
8 changes: 4 additions & 4 deletions mithril-common/src/chain_observer/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub type ChainAddress = String;
#[derive(Debug, Error)]
pub enum TxDatumError {
/// Error raised when the content could not be parsed.
#[error("could not parse tx datum: {0:?}")]
InvalidContent(StdError),
#[error("could not parse tx datum")]
InvalidContent(#[source] StdError),

/// Error raised when building the tx datum failed.
#[error("could not build tx datum: {0}")]
Build(serde_json::Error),
#[error("could not build tx datum")]
Build(#[source] serde_json::Error),
}

/// [TxDatum] represents transaction Datum.
Expand Down
4 changes: 2 additions & 2 deletions mithril-common/src/crypto_helper/cardano/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub struct Sum6KesBytes(#[serde(with = "As::<Bytes>")] pub [u8; 612]);

/// Parse error
#[derive(Error, Debug)]
#[error("Codec parse error: `{0:?}`")]
pub struct CodecParseError(StdError);
#[error("Codec parse error")]
pub struct CodecParseError(#[source] StdError);

/// Fields for a shelley formatted file (holds for vkeys, skeys or certs)
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
Expand Down
4 changes: 2 additions & 2 deletions mithril-common/src/crypto_helper/era.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub type EraMarkersVerifierSignature = ProtocolKey<ed25519_dalek::Signature>;
/// [EraMarkersSigner] and [EraMarkersVerifier] related errors.
pub enum EraMarkersVerifierError {
/// Error raised when a Signature verification fail
#[error("era markers signature verification error: '{0}'")]
SignatureVerification(StdError),
#[error("era markers signature verification error")]
SignatureVerification(#[source] StdError),
}

/// A cryptographic signer that is responsible for signing the EraMarkers
Expand Down
4 changes: 2 additions & 2 deletions mithril-common/src/crypto_helper/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use super::{ProtocolGenesisSecretKey, ProtocolGenesisSignature, ProtocolGenesisV

#[derive(Error, Debug)]
/// [ProtocolGenesisSigner] and [ProtocolGenesisVerifier] related errors.
#[error("genesis signature verification error: '{0}'")]
pub struct ProtocolGenesisError(StdError);
#[error("genesis signature verification error")]
pub struct ProtocolGenesisError(#[source] StdError);

/// A protocol Genesis Signer that is responsible for signing the
/// [Genesis Certificate](https://mithril.network/doc/mithril/mithril-protocol/certificates#the-certificate-chain-design)
Expand Down

0 comments on commit 85cbf96

Please sign in to comment.