sos_platform_authenticator/
error.rs

1use http::StatusCode;
2use thiserror::Error;
3
4/// Errors generated by the platform authenticator library.
5#[derive(Debug, Error)]
6pub enum Error {
7    /// Error generated when a keyring entry could not be found.
8    #[error("keyring entry not found")]
9    NoKeyringEntry,
10
11    /// Error generated when it is not possible to authenticate
12    /// using the platform authenticator and keyring; authentication
13    /// may still succeed with a password supplied by the user.
14    #[error("unauthorized")]
15    Unauthorized,
16
17    /// Error generated when an attempt to authenticate failed.
18    #[error("forbidden")]
19    Forbidden,
20
21    /// Error generated converting to UTF-8.
22    #[cfg(all(not(target_os = "android"), not(target_os = "macos")))]
23    #[error(transparent)]
24    Keyring(#[from] keyring::Error),
25
26    /// Error generated converting to UTF-8.
27    #[error(transparent)]
28    Utf8(#[from] std::str::Utf8Error),
29
30    /// Error generated by the security framework.
31    #[cfg(target_os = "macos")]
32    #[error(transparent)]
33    SecurityFramework(#[from] security_framework::base::Error),
34}
35
36impl From<&Error> for StatusCode {
37    fn from(value: &Error) -> Self {
38        match value {
39            Error::NoKeyringEntry => StatusCode::NOT_FOUND,
40            Error::Unauthorized => StatusCode::UNAUTHORIZED,
41            Error::Forbidden => StatusCode::FORBIDDEN,
42            _ => StatusCode::INTERNAL_SERVER_ERROR,
43        }
44    }
45}