# error.rs - source
Source of the Rust file `crates/tlsn/src/error.rs`.

Skip to main content

#
tlsn/
error.rs

    1use std::fmt::Display;
    2
    3/// TLSNotary error.
    4///
    5/// Errors are categorized by kind:
    6///
    7/// - **User** ([`is_user`](Self::is_user)): e.g. rejected by the remote party.
    8/// - **IO** ([`is_io`](Self::is_io)): network or communication failure.
    9/// - **Internal** ([`is_internal`](Self::is_internal)): an unknown internal
    10///   error in the library.
    11/// - **Config** ([`is_config`](Self::is_config)): invalid configuration
    12///   provided by the user.
    13///
    14/// The [`msg`](Self::msg) method returns additional context if available, such
    15/// as a rejection message provided by a verifier.
    16#[derive(Debug, thiserror::Error)]
    17pub struct Error {
    18    kind: ErrorKind,
    19    msg: Option<String>,
    20    source: Option<Box<dyn std::error::Error + Send + Sync>>,
    21}
    22
    23impl Error {
    24    pub(crate) fn io() -> Self {
    25        Self {
    26            kind: ErrorKind::Io,
    27            msg: None,
    28            source: None,
    29        }
    30    }
    31
    32    pub(crate) fn internal() -> Self {
    33        Self {
    34            kind: ErrorKind::Internal,
    35            msg: None,
    36            source: None,
    37        }
    38    }
    39
    40    pub(crate) fn user() -> Self {
    41        Self {
    42            kind: ErrorKind::User,
    43            msg: None,
    44            source: None,
    45        }
    46    }
    47
    48    pub(crate) fn config() -> Self {
    49        Self {
    50            kind: ErrorKind::Config,
    51            msg: None,
    52            source: None,
    53        }
    54    }
    55
    56    pub(crate) fn with_msg(mut self, msg: impl Into<String>) -> Self {
    57        self.msg = Some(msg.into());
    58        self
    59    }
    60
    61    pub(crate) fn with_source<T>(mut self, source: T) -> Self
    62    where
    63        T: Into<Box<dyn std::error::Error + Send + Sync>>,
    64    {
    65        self.source = Some(source.into());
    66        self
    67    }
    68
    69    /// Returns `true` if the error was user created.
    70    pub fn is_user(&self) -> bool {
    71        self.kind.is_user()
    72    }
    73
    74    /// Returns `true` if the error originated from an IO error.
    75    pub fn is_io(&self) -> bool {
    76        self.kind.is_io()
    77    }
    78
    79    /// Returns `true` if the error originated from an internal bug.
    80    pub fn is_internal(&self) -> bool {
    81        self.kind.is_internal()
    82    }
    83
    84    /// Returns `true` if the error originated from invalid configuration.
    85    pub fn is_config(&self) -> bool {
    86        self.kind.is_config()
    87    }
    88
    89    /// Returns the error message if available.
    90    pub fn msg(&self) -> Option<&str> {
    91        self.msg.as_deref()
    92    }
    93}
    94
    95impl Display for Error {
    96    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    97        match self.kind {
    98            ErrorKind::User => write!(f, "user error")?,
    99            ErrorKind::Io => write!(f, "io error")?,
    100            ErrorKind::Internal => write!(f, "internal error")?,
    101            ErrorKind::Config => write!(f, "config error")?,
    102        }
    103
    104        if let Some(msg) = &self.msg {
    105            write!(f, ": {msg}")?;
    106        }
    107
    108        if let Some(source) = &self.source {
    109            write!(f, " caused by: {source}")?;
    110        }
    111
    112        Ok(())
    113    }
    114}
    115
    116impl From<std::io::Error> for Error {
    117    fn from(e: std::io::Error) -> Self {
    118        Self::io().with_source(e)
    119    }
    120}
    121
    122impl From<mpz_common::ContextError> for Error {
    123    fn from(e: mpz_common::ContextError) -> Self {
    124        Self::internal().with_msg("context error").with_source(e)
    125    }
    126}
    127
    128impl From<mpc_tls::MpcTlsError> for Error {
    129    fn from(e: mpc_tls::MpcTlsError) -> Self {
    130        Self::internal().with_msg("mpc-tls error").with_source(e)
    131    }
    132}
    133
    134impl From<tlsn_mux::ConnectionError> for Error {
    135    fn from(e: tlsn_mux::ConnectionError) -> Self {
    136        Self::io().with_msg("mux connection error").with_source(e)
    137    }
    138}
    139
    140#[derive(Debug)]
    141enum ErrorKind {
    142    User,
    143    Io,
    144    Internal,
    145    Config,
    146}
    147
    148impl ErrorKind {
    149    fn is_user(&self) -> bool {
    150        matches!(self, ErrorKind::User)
    151    }
    152
    153    fn is_io(&self) -> bool {
    154        matches!(self, ErrorKind::Io)
    155    }
    156
    157    fn is_internal(&self) -> bool {
    158        matches!(self, ErrorKind::Internal)
    159    }
    160
    161    fn is_config(&self) -> bool {
    162        matches!(self, ErrorKind::Config)
    163    }
    164}