Skip to content

Commit 8d55db3

Browse files
committed
refactor runtime errors
1 parent 1690e00 commit 8d55db3

File tree

10 files changed

+433
-152
lines changed

10 files changed

+433
-152
lines changed
Lines changed: 95 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,117 @@
1-
use crate::certificate_creator::CertificateCreationError;
2-
use crate::snapshot_stores::SnapshotStoreError;
3-
use crate::{ProtocolError, SignerRegistrationError, SnapshotError};
1+
use crate::ProtocolError;
42

53
use mithril_common::certificate_chain::CertificateVerifierError;
64
use mithril_common::chain_observer::ChainObserverError;
7-
use mithril_common::digesters::{ImmutableDigesterError, ImmutableFileListingError};
5+
use mithril_common::digesters::ImmutableDigesterError;
86
use mithril_common::entities::BeaconComparisonError;
9-
use mithril_common::entities::Epoch;
107
use mithril_common::store::StoreError;
118
use mithril_common::BeaconProviderError;
129
use std::error::Error as StdError;
13-
use std::io;
1410
use thiserror::Error;
1511

12+
/// Error encountered or produced by the Runtime.
13+
/// This enum represents the faith of the errors produced during the state
14+
/// transitions.
1615
#[derive(Error, Debug)]
1716
pub enum RuntimeError {
18-
/// MultiSigner error
19-
#[error("multi signer error: {0}")]
20-
MultiSigner(#[from] ProtocolError),
21-
22-
#[error("snapshotter error: {0}")]
23-
Snapshotter(#[from] SnapshotError),
24-
25-
#[error("digester error: {0}")]
26-
Digester(#[from] ImmutableDigesterError),
27-
28-
#[error("store error: {0}")]
29-
StoreError(#[from] StoreError),
30-
31-
#[error("snapshot uploader error: {0}")]
32-
SnapshotUploader(String),
33-
34-
#[error("snapshot build error: {0}")]
35-
SnapshotBuild(#[from] io::Error),
36-
37-
#[error("immutable file scanning error: {0}")]
38-
ImmutableFile(#[from] ImmutableFileListingError),
17+
/// Errors that need the runtime to try again without changing its state.
18+
#[error("An error occured: {message}. This runtime cycle will be skipped. Nested error: {nested_error:#?}.")]
19+
KeepState {
20+
/// error message
21+
message: String,
3922

40-
#[error("chain observer error: {0}")]
41-
ChainObserver(#[from] ChainObserverError),
23+
/// Eventual caught error
24+
nested_error: Option<Box<dyn StdError + Sync + Send>>,
25+
},
26+
/// A Critical error means the Runtime stops and the software exits with an
27+
/// error code.
28+
#[error("Critical error:'{message}'. Nested error: {nested_error:#?}.")]
29+
Critical {
30+
/// error message
31+
message: String,
4232

43-
#[error("beacon provider error: {0}")]
44-
BeaconProvider(#[from] BeaconProviderError),
33+
/// Eventual caught error
34+
nested_error: Option<Box<dyn StdError + Sync + Send>>,
35+
},
36+
}
4537

46-
#[error("certificate verifier error: {0}")]
47-
CertificateVerifier(#[from] CertificateVerifierError),
38+
impl RuntimeError {
39+
/// Create a new KeepState error
40+
pub fn keep_state(message: &str, error: Option<Box<dyn StdError + Sync + Send>>) -> Self {
41+
Self::KeepState {
42+
message: message.to_string(),
43+
nested_error: error,
44+
}
45+
}
46+
47+
/// Create a new Critical error
48+
pub fn critical(message: &str, error: Option<Box<dyn StdError + Sync + Send>>) -> Self {
49+
Self::Critical {
50+
message: message.to_string(),
51+
nested_error: error,
52+
}
53+
}
54+
}
4855

49-
#[error("certificate chain gap error: {0} vs {1}")]
50-
CertificateChainEpochGap(Epoch, Epoch),
56+
impl From<BeaconComparisonError> for RuntimeError {
57+
fn from(value: BeaconComparisonError) -> Self {
58+
Self::KeepState {
59+
message: "Beacon comparison error".to_string(),
60+
nested_error: Some(value.into()),
61+
}
62+
}
63+
}
5164

52-
#[error("snapshot store error: {0}")]
53-
SnapshotStore(#[from] SnapshotStoreError),
65+
impl From<ImmutableDigesterError> for RuntimeError {
66+
fn from(value: ImmutableDigesterError) -> Self {
67+
Self::KeepState {
68+
message: "Error while reading immutable files".to_string(),
69+
nested_error: Some(value.into()),
70+
}
71+
}
72+
}
5473

55-
#[error("certificate creation error: {0}")]
56-
CertificateCreation(#[from] CertificateCreationError),
74+
impl From<ProtocolError> for RuntimeError {
75+
fn from(value: ProtocolError) -> Self {
76+
Self::KeepState {
77+
message: "Mithril Protocol Error".to_string(),
78+
nested_error: Some(value.into()),
79+
}
80+
}
81+
}
5782

58-
#[error("beacon comparison error: {0}")]
59-
BeaconComparisonError(#[from] BeaconComparisonError),
83+
impl From<ChainObserverError> for RuntimeError {
84+
fn from(value: ChainObserverError) -> Self {
85+
Self::KeepState {
86+
message: "Chain observer error".to_string(),
87+
nested_error: Some(value.into()),
88+
}
89+
}
90+
}
6091

61-
#[error("signer registration error: {0}")]
62-
SignerRegistration(#[from] SignerRegistrationError),
92+
impl From<StoreError> for RuntimeError {
93+
fn from(value: StoreError) -> Self {
94+
Self::KeepState {
95+
message: "Store Error".to_string(),
96+
nested_error: Some(value.into()),
97+
}
98+
}
99+
}
63100

64-
#[error("general error: {0}")]
65-
General(Box<dyn StdError + Sync + Send>),
101+
impl From<BeaconProviderError> for RuntimeError {
102+
fn from(value: BeaconProviderError) -> Self {
103+
Self::KeepState {
104+
message: "Could not read beacon from chain".to_string(),
105+
nested_error: Some(value.into()),
106+
}
107+
}
108+
}
66109

67-
/// A Critical error means the Runtime stops and the software exits with an
68-
/// error code.
69-
#[error("Critical error '{message}'. Nested error: {nested_error:#?}")]
70-
Critical {
71-
message: String,
72-
nested_error: Option<Box<dyn StdError + Sync + Send>>,
73-
},
110+
impl From<CertificateVerifierError> for RuntimeError {
111+
fn from(value: CertificateVerifierError) -> Self {
112+
Self::KeepState {
113+
message: "Could not verify given certificate".to_string(),
114+
nested_error: Some(value.into()),
115+
}
116+
}
74117
}

0 commit comments

Comments
 (0)