1use thiserror::Error;
2
3use world_id_core::primitives::PrimitiveError;
4
5#[cfg(feature = "storage")]
6use crate::storage::StorageError;
7use world_id_core::AuthenticatorError;
8
9#[derive(Debug, Error, uniffi::Error)]
11pub enum WalletKitError {
12 #[error("invalid_input_{attribute}")]
14 InvalidInput {
15 attribute: String,
17 reason: String,
19 },
20
21 #[error("invalid_number")]
23 InvalidNumber,
24
25 #[error("serialization_error")]
27 SerializationError {
28 error: String,
30 },
31
32 #[error("network_error at {url}: {error}")]
34 NetworkError {
35 url: String,
37 error: String,
39 status: Option<u16>,
41 },
42
43 #[error("request_error")]
45 Reqwest {
46 error: String,
48 },
49
50 #[error("proof_generation_error")]
52 ProofGeneration {
53 error: String,
55 },
56
57 #[error("semaphore_not_enabled")]
59 SemaphoreNotEnabled,
60
61 #[error("credential_not_issued")]
63 CredentialNotIssued,
64
65 #[error("credential_not_mined")]
67 CredentialNotMined,
68
69 #[error("Account is not registered for this authenticator.")]
72 AccountDoesNotExist,
73
74 #[error("Account already exists for this authenticator.")]
76 AccountAlreadyExists,
77
78 #[error("unauthorized_authenticator")]
80 UnauthorizedAuthenticator,
81
82 #[error("unexpected_authenticator_error: {error}")]
84 AuthenticatorError {
85 error: String,
87 },
88
89 #[error("unfulfillable_request")]
91 UnfulfillableRequest,
92
93 #[error("invalid response: {0}")]
98 ResponseValidation(String),
99
100 #[error("nullifier_replay")]
102 NullifierReplay,
103
104 #[error("groth16_material_cache_invalid")]
106 Groth16MaterialCacheInvalid {
107 path: String,
109 error: String,
111 },
112
113 #[error("groth16_material_embedded_load")]
115 Groth16MaterialEmbeddedLoad {
116 error: String,
118 },
119
120 #[error("unexpected_error: {error}")]
122 Generic {
123 error: String,
125 },
126}
127
128impl From<reqwest::Error> for WalletKitError {
129 fn from(error: reqwest::Error) -> Self {
130 Self::Reqwest {
131 error: error.to_string(),
132 }
133 }
134}
135
136impl From<PrimitiveError> for WalletKitError {
137 fn from(error: PrimitiveError) -> Self {
138 match error {
139 PrimitiveError::InvalidInput { attribute, reason } => {
140 Self::InvalidInput { attribute, reason }
141 }
142 PrimitiveError::Serialization(error) => Self::SerializationError { error },
143 PrimitiveError::Deserialization(reason) => Self::InvalidInput {
144 attribute: "deserialization".to_string(),
145 reason,
146 },
147 PrimitiveError::NotInField => Self::InvalidInput {
148 attribute: "field_element".to_string(),
149 reason: "Provided value is not in the field".to_string(),
150 },
151 PrimitiveError::OutOfBounds => Self::InvalidInput {
152 attribute: "index".to_string(),
153 reason: "Provided index is out of bounds".to_string(),
154 },
155 }
156 }
157}
158
159impl From<semaphore_rs::protocol::ProofError> for WalletKitError {
160 fn from(error: semaphore_rs::protocol::ProofError) -> Self {
161 Self::ProofGeneration {
162 error: error.to_string(),
163 }
164 }
165}
166
167#[cfg(feature = "storage")]
168impl From<StorageError> for WalletKitError {
169 fn from(error: StorageError) -> Self {
170 Self::Generic {
171 error: error.to_string(),
172 }
173 }
174}
175
176impl From<AuthenticatorError> for WalletKitError {
177 fn from(error: AuthenticatorError) -> Self {
178 match error {
179 AuthenticatorError::AccountDoesNotExist => Self::AccountDoesNotExist,
180 AuthenticatorError::AccountAlreadyExists => Self::AccountAlreadyExists,
181
182 AuthenticatorError::NetworkError(error) => Self::NetworkError {
183 url: error
184 .url()
185 .map(std::string::ToString::to_string)
186 .unwrap_or_default(),
187 error: error.to_string(),
188 status: None,
189 },
190 AuthenticatorError::PublicKeyNotFound => Self::UnauthorizedAuthenticator,
191 AuthenticatorError::GatewayError { status, body } => Self::NetworkError {
192 url: "gateway".to_string(),
193 error: body,
194 status: Some(status.as_u16()),
195 },
196 AuthenticatorError::PrimitiveError(error) => Self::from(error),
197
198 _ => Self::AuthenticatorError {
199 error: error.to_string(),
200 },
201 }
202 }
203}