summaryrefslogtreecommitdiff
path: root/src/errors.rs
blob: 6b53c2d3cd5dff5ccadba32c6da3fb3a9afd8a47 (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
89
90
91
92
93
94
95
96
use axum::{
    http::StatusCode,
    response::{IntoResponse, Response},
};
use derive_more::From;

pub type Result<T> = core::result::Result<T, Error>;

#[derive(Debug, From)]
pub enum Error {
    ConfigNotFound,
    ConfigHomeNotFound,

    #[from]
    Io(std::io::Error),

    #[from]
    EnvVar(std::env::VarError),

    #[from]
    Askama(askama::Error),

    #[from]
    SerdeError(serde_json::Error),

    #[from]
    TomlDeError(toml::de::Error),

    #[from]
    TomlSerError(toml::ser::Error),

    #[from]
    ParseIntError(std::num::ParseIntError),
}

impl core::fmt::Display for Error {
    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), std::fmt::Error> {
        match self {
            Error::ConfigNotFound => write!(
                fmt,
                "Config file can't be found at ~/.config/sbm-rs/config.toml"
            ),
            Error::ConfigHomeNotFound => write!(fmt, "Config home can't be found"),
            Error::Io(e) => write!(fmt, "{e}"),
            Error::EnvVar(e) => write!(fmt, "Environment variable error: {e}"),
            Error::Askama(e) => write!(fmt, "Askama error: {e}"),
            Error::SerdeError(e) => write!(fmt, "Serde error: {e}"),
            Error::TomlDeError(e) => write!(fmt, "TOML deserialization error: {e}"),
            Error::TomlSerError(e) => write!(fmt, "TOML serialization error: {e}"),
            Error::ParseIntError(e) => write!(fmt, "Parsing error: {e}"),
        }
    }
}

impl IntoResponse for Error {
    fn into_response(self) -> Response {
        let (status, error_message) = match self {
            Error::ConfigNotFound => (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Config file can't be found at ~/.config/sbm-rs/config.toml"),
            ),
            Error::ConfigHomeNotFound => (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Config home can't be found"),
            ),
            Error::Io(ref e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")),
            Error::EnvVar(ref e) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Environment variable error: {e}"),
            ),
            Error::Askama(ref e) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Askama error: {e}"),
            ),
            Error::SerdeError(ref e) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Serde error: {e}"),
            ),
            Error::TomlDeError(ref e) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("TOML deserialization error: {e}"),
            ),
            Error::TomlSerError(ref e) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("TOML serialization error: {e}"),
            ),
            Error::ParseIntError(ref e) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Parsing error: {e}"),
            ),
        };

        println!("{} {}", &status, &error_message);
        (status, error_message).into_response()
    }
}