use std::time::Duration;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ErrorKind {
Configuration,
Callback,
Network,
Provider,
Session,
Unauthenticated,
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("invalid OAuth configuration: {0}")]
Configuration(String),
#[error("failed to construct an OAuth endpoint URL")]
UrlConstruction,
#[error("secure random generation failed")]
Randomness,
#[error("missing or already consumed OAuth flow state")]
MissingFlowState,
#[error("OAuth flow state expired after {0:?}")]
ExpiredFlowState(Duration),
#[error("OAuth flow state was malformed")]
InvalidFlowState,
#[error("OAuth callback state did not match")]
StateMismatch,
#[error("invalid PKCE flow data")]
InvalidPkce,
#[error("OAuth HTTP request timed out")]
Timeout,
#[error("OAuth HTTP request failed")]
Http(#[source] reqwest::Error),
#[error("OAuth authorization was not completed (`{error}`)")]
AuthorizationDenied { error: String },
#[error("OAuth provider returned error `{error}`")]
OAuthProvider { error: String },
#[error("OAuth token exchange failed with HTTP status {status}")]
TokenExchange { status: u16 },
#[error("failed to decode OAuth token response")]
TokenDecode(#[source] serde_json::Error),
#[error("MediaWiki profile request failed with HTTP status {status}")]
ProfileRequest { status: u16 },
#[error("failed to decode MediaWiki user profile")]
ProfileDecode(#[source] serde_json::Error),
#[error("OAuth HTTP response exceeded the {limit} byte limit")]
ResponseTooLarge { limit: usize },
#[error("OAuth session operation failed")]
Session,
#[error("not logged in")]
NotLoggedIn,
#[error("session expired")]
SessionExpired,
}
impl Error {
pub fn kind(&self) -> ErrorKind {
match self {
Self::Configuration(_) | Self::UrlConstruction | Self::Randomness => {
ErrorKind::Configuration
}
Self::MissingFlowState
| Self::ExpiredFlowState(_)
| Self::InvalidFlowState
| Self::StateMismatch
| Self::InvalidPkce
| Self::AuthorizationDenied { .. } => ErrorKind::Callback,
Self::Timeout | Self::Http(_) => ErrorKind::Network,
Self::OAuthProvider { .. }
| Self::TokenExchange { .. }
| Self::TokenDecode(_)
| Self::ProfileRequest { .. }
| Self::ProfileDecode(_)
| Self::ResponseTooLarge { .. } => ErrorKind::Provider,
Self::Session => ErrorKind::Session,
Self::NotLoggedIn | Self::SessionExpired => ErrorKind::Unauthenticated,
}
}
pub(crate) fn configuration(message: impl Into<String>) -> Self {
Self::Configuration(message.into())
}
pub(crate) fn from_http(error: reqwest::Error) -> Self {
if error.is_timeout() {
Self::Timeout
} else {
Self::Http(error)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn errors_have_stable_categories() {
assert_eq!(Error::NotLoggedIn.kind(), ErrorKind::Unauthenticated);
assert_eq!(Error::StateMismatch.kind(), ErrorKind::Callback);
assert_eq!(Error::Timeout.kind(), ErrorKind::Network);
assert_eq!(
Error::OAuthProvider {
error: "invalid_grant".to_owned(),
}
.kind(),
ErrorKind::Provider
);
assert_eq!(
Error::Configuration("missing client_id".to_owned()).kind(),
ErrorKind::Configuration
);
}
}