summaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 1b4be19bd2d0732bd0b3e1cfc86bc2b968f60a24 (plain) (blame)
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
97
98
99
use ipnetwork::IpNetworkError;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use mac_address::MacParseError;
use serde_json::json;
use std::io;
use tracing::{error, warn};
use utoipa::ToSchema;

#[derive(Debug, thiserror::Error, ToSchema)]
pub enum Error {
    #[error("json: {source}")]
    Json {
        #[from]
        source: serde_json::Error,
    },

    #[error("buffer parse: {source}")]
    ParseInt {
        #[from]
        source: std::num::ParseIntError,
    },

    #[error("string parse: {source}")]
    IpParse {
        #[from]
        source: IpNetworkError,
    },

    #[error("mac parse: {source}")]
    MacParse {
        #[from]
        source: MacParseError,
    },

    #[error("io: {source}")]
    Io {
        #[from]
        source: io::Error,
    },

    #[error("No ip set for device but ping requested")]
    NoIpOnPing,
}

impl IntoResponse for Error {
    fn into_response(self) -> Response {
        let (status, error_message) = match self {
            Self::Json { source } => {
                // !THIS REALLY SHOULD NOT HAPPEN!:
                // Json file has to had been tampered with by an external force
                error!("{source}");
                (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
            }
            Self::ParseInt { source } => {
                // !THIS REALLY SHOULD NOT HAPPEN!:
                // Mac Address `&str` can't be converted to hex, which should be impossible trough
                // `MacAddress` type-check on device registration and edit
                error!("{source}");
                (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
            }
            Self::Io { source } => {
                if source.kind() == io::ErrorKind::NotFound {
                    warn!("unknown device requested");
                    (StatusCode::NOT_FOUND, "Requested device not found")
                } else {
                    error!("{source}");
                    (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
                }
            }
            Self::MacParse { source } => {
                warn!("{source}");
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "The given MAC-Address couldn't be parsed",
                )
            }
            Self::IpParse { source } => {
                warn!("{source}");
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "The given IP-Address couldn't be parsed",
                )
            }
            Self::NoIpOnPing => {
                warn!("Ping requested but no ip given");
                (
                    StatusCode::BAD_REQUEST,
                    "No IP saved for device, ping can't be executed. Device may be started anyway",
                )
            }
        };
        let body = Json(json!({
            "error": error_message,
        }));
        (status, body).into_response()
    }
}