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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::fmt;

use crate::{http_parse_error::HttpParseError, Body, HttpResponse};

#[derive(Debug)]
pub struct ApiError {
    pub error_response: HttpResponse,
    pub method: Option<String>,
    pub path: Option<String>,
}

impl ApiError {
    pub fn new_with_html(code: u16, message: String) -> Self {
        ApiError {
            error_response: format_error(code, message),
            method: None,
            path: None,
        }
    }

    pub fn new_with_json(code: u16, message: String) -> Self {
        ApiError {
            error_response: HttpResponse::new(
                Some(Body::Json(serde_json::json!({"message": message}))),
                None,
                code,
            ),
            method: None,
            path: None,
        }
    }
}

impl fmt::Display for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl std::error::Error for ApiError {}

impl From<std::io::Error> for ApiError {
    fn from(error: std::io::Error) -> Self {
        ApiError::new_with_html(404, format!("IO Error: {}", error))
    }
}

impl From<Box<dyn std::error::Error>> for ApiError {
    fn from(error: Box<dyn std::error::Error>) -> ApiError {
        ApiError::new_with_json(500, error.to_string())
    }
}

impl From<serde_json::Error> for ApiError {
    fn from(error: serde_json::Error) -> Self {
        ApiError::new_with_json(400, format!("JSON Serialization Error: {}", error))
    }
}

impl From<&str> for ApiError {
    fn from(error: &str) -> Self {
        ApiError::new_with_json(400, error.to_string())
    }
}

impl From<HttpParseError> for ApiError {
    fn from(error: HttpParseError) -> Self {
        ApiError::new_with_json(
            400,
            format!("Error parsing HTTP request: {}", error.message),
        )
    }
}

fn format_error(error_code: u16, message: String) -> HttpResponse {
    let html = format!(
        "<!DOCTYPE html>
    <html lang=\"en\">
    <head>
        <meta charset=\"UTF-8\">
        <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
        <title>Error</title>
        <style>
        body {{
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: Arial, sans-serif;
        }}
        .error-container {{
            text-align: center;
        }}
        .error-container h1 {{
            font-size: 3em;
            color: #ff0000;
        }}
        .error-container p {{
            font-size: 1.5em;
        }}
    </style>
    </head>

    <body>
        <div class=\"error-container\">
            <h1>{} {}</h1>
            <p>{}</p>
        </div>
    </body>
    </html>",
        error_code,
        get_cannonical_reason(error_code),
        message
    );
    HttpResponse::new(
        Some(Body::Text(html)),
        Some(String::from("text/html")),
        error_code,
    )
}

fn get_cannonical_reason<'a>(status_code: u16) -> &'a str {
    match status_code {
        200 => "OK",
        201 => "Created",
        204 => "No Content",
        301 => "Moved Permanently",
        302 => "Found",
        304 => "Not Modified",
        400 => "Bad Request",
        401 => "Unauthorized",
        403 => "Forbidden",
        404 => "Not Found",
        405 => "Method Not Allowed",
        500 => "Internal Server Error",
        501 => "Not Implemented",
        502 => "Bad Gateway",
        503 => "Service Unavailable",
        _ => "Unknown Status Code",
    }
}