summaryrefslogtreecommitdiff
path: root/server/src/error.rs
blob: ebe1d93bb501fa91d66d33371c7e0d411e6e7df6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use axum::{
    http::{self, StatusCode},
    response::IntoResponse,
};
use openidconnect::{
    core::CoreErrorResponseType, ClaimsVerificationError, StandardErrorResponse,
};
use tracing::error;

type UserInfoError =
    openidconnect::UserInfoError<openidconnect::reqwest::Error<reqwest::Error>>;

type RequestTokenError = openidconnect::RequestTokenError<
    openidconnect::reqwest::Error<reqwest::Error>,
    StandardErrorResponse<CoreErrorResponseType>,
>;

#[derive(thiserror::Error, Debug)]
pub enum BridgeError {
    #[error("Invalid request: {0}")]
    InvalidRequest(String),

    #[error("Version conflict when updating object {0} to version {1}")]
    JournalConflict(String, i64),

    #[error("Requesting token failed")]
    OpenidRequestTokenError(#[from] RequestTokenError),

    #[error("Requesting user info failed")]
    OpenidUserInfoError(#[from] UserInfoError),

    #[error("Failed to configure OpenId request")]
    OpenIdConfigurationError(#[from] openidconnect::ConfigurationError),

    #[error("Unexpected authorization error")]
    UnexpectedInvalidAuthorization(#[from] ClaimsVerificationError),

    #[error("User is not logged in")]
    NotLoggedIn,

    #[error("Anyhow error")]
    AnyhowError(#[from] anyhow::Error),

    #[error("Authentication error")]
    SigningFailed(#[from] openidconnect::SigningError),

    #[error("Database error")]
    SqlxError(#[from] sqlx::Error),

    #[error("Uuid parse failed")]
    UuidError(#[from] uuid::Error),

    #[error("Serialization error")]
    SerdeError(#[from] serde_json::Error),

    #[error("Internal server error: {0}")]
    Internal(String),

    #[error("Duration out of range")]
    DurationOutOfRange(#[from] chrono::OutOfRangeError),

    #[error("Not found: {0}")]
    NotFound(String),

    #[error("Sequence number mismatch: {0} ≠ {1}")]
    UpdateConflict(i64, i64),
}

impl BridgeError {
    pub fn as_rejection(&self) -> (http::StatusCode, String) {
        match self {
            BridgeError::NotLoggedIn => {
                (StatusCode::UNAUTHORIZED, "Must be logged in".to_string())
            }
            BridgeError::OpenidRequestTokenError(_) => {
                (StatusCode::UNAUTHORIZED, "Error fetching token".to_string())
            }
            _ => (StatusCode::INTERNAL_SERVER_ERROR, "Error: {self}".to_string()),
        }
    }
}

impl IntoResponse for BridgeError {
    fn into_response(self) -> axum::response::Response {
        error!("Error occurred: {self:?}");
        self.as_rejection().into_response()
    }
}