walletkit_core/
error.rs

1use thiserror::Error;
2
3#[cfg(feature = "v4")]
4use world_id_core::AuthenticatorError;
5
6/// Error outputs from `WalletKit`
7#[derive(Debug, Error, uniffi::Error)]
8pub enum WalletKitError {
9    /// Invalid input provided (e.g., incorrect length, format, etc.)
10    #[error("invalid_input_{attribute}")]
11    InvalidInput {
12        /// The attribute that is invalid
13        attribute: String,
14        /// The reason the input is invalid
15        reason: String,
16    },
17
18    /// The presented data is not a valid U256 integer
19    #[error("invalid_number")]
20    InvalidNumber,
21
22    /// Unexpected error serializing information
23    #[error("serialization_error")]
24    SerializationError {
25        /// The error message from the serialization
26        error: String,
27    },
28
29    /// Network connection error with details
30    #[error("network_error at {url}: {error}")]
31    NetworkError {
32        /// The URL of the request
33        url: String,
34        /// The error message from the request
35        error: String,
36        /// The HTTP status code of the request, if available
37        status: Option<u16>,
38    },
39
40    /// HTTP request failure
41    #[error("request_error")]
42    Reqwest {
43        /// The error message from the request
44        error: String,
45    },
46
47    /// Unhandled error generating a Zero-Knowledge Proof
48    #[error("proof_generation_error")]
49    ProofGeneration {
50        /// The error message from the proof generation
51        error: String,
52    },
53
54    /// The `semaphore` feature flag is not enabled
55    #[error("semaphore_not_enabled")]
56    SemaphoreNotEnabled,
57
58    /// The requested credential is not issued for this World ID
59    #[error("credential_not_issued")]
60    CredentialNotIssued,
61
62    /// The requested credential has not been submitted on-chain
63    #[error("credential_not_mined")]
64    CredentialNotMined,
65
66    /// This operation requires a registered account and an account is not registered
67    /// for this authenticator. Call `create_account` first to register it.
68    #[error("Account is not registered for this authenticator.")]
69    AccountDoesNotExist,
70
71    /// The account already exists for this authenticator. Call `account_index` to get the account index.
72    #[error("Account already exists for this authenticator.")]
73    AccountAlreadyExists,
74
75    /// The public key was not found in the batch, i.e. the authenticator is not authorized to sign for this action
76    #[error("unauthorized_authenticator")]
77    UnauthorizedAuthenticator,
78
79    /// An unexpected error occurred with the Authenticator
80    #[error("unexpected_authenticator_error")]
81    AuthenticatorError {
82        /// The error message from the authenticator
83        error: String,
84    },
85
86    /// An unexpected error occurred
87    #[error("unexpected_error: {error}")]
88    Generic {
89        /// The details of the error
90        error: String,
91    },
92}
93
94impl From<reqwest::Error> for WalletKitError {
95    fn from(error: reqwest::Error) -> Self {
96        Self::Reqwest {
97            error: error.to_string(),
98        }
99    }
100}
101
102impl From<semaphore_rs::protocol::ProofError> for WalletKitError {
103    fn from(error: semaphore_rs::protocol::ProofError) -> Self {
104        Self::ProofGeneration {
105            error: error.to_string(),
106        }
107    }
108}
109
110#[cfg(feature = "v4")]
111impl From<AuthenticatorError> for WalletKitError {
112    fn from(error: AuthenticatorError) -> Self {
113        match error {
114            AuthenticatorError::AccountDoesNotExist => Self::AccountDoesNotExist,
115            AuthenticatorError::AccountAlreadyExists => Self::AccountAlreadyExists,
116
117            AuthenticatorError::NetworkError(error) => Self::NetworkError {
118                url: error
119                    .url()
120                    .map(std::string::ToString::to_string)
121                    .unwrap_or_default(),
122                error: error.to_string(),
123                status: None,
124            },
125            AuthenticatorError::PublicKeyNotFound => Self::UnauthorizedAuthenticator,
126            AuthenticatorError::GatewayError { status, body } => Self::NetworkError {
127                url: "gateway".to_string(),
128                error: body,
129                status: Some(status.as_u16()),
130            },
131            AuthenticatorError::PrimitiveError(error) => {
132                use world_id_core::primitives::PrimitiveError;
133                match error {
134                    PrimitiveError::InvalidInput { attribute, reason } => {
135                        Self::InvalidInput { attribute, reason }
136                    }
137                    _ => Self::Generic {
138                        error: error.to_string(),
139                    },
140                }
141            }
142
143            _ => Self::AuthenticatorError {
144                error: error.to_string(),
145            },
146        }
147    }
148}