credibil_did/
error.rs

1/// DID resolution error codes
2#[derive(thiserror::Error, Debug)]
3pub enum Error {
4    /// The DID method is not supported.
5    #[error("methodNotSupported")]
6    MethodNotSupported(String),
7
8    /// The DID supplied to the DID resolution function does not conform to
9    /// valid syntax.
10    #[error("invalidDid")]
11    InvalidDid(String),
12
13    /// The DID resolver was unable to find the DID document resulting from
14    /// this resolution request.
15    #[error("notFound")]
16    NotFound(String),
17
18    /// The representation requested via the accept input metadata property is
19    /// not supported by the DID method and/or DID resolver.
20    #[error("representationNotSupported")]
21    RepresentationNotSupported(String),
22
23    /// The DID URL is invalid
24    #[error("invalidDidUrl")]
25    InvalidDidUrl(String),
26
27    // ---- Creation Errors ----  //
28    /// The byte length of raw public key does not match that expected for the
29    /// associated multicodecValue.
30    #[error("invalidPublicKeyLength")]
31    InvalidPublicKeyLength(String),
32
33    /// The public key is invalid
34    #[error("invalidPublicKey")]
35    InvalidPublicKey(String),
36
37    /// Public key format is not known to the implementation.
38    #[error("unsupportedPublicKeyType")]
39    UnsupportedPublicKeyType(String),
40
41    /// Other, unspecified errors.
42    #[error(transparent)]
43    Other(#[from] anyhow::Error),
44}
45
46impl Error {
47    /// Returns the error code.
48    #[must_use]
49    pub fn code(&self) -> String {
50        self.to_string()
51    }
52
53    /// Returns the associated error message.
54    #[must_use]
55    pub fn message(&self) -> String {
56        match self {
57            Self::MethodNotSupported(msg)
58            | Self::InvalidDid(msg)
59            | Self::NotFound(msg)
60            | Self::InvalidDidUrl(msg)
61            | Self::RepresentationNotSupported(msg)
62            | Self::InvalidPublicKeyLength(msg)
63            | Self::InvalidPublicKey(msg)
64            | Self::UnsupportedPublicKeyType(msg) => msg.clone(),
65            Self::Other(err) => err.to_string(),
66        }
67    }
68}
69
70// impl From<anyhow::Error> for Error {
71//     fn from(err: anyhow::Error) -> Self {
72//         Self
73//     }
74// }
75
76#[cfg(test)]
77mod test {
78    use super::*;
79
80    #[test]
81    fn error_code() {
82        let err = Error::MethodNotSupported("Method not supported".into());
83        assert_eq!(err.message(), "Method not supported");
84    }
85}