bitwarden_importers/importers/onepassword/access/error.rs
1//! Error type for the 1Password client.
2//!
3//! The codes noted below are 1Password server error codes.
4
5use thiserror::Error;
6
7/// Errors returned by the 1Password access library.
8#[derive(Debug, Error)]
9pub enum OnePasswordError {
10 /// Network or transport failure.
11 #[error("network error: {0}")]
12 Network(String),
13
14 /// Invalid username, password, or Secret Key (1Password code 102).
15 #[error("invalid credentials")]
16 BadCredentials,
17
18 /// The server refused the email as malformed. An email with no account is not refused, it
19 /// fails later as `BadCredentials`.
20 #[error("invalid email")]
21 InvalidEmail,
22
23 /// The sign-in subdomain is not a valid DNS label.
24 #[error("invalid sign-in address: {0}")]
25 InvalidSignInAddress(String),
26
27 /// The Secret Key cannot be one: too short, too long, or an unknown format.
28 #[error("invalid account key: {0}")]
29 InvalidAccountKey(String),
30
31 /// The requested resource was not found (1Password code 117).
32 #[error("not found")]
33 NotFound,
34
35 /// The account requires two-factor authentication to continue.
36 #[error("two-factor authentication required")]
37 TwoFactorRequired,
38
39 /// A submitted two-factor code was rejected.
40 #[error("two-factor authentication failed")]
41 TwoFactorFailed,
42
43 /// Decryption of a server payload failed.
44 #[error("decryption failed")]
45 Decryption,
46
47 /// A server response could not be parsed.
48 #[error("failed to parse server response")]
49 Parse,
50
51 /// The server refused a request without a 1Password error body saying why.
52 #[error("unexpected response from '{endpoint}' (HTTP {status})")]
53 UnexpectedStatus {
54 /// The endpoint, relative to the API root.
55 endpoint: String,
56 /// The HTTP status code.
57 status: u16,
58 },
59
60 /// An item, category, or auth method that is not supported yet.
61 #[error("unsupported: {0}")]
62 Unsupported(String),
63
64 /// An invariant was violated: malformed input, a size mismatch, or a "should not happen" case.
65 #[error("internal error: {0}")]
66 Internal(String),
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn errors_describe_themselves() {
75 assert_eq!(
76 OnePasswordError::Network("timed out".into()).to_string(),
77 "network error: timed out"
78 );
79 assert_eq!(
80 OnePasswordError::Unsupported("Duo".into()).to_string(),
81 "unsupported: Duo"
82 );
83 assert_eq!(
84 OnePasswordError::BadCredentials.to_string(),
85 "invalid credentials"
86 );
87 }
88}