dogy_backend_api/middleware/auth/
error.rs

1//! This module contains the error types about authentication.
2use derive_more::From;
3use serde::Serialize;
4use serde_with::serde_as;
5
6/// Result Type for Auth.
7pub type Result<T> = core::result::Result<T, Error>;
8
9/// Error types for Authentication.
10#[serde_as]
11#[derive(Debug, Serialize, From)]
12#[serde(tag = "code", content = "details")]
13pub enum Error {
14    // Internals
15    /// This error will occur if the decoding key is invalid.
16    ///
17    /// Namely, if either the decoding key which is `RSA_MODULUS` is empty or if its invalid.
18    InvalidDecodingKey,
19
20    /// This error will occur if the token provider in the `Authorization` header is invalid after
21    /// decoding.
22    InvalidToken,
23
24    // Middleware-related
25    /// This error will occur if the request does not contain an `Authorization` header.
26    MissingAuthHeader,
27
28    /// This error will occur if the request does not contain a `Bearer` prefix in the
29    /// `Authorization` header.
30    NoBearerPrefix,
31
32    // User-related
33    /// This error will occur if an authenticated user is not found in the database.
34    UserNotFound { user_id: String },
35}
36
37impl std::fmt::Display for Error {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
39        write!(f, "{self:?}")
40    }
41}
42
43impl std::error::Error for Error {}