sbm_rs/
errors.rs

1use axum::{
2    http::StatusCode,
3    response::{IntoResponse, Response},
4};
5use derive_more::From;
6
7pub type Result<T> = core::result::Result<T, Error>;
8
9#[derive(Debug, From)]
10pub enum Error {
11    ConfigNotFound,
12    ConfigHomeNotFound,
13
14    #[from]
15    Io(std::io::Error),
16
17    #[from]
18    EnvVar(std::env::VarError),
19
20    #[from]
21    Askama(askama::Error),
22
23    #[from]
24    SerdeError(serde_json::Error),
25
26    #[from]
27    TomlDeError(toml::de::Error),
28
29    #[from]
30    TomlSerError(toml::ser::Error),
31}
32
33impl core::fmt::Display for Error {
34    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), std::fmt::Error> {
35        match self {
36            Error::ConfigNotFound => write!(
37                fmt,
38                "Config file can't be found at ~/.config/sbm-rs/config.toml"
39            ),
40            Error::ConfigHomeNotFound => write!(fmt, "Config home can't be found"),
41            Error::Io(e) => write!(fmt, "{e}"),
42            Error::EnvVar(e) => write!(fmt, "Environment variable error: {e}"),
43            Error::Askama(e) => write!(fmt, "Askama error: {e}"),
44            Error::SerdeError(e) => write!(fmt, "Serde error: {e}"),
45            Error::TomlDeError(e) => write!(fmt, "TOML deserialization error: {e}"),
46            Error::TomlSerError(e) => write!(fmt, "TOML serialization error: {e}"),
47        }
48    }
49}
50
51impl IntoResponse for Error {
52    fn into_response(self) -> Response {
53        let (status, error_message) = match self {
54            Error::ConfigNotFound => (
55                StatusCode::INTERNAL_SERVER_ERROR,
56                format!("Config file can't be found at ~/.config/sbm-rs/config.toml"),
57            ),
58            Error::ConfigHomeNotFound => (
59                StatusCode::INTERNAL_SERVER_ERROR,
60                format!("Config home can't be found"),
61            ),
62            Error::Io(ref e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")),
63            Error::EnvVar(ref e) => (
64                StatusCode::INTERNAL_SERVER_ERROR,
65                format!("Environment variable error: {e}"),
66            ),
67            Error::Askama(ref e) => (
68                StatusCode::INTERNAL_SERVER_ERROR,
69                format!("Askama error: {e}"),
70            ),
71            Error::SerdeError(ref e) => (
72                StatusCode::INTERNAL_SERVER_ERROR,
73                format!("Serde error: {e}"),
74            ),
75            Error::TomlDeError(ref e) => (
76                StatusCode::INTERNAL_SERVER_ERROR,
77                format!("TOML deserialization error: {e}"),
78            ),
79            Error::TomlSerError(ref e) => (
80                StatusCode::INTERNAL_SERVER_ERROR,
81                format!("TOML serialization error: {e}"),
82            ),
83        };
84
85        println!("{} {}", &status, &error_message);
86        (status, error_message).into_response()
87    }
88}