Skip to main content

rustauth_oauth_provider/
error.rs

1use http::StatusCode;
2use rustauth_core::error::RustAuthError;
3use serde::Serialize;
4
5/// OAuth provider runtime error.
6#[derive(Debug, Clone, thiserror::Error)]
7#[error("{error}: {error_description}")]
8pub struct OAuthProviderError {
9    pub status: StatusCode,
10    pub error: String,
11    pub error_description: String,
12}
13
14impl OAuthProviderError {
15    pub fn new(
16        status: StatusCode,
17        error: impl Into<String>,
18        error_description: impl Into<String>,
19    ) -> Self {
20        Self {
21            status,
22            error: error.into(),
23            error_description: error_description.into(),
24        }
25    }
26
27    pub fn invalid_request(description: impl Into<String>) -> Self {
28        Self::new(StatusCode::BAD_REQUEST, "invalid_request", description)
29    }
30
31    pub fn invalid_client(description: impl Into<String>) -> Self {
32        Self::new(StatusCode::BAD_REQUEST, "invalid_client", description)
33    }
34
35    pub fn unauthorized(description: impl Into<String>) -> Self {
36        Self::new(StatusCode::UNAUTHORIZED, "invalid_client", description)
37    }
38
39    pub fn invalid_scope(description: impl Into<String>) -> Self {
40        Self::new(StatusCode::BAD_REQUEST, "invalid_scope", description)
41    }
42
43    pub fn access_denied(description: impl Into<String>) -> Self {
44        Self::new(StatusCode::FORBIDDEN, "access_denied", description)
45    }
46}
47
48impl From<OAuthProviderError> for RustAuthError {
49    fn from(error: OAuthProviderError) -> Self {
50        RustAuthError::Api(error.to_string())
51    }
52}
53
54#[derive(Debug, Serialize)]
55pub(crate) struct OAuthErrorBody<'a> {
56    pub error: &'a str,
57    pub error_description: &'a str,
58}