Skip to main content

beyondigital_auth_sdk/
error.rs

1use thiserror::Error;
2
3use crate::logout::LogoutTokenError;
4
5#[derive(Debug, Error)]
6pub enum AuthError {
7    #[error("oauth error {error}: {description} (http {status})")]
8    OAuth {
9        error: String,
10        description: String,
11        status: u16,
12    },
13    #[error("jwt error ({code}): {message}")]
14    Jwt { code: String, message: String },
15    /// A back-channel logout token that must not be acted on; see [`LogoutTokenError::reason`].
16    #[error(transparent)]
17    LogoutToken(LogoutTokenError),
18    /// The caller stopped a device flow through its [`CancelToken`](crate::CancelToken).
19    #[error("cancelled: {0}")]
20    Cancelled(String),
21    #[error("network error: {0}")]
22    Network(String),
23    #[error("config error: {0}")]
24    Config(String),
25}
26
27impl AuthError {
28    pub fn oauth(error: impl Into<String>, description: impl Into<String>, status: u16) -> Self {
29        Self::OAuth {
30            error: error.into(),
31            description: description.into(),
32            status,
33        }
34    }
35    pub fn jwt(message: impl Into<String>, code: impl Into<String>) -> Self {
36        Self::Jwt {
37            message: message.into(),
38            code: code.into(),
39        }
40    }
41    pub fn is_oauth(&self) -> bool {
42        matches!(self, Self::OAuth { .. })
43    }
44    /// A token problem: a [`Jwt`](Self::Jwt) error or a rejected [logout token](Self::LogoutToken).
45    pub fn is_jwt(&self) -> bool {
46        matches!(self, Self::Jwt { .. } | Self::LogoutToken(_))
47    }
48    pub fn is_cancelled(&self) -> bool {
49        matches!(self, Self::Cancelled(_))
50    }
51}