tlsn_verifier/
error.rs

1use mpc_tls::MpcTlsError;
2use std::{error::Error, fmt};
3use tlsn_common::{encoding::EncodingError, zk_aes::ZkAesCtrError};
4
5/// Error for [`Verifier`](crate::Verifier).
6#[derive(Debug, thiserror::Error)]
7pub struct VerifierError {
8    kind: ErrorKind,
9    source: Option<Box<dyn Error + Send + Sync + 'static>>,
10}
11
12impl VerifierError {
13    fn new<E>(kind: ErrorKind, source: E) -> Self
14    where
15        E: Into<Box<dyn Error + Send + Sync + 'static>>,
16    {
17        Self {
18            kind,
19            source: Some(source.into()),
20        }
21    }
22
23    pub(crate) fn mpc<E>(source: E) -> Self
24    where
25        E: Into<Box<dyn Error + Send + Sync + 'static>>,
26    {
27        Self::new(ErrorKind::Mpc, source)
28    }
29
30    pub(crate) fn zk<E>(source: E) -> Self
31    where
32        E: Into<Box<dyn Error + Send + Sync + 'static>>,
33    {
34        Self::new(ErrorKind::Zk, source)
35    }
36
37    pub(crate) fn attestation<E>(source: E) -> Self
38    where
39        E: Into<Box<dyn Error + Send + Sync + 'static>>,
40    {
41        Self::new(ErrorKind::Attestation, source)
42    }
43
44    pub(crate) fn verify<E>(source: E) -> Self
45    where
46        E: Into<Box<dyn Error + Send + Sync + 'static>>,
47    {
48        Self::new(ErrorKind::Verify, source)
49    }
50}
51
52#[derive(Debug)]
53enum ErrorKind {
54    Io,
55    Config,
56    Mpc,
57    Zk,
58    Commit,
59    Attestation,
60    Verify,
61}
62
63impl fmt::Display for VerifierError {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        f.write_str("verifier error: ")?;
66
67        match self.kind {
68            ErrorKind::Io => f.write_str("io error")?,
69            ErrorKind::Config => f.write_str("config error")?,
70            ErrorKind::Mpc => f.write_str("mpc error")?,
71            ErrorKind::Zk => f.write_str("zk error")?,
72            ErrorKind::Commit => f.write_str("commit error")?,
73            ErrorKind::Attestation => f.write_str("attestation error")?,
74            ErrorKind::Verify => f.write_str("verification error")?,
75        }
76
77        if let Some(source) = &self.source {
78            write!(f, " caused by: {}", source)?;
79        }
80
81        Ok(())
82    }
83}
84
85impl From<std::io::Error> for VerifierError {
86    fn from(e: std::io::Error) -> Self {
87        Self::new(ErrorKind::Io, e)
88    }
89}
90
91impl From<tlsn_common::config::ProtocolConfigError> for VerifierError {
92    fn from(e: tlsn_common::config::ProtocolConfigError) -> Self {
93        Self::new(ErrorKind::Config, e)
94    }
95}
96
97impl From<uid_mux::yamux::ConnectionError> for VerifierError {
98    fn from(e: uid_mux::yamux::ConnectionError) -> Self {
99        Self::new(ErrorKind::Io, e)
100    }
101}
102
103impl From<mpz_common::ContextError> for VerifierError {
104    fn from(e: mpz_common::ContextError) -> Self {
105        Self::new(ErrorKind::Mpc, e)
106    }
107}
108
109impl From<MpcTlsError> for VerifierError {
110    fn from(e: MpcTlsError) -> Self {
111        Self::new(ErrorKind::Mpc, e)
112    }
113}
114
115impl From<ZkAesCtrError> for VerifierError {
116    fn from(e: ZkAesCtrError) -> Self {
117        Self::new(ErrorKind::Zk, e)
118    }
119}
120
121impl From<EncodingError> for VerifierError {
122    fn from(e: EncodingError) -> Self {
123        Self::new(ErrorKind::Commit, e)
124    }
125}