aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 1592a787adca7a8e16a86ad1623b7540d4093c39 (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
use std::error::Error;
use std::io;
use axum::http::StatusCode;
use axum::Json;
use axum::response::{IntoResponse, Response};
use serde_json::json;
use tracing::error;
use crate::auth::AuthError;

#[derive(Debug)]
pub enum WebolError {
    Generic,
    // User(UserError),
    Auth(AuthError),
    Ping(surge_ping::SurgeError),
    DB(sqlx::Error),
    IpParse(<std::net::IpAddr as std::str::FromStr>::Err),
    BufferParse(std::num::ParseIntError),
    Broadcast(io::Error),
}

impl IntoResponse for WebolError {
    fn into_response(self) -> Response {
        let (status, error_message) = match self {
            Self::Auth(err) => err.get(),
            // Self::User(err) => err.get(),
            Self::Generic => (StatusCode::INTERNAL_SERVER_ERROR, ""),
            Self::Ping(err) => {
                error!("Ping: {}", err.source().unwrap());
                (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
            },
            Self::IpParse(err) => {
                error!("server error: {}", err.to_string());
                (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
            },
            Self::DB(err) => {
                error!("server error: {}", err.to_string());
                (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
            },
            Self::Broadcast(err) => {
                error!("server error: {}", err.to_string());
                (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
            },
            Self::BufferParse(err) => {
                error!("server error: {}", err.to_string());
                (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
            },
        };
        let body = Json(json!({
            "error": error_message,
        }));
        (status, body).into_response()
    }
}

// #[derive(Debug)]
// pub enum UserError {
//     UnknownUUID,
// }
//
// impl UserError {
//     pub fn get(self) -> (StatusCode, &'static str) {
//         match self {
//             Self::UnknownUUID => (StatusCode::UNPROCESSABLE_ENTITY, "Unknown UUID"),
//         }
//     }
// }