Skip to main content

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    /// The user canceled the SSO login or the device enrollment.
44    #[error("canceled")]
45    Canceled,
46
47    /// The enrolled device denied the new device.
48    #[error("denied")]
49    Denied,
50
51    /// The user ran out of attempts at the verification code of a device enrollment.
52    #[error("too many wrong verification codes")]
53    VerificationCodeFailed,
54
55    /// Decryption of a server payload failed.
56    #[error("decryption failed")]
57    Decryption,
58
59    /// A server response could not be parsed.
60    #[error("failed to parse server response")]
61    Parse,
62
63    /// The server refused a request without a 1Password error body saying why.
64    #[error("unexpected response from '{endpoint}' (HTTP {status})")]
65    UnexpectedStatus {
66        /// The endpoint, relative to the API root.
67        endpoint: String,
68        /// The HTTP status code.
69        status: u16,
70    },
71
72    /// An item, category, or auth method that is not supported yet.
73    #[error("unsupported: {0}")]
74    Unsupported(String),
75
76    /// An invariant was violated: malformed input, a size mismatch, or a "should not happen" case.
77    #[error("internal error: {0}")]
78    Internal(String),
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn errors_describe_themselves() {
87        assert_eq!(
88            OnePasswordError::Network("timed out".into()).to_string(),
89            "network error: timed out"
90        );
91        assert_eq!(
92            OnePasswordError::Unsupported("Duo".into()).to_string(),
93            "unsupported: Duo"
94        );
95        assert_eq!(
96            OnePasswordError::BadCredentials.to_string(),
97            "invalid credentials"
98        );
99    }
100}