identity_resolver/
error.rs

1// Copyright 2020-2022 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4/// Alias for a `Result` with the error type [`Error`].
5pub type Result<T, E = Error> = core::result::Result<T, E>;
6
7/// Error returned from the [Resolver's](crate::Resolver) methods.
8///
9/// The [`Self::error_cause`](Self::error_cause()) method provides information about the cause of the error.
10#[derive(Debug)]
11pub struct Error {
12  error_cause: ErrorCause,
13}
14
15impl Error {
16  pub(crate) fn new(cause: ErrorCause) -> Self {
17    Self { error_cause: cause }
18  }
19
20  /// Returns the cause of the error.
21  pub fn error_cause(&self) -> &ErrorCause {
22    &self.error_cause
23  }
24
25  /// Converts the error into [`ErrorCause`].
26  pub fn into_error_cause(self) -> ErrorCause {
27    self.error_cause
28  }
29}
30
31impl std::fmt::Display for Error {
32  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33    write!(f, "{}", self.error_cause)
34  }
35}
36
37impl std::error::Error for Error {
38  fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
39    self.error_cause.source()
40  }
41}
42
43/// Error failure modes associated with the methods on the [Resolver's](crate::Resolver).
44///
45/// NOTE: This is a "read only error" in the sense that it can only be constructed by the methods in this crate.
46#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
47#[non_exhaustive]
48pub enum ErrorCause {
49  /// Caused by a failure to parse a DID string during DID resolution.
50  #[error("did resolution failed: could not parse the given did")]
51  #[non_exhaustive]
52  DIDParsingError {
53    /// The source of the parsing error.
54    source: Box<dyn std::error::Error + Send + Sync + 'static>,
55  },
56  /// A handler attached to the [`Resolver`](crate::resolution::Resolver) attempted to resolve the DID, but the
57  /// resolution did not succeed.
58  #[error("did resolution failed: the attached handler failed")]
59  #[non_exhaustive]
60  HandlerError {
61    /// The source of the handler error.
62    source: Box<dyn std::error::Error + Send + Sync + 'static>,
63  },
64  /// Caused by attempting to resolve a DID whose method does not have a corresponding handler attached to the
65  /// [`Resolver`](crate::resolution::Resolver).
66  #[error("did resolution failed: the DID method \"{method}\" is not supported by the resolver")]
67  UnsupportedMethodError {
68    /// The method that is unsupported.
69    method: String,
70  },
71  /// No client attached to the specific network.
72  #[error("none of the attached clients support the network {0}")]
73  UnsupportedNetwork(String),
74}