identity_credential/domain_linkage/
error.rs

1// Copyright 2020-2023 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use std::error::Error;
5
6pub(crate) type DomainLinkageValidationResult = Result<(), DomainLinkageValidationError>;
7
8/// An error caused by a failure to verify a Domain Linkage configuration or credential.
9#[derive(Debug, thiserror::Error)]
10pub struct DomainLinkageValidationError {
11  /// Cause of the error.
12  pub cause: DomainLinkageValidationErrorCause,
13  /// Source of the error.
14  pub source: Option<Box<dyn Error + Send + Sync + 'static>>,
15}
16
17/// List of errors caused by failures to verify a Domain Linkage configuration or credential.
18#[derive(Debug, thiserror::Error)]
19pub struct DomainLinkageValidationErrorList {
20  /// set of errors
21  pub errors: Vec<DomainLinkageValidationError>,
22}
23
24/// A set of errors coming from validating multiple domain linkage credentials.
25impl DomainLinkageValidationErrorList {
26  /// Create new set of domain linkage validation errors.
27  pub fn new(errors: Vec<DomainLinkageValidationError>) -> Self {
28    Self { errors }
29  }
30}
31
32// Implement for `Error` trait.
33impl std::fmt::Display for DomainLinkageValidationErrorList {
34  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35    write!(f, "{:?}", self.errors)
36  }
37}
38
39impl std::fmt::Display for DomainLinkageValidationError {
40  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41    write!(f, "{}", self.cause)
42  }
43}
44
45impl From<DomainLinkageValidationError> for &str {
46  fn from(value: DomainLinkageValidationError) -> Self {
47    value.cause.into()
48  }
49}
50
51/// The causes for why domain linkage validation can fail.
52#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
53#[non_exhaustive]
54pub enum DomainLinkageValidationErrorCause {
55  /// Caused when a Domain Linkage Credential cannot be successfully validated.
56  #[error("invalid credential")]
57  CredentialValidationError,
58  /// Caused by an invalid JWT.
59  #[error("invalid JWT")]
60  InvalidJwt,
61  /// Caused by a missing expiration date.
62  #[error("the expiration date is missing")]
63  MissingExpirationDate,
64  /// Caused by the presence of an id property on a Domain Linkage Credential.
65  #[error("id property is not allowed")]
66  ImpermissibleIdProperty,
67  /// Caused by a mismatch of the issuer and subject of the Domain Linkage Credential.
68  #[error("issuer DID does not match the subject")]
69  IssuerSubjectMismatch,
70  /// Caused by an invalid Domain Linkage Credential subject.
71  #[error("subject id is invalid")]
72  InvalidSubjectId,
73  /// Caused by the presence of multiple subjects on a Domain Linkage Credential.
74  #[error("credential contains multiple subjects")]
75  MultipleCredentialSubjects,
76  /// Caused by an invalid issuer DID.
77  #[error("invalid issuer DID")]
78  InvalidIssuer,
79  /// Caused by a missing id property on the Domain Linkage Credential subject.
80  #[error("subject id property is missing")]
81  MissingSubjectId,
82  /// Caused by an invalid `type` property on the Domain Linkage Credential.
83  #[error("credential type is invalid")]
84  InvalidTypeProperty,
85  /// Caused by a mismatch between the Domain Linkage Credential subject's origin and the provided domain origin.
86  #[error("the subject's origin does not match the provided domain origin")]
87  OriginMismatch,
88  /// Caused by a missing or invalid Domain Linkage Credential subject's origin.
89  #[error("the subject's origin property is either invalid or missing")]
90  InvalidSubjectOrigin,
91  /// Caused by an invalid semantic structure of the Domain Linkage Configuration.
92  #[error("invalid semantic structure of the domain linkage configuration")]
93  InvalidStructure,
94  /// List of errors stemming from multiple validations.
95  #[error("one or more validations failed")]
96  List,
97}