Skip to main content

hopr_transport_protocol/
errors.rs

1use hopr_api::types::{internal::errors::CoreTypesError, primitive::errors::GeneralError};
2use thiserror::Error;
3
4/// Errors generated by the crate.
5#[derive(Error, Debug)]
6pub enum ProtocolError {
7    #[error("timeout on protocol operation")]
8    Timeout,
9
10    #[error(transparent)]
11    Other(Box<dyn std::error::Error + Send + Sync + 'static>),
12
13    #[error("General error {0}")]
14    GeneralError(#[from] GeneralError),
15
16    #[error("Core error {0}")]
17    CoreError(#[from] CoreTypesError),
18
19    #[error("Failed on a logical error: {0}")]
20    Logic(String),
21}
22
23/// Result used by the crate, based on the [ProtocolError] error type.
24pub type Result<T> = core::result::Result<T, ProtocolError>;
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn protocol_error_variants_should_display_correctly() {
32        let variants: Vec<String> = vec![
33            ProtocolError::Timeout.to_string(),
34            ProtocolError::Logic("test logic error".into()).to_string(),
35            ProtocolError::GeneralError(GeneralError::ParseError("bad parse".into())).to_string(),
36        ];
37        insta::assert_debug_snapshot!(variants);
38    }
39}