aboutsummaryrefslogtreecommitdiff
path: root/src/error/mod.rs
blob: 4b8e1c496b17c6d74455bfd5d00d21044bbf70c3 (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
mod client;
mod server;

pub use client::ClientError;
pub use server::ServerError;

use argon2::password_hash::Error as PasswordHashError;
use axum::extract::rejection::JsonRejection;
use axum_extra::typed_header::TypedHeaderRejection;
use sea_orm::DbErr;

pub enum ApiError {
    Client(ClientError),
    Server(ServerError),
}

impl From<ClientError> for ApiError {
    #[inline]
    fn from(value: ClientError) -> Self {
        Self::Client(value)
    }
}

impl From<ServerError> for ApiError {
    #[inline]
    fn from(value: ServerError) -> Self {
        Self::Server(value)
    }
}

impl From<JsonRejection> for ApiError {
    fn from(value: JsonRejection) -> Self {
        ClientError::BadJsonBody(value.body_text()).into()
    }
}

impl From<TypedHeaderRejection> for ApiError {
    fn from(value: TypedHeaderRejection) -> Self {
        ClientError::BadAuthTokenHeader(value.to_string()).into()
    }
}

impl From<DbErr> for ApiError {
    fn from(value: DbErr) -> Self {
        ServerError::Database(value.to_string()).into()
    }
}

impl From<PasswordHashError> for ApiError {
    fn from(value: PasswordHashError) -> Self {
        ServerError::PasswordHash(value.to_string()).into()
    }
}