did_utils/methods/
errors.rs

1use hyper::StatusCode;
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4
5/// Registry for [error] types found across the DID core specification,
6/// and especially during the DID resolution process.
7///
8/// [error]: https://www.w3.org/TR/did-spec-registries/#error
9#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Error)]
10#[serde(rename_all = "camelCase")]
11pub enum DIDResolutionError {
12    #[error("invalidDid")]
13    InvalidDid,
14    #[error("invalidDidUrl")]
15    InvalidDidUrl,
16    #[error("invalidDidUrlPrefix")]
17    InvalidDidUrlPrefix,
18    #[error("invalidDidUrlFormat")]
19    InvalidDidUrlFormat,
20    #[error("didUrlPartLengthTooShort")]
21    DidUrlPartLengthTooShort,
22    #[error("notFound")]
23    NotFound,
24    #[error("representationNotSupported")]
25    RepresentationNotSupported,
26    #[error("methodNotSupported")]
27    MethodNotSupported,
28    #[error("internalError")]
29    InternalError,
30    #[error("invalidPublicKey")]
31    InvalidPublicKey,
32    #[error("invalidPublicKeyLength")]
33    InvalidPublicKeyLength,
34    #[error("invalidPublicKeyType")]
35    InvalidPublicKeyType,
36    #[error("unsupportedPublicKeyType")]
37    UnsupportedPublicKeyType,
38    #[error("notAllowedVerificationMethodType")]
39    NotAllowedVerificationMethodType,
40    #[error("notAllowedKeyType")]
41    NotAllowedKeyType,
42    #[error("notAllowedMethod")]
43    NotAllowedMethod,
44    #[error("notAllowedCertificate")]
45    NotAllowedCertificate,
46    #[error("notAllowedLocalDuplicateKey")]
47    NotAllowedLocalDuplicateKey,
48    #[error("notAllowedLocalDerivedKey")]
49    NotAllowedLocalDerivedKey,
50    #[error("notAllowedGlobalDuplicateKey")]
51    NotAllowedGlobalDuplicateKey,
52    #[error("Non-success server response")]
53    NonSuccessResponse,
54}
55
56#[derive(Error, Debug)]
57#[non_exhaustive]
58pub enum DidWebError {
59    #[error("DID method not supported: {0}")]
60    MethodNotSupported(String),
61    #[error("Representation not supported: {0}")]
62    RepresentationNotSupported(String),
63    #[error("Invalid DID: {0}")]
64    InvalidDid(String),
65    #[error("Parsing error: {0}")]
66    ParsingError(#[from] ParsingErrorSource),
67    #[error("URL parsing error: {0}")]
68    HttpError(#[from] hyper::Error),
69    #[error("Non-success server response: {0}")]
70    NonSuccessResponse(StatusCode),
71    #[error(transparent)]
72    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
73}
74
75#[derive(Error, Debug)]
76pub enum ParsingErrorSource {
77    #[error("JSON parsing error: {0}")]
78    JsonError(#[from] serde_json::Error),
79    #[error("Invalid encoding: {0}")]
80    Utf8Error(#[from] std::string::FromUtf8Error),
81}
82
83impl From<serde_json::Error> for DidWebError {
84    fn from(error: serde_json::Error) -> Self {
85        DidWebError::ParsingError(ParsingErrorSource::JsonError(error))
86    }
87}
88
89impl From<std::string::FromUtf8Error> for DidWebError {
90    fn from(error: std::string::FromUtf8Error) -> Self {
91        DidWebError::ParsingError(ParsingErrorSource::Utf8Error(error))
92    }
93}