Skip to main content

oxidite_core/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum Error {
5    #[error("Internal server error: {0}")]
6    InternalServerError(String),
7    #[error("Resource not found: {0}")]
8    NotFound(String),
9    #[error("Bad request: {0}")]
10    BadRequest(String),
11    #[error("Unauthorized access: {0}")]
12    Unauthorized(String),
13    #[error("Forbidden: {0}")]
14    Forbidden(String),
15    #[error("Resource conflict: {0}")]
16    Conflict(String),
17    #[error("Validation failed: {0}")]
18    Validation(String),
19    #[error("Rate limit exceeded: {0}")]
20    RateLimited(String),
21    #[error("Service temporarily unavailable: {0}")]
22    ServiceUnavailable(String),
23    #[error("Method not allowed: {0}")]
24    MethodNotAllowed(String),
25    #[error(transparent)]
26    Hyper(#[from] hyper::Error),
27    #[error(transparent)]
28    Io(#[from] std::io::Error),
29    #[error(transparent)]
30    SerdeJson(#[from] serde_json::Error),
31    #[error(transparent)]
32    SerdeUrlEncoded(#[from] serde_urlencoded::de::Error),
33    #[error(transparent)]
34    Http(#[from] http::Error),
35    #[error(transparent)]
36    Utf8(#[from] std::str::Utf8Error),
37}
38
39/// A specialized Result type for Oxidite applications
40pub type Result<T> = std::result::Result<T, Error>;
41
42impl Error {
43    /// Get the HTTP status code for this error
44    pub fn status_code(&self) -> hyper::StatusCode {
45        match self {
46            Error::NotFound(_) => hyper::StatusCode::NOT_FOUND,
47            Error::BadRequest(_) | Error::SerdeJson(_) | Error::SerdeUrlEncoded(_) | Error::Utf8(_) => hyper::StatusCode::BAD_REQUEST,
48            Error::Unauthorized(_) => hyper::StatusCode::UNAUTHORIZED,
49            Error::Forbidden(_) => hyper::StatusCode::FORBIDDEN,
50            Error::Conflict(_) => hyper::StatusCode::CONFLICT,
51            Error::Validation(_) => hyper::StatusCode::UNPROCESSABLE_ENTITY,
52            Error::RateLimited(_) => hyper::StatusCode::TOO_MANY_REQUESTS,
53            Error::ServiceUnavailable(_) => hyper::StatusCode::SERVICE_UNAVAILABLE,
54            Error::MethodNotAllowed(_) => hyper::StatusCode::METHOD_NOT_ALLOWED,
55            Error::InternalServerError(_) | Error::Hyper(_) | Error::Io(_) | Error::Http(_) => hyper::StatusCode::INTERNAL_SERVER_ERROR,
56        }
57    }
58
59    /// Check if this is a client error (4xx status code)
60    /// These errors are expected and should be logged at debug/trace level
61    pub fn is_client_error(&self) -> bool {
62        let status = self.status_code();
63        status.is_client_error()
64    }
65
66    /// Check if this is a server error (5xx status code)
67    /// These errors are unexpected and should be logged at error level
68    pub fn is_server_error(&self) -> bool {
69        let status = self.status_code();
70        status.is_server_error()
71    }
72}
73
74/// Render an Ignition-style HTML error page
75pub fn render_ignition_error(error: &Error) -> String {
76    let error_type = match error {
77        Error::InternalServerError(_) => "Internal Server Error",
78        Error::NotFound(_) => "Not Found",
79        Error::BadRequest(_) => "Bad Request",
80        Error::Unauthorized(_) => "Unauthorized",
81        Error::Forbidden(_) => "Forbidden",
82        Error::Conflict(_) => "Conflict",
83        Error::Validation(_) => "Validation Error",
84        Error::RateLimited(_) => "Rate Limited",
85        Error::ServiceUnavailable(_) => "Service Unavailable",
86        Error::MethodNotAllowed(_) => "Method Not Allowed",
87        Error::Hyper(_) => "Hyper Protocol Error",
88        Error::Io(_) => "I/O Error",
89        Error::SerdeJson(_) => "JSON Serialization Error",
90        Error::SerdeUrlEncoded(_) => "URL Encoded Error",
91        Error::Http(_) => "HTTP Error",
92        Error::Utf8(_) => "UTF-8 Encoding Error",
93    };
94
95    let error_message = error.to_string();
96
97    format!(r#"<!DOCTYPE html>
98<html lang="en">
99<head>
100    <meta charset="UTF-8">
101    <meta name="viewport" content="width=device-width, initial-scale=1.0">
102    <title>Oxidite Exception: {}</title>
103    <style>
104        :root {{
105            --bg-color: #0f1115;
106            --surface: #1e2128;
107            --primary: #f05033;
108            --text: #e2e8f0;
109            --text-muted: #94a3b8;
110            --border: #334155;
111            --danger: #ef4444;
112        }}
113        body {{
114            font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
115            background-color: var(--bg-color);
116            color: var(--text);
117            margin: 0;
118            padding: 0;
119            display: flex;
120            justify-content: center;
121        }}
122        .container {{
123            max-width: 1200px;
124            width: 100%;
125            margin: 40px;
126            background-color: var(--surface);
127            border-radius: 12px;
128            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
129            overflow: hidden;
130            border: 1px solid var(--border);
131        }}
132        .header {{
133            background: linear-gradient(135deg, #2a0808 0%, var(--surface) 100%);
134            padding: 40px;
135            border-bottom: 1px solid var(--border);
136        }}
137        .logo {{
138            font-size: 24px;
139            font-weight: 800;
140            color: var(--primary);
141            margin-bottom: 20px;
142            display: flex;
143            align-items: center;
144            gap: 10px;
145        }}
146        .logo svg {{
147            width: 32px;
148            height: 32px;
149        }}
150        h1 {{
151            margin: 0;
152            font-size: 32px;
153            font-weight: 700;
154            color: white;
155            line-height: 1.2;
156        }}
157        .exception-type {{
158            display: inline-block;
159            background-color: rgba(239, 68, 68, 0.1);
160            color: var(--danger);
161            padding: 4px 12px;
162            border-radius: 9999px;
163            font-size: 14px;
164            font-weight: 600;
165            margin-bottom: 16px;
166        }}
167        .content {{
168            padding: 40px;
169        }}
170        .stack-trace {{
171            background-color: #0d1117;
172            border-radius: 8px;
173            padding: 20px;
174            font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
175            font-size: 14px;
176            line-height: 1.6;
177            overflow-x: auto;
178            border: 1px solid var(--border);
179        }}
180        .footer {{
181            padding: 20px 40px;
182            background-color: #15181e;
183            border-top: 1px solid var(--border);
184            font-size: 14px;
185            color: var(--text-muted);
186            display: flex;
187            justify-content: space-between;
188        }}
189    </style>
190</head>
191<body>
192    <div class="container">
193        <div class="header">
194            <div class="logo">
195                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
196                    <path d="M12 2v20M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"/>
197                </svg>
198                Oxidite
199            </div>
200            <div class="exception-type">{}</div>
201            <h1>{}</h1>
202        </div>
203        <div class="content">
204            <h3>Stack Trace</h3>
205            <div class="stack-trace">
206                <div>Environment: development</div>
207                <div style="color: var(--text-muted); margin-top: 10px;">Backtrace captured at crash site:</div>
208                <div style="color: var(--primary); margin-top: 10px;">{}</div>
209            </div>
210        </div>
211        <div class="footer">
212            <span>Oxidite Framework v2.2.0</span>
213            <span>Running in Development Mode</span>
214        </div>
215    </div>
216</body>
217</html>"#, error_type, error_type, error_message, error_message)
218}