Skip to main content

rustbasic_core/
errors.rs

1use crate::view::render;
2use crate::router::{IntoResponse, Response};
3use serde_json::json;
4use http::StatusCode;
5
6pub struct ErrorController;
7
8impl ErrorController {
9    /// Handler umum untuk menampilkan halaman error
10    pub fn show(code: u16, message: &str) -> Response {
11        let status = StatusCode::from_u16(code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
12        
13        let title = match code {
14            401 => "Unauthorized",
15            402 => "Payment Required",
16            403 => "Forbidden",
17            404 => "Page Not Found",
18            419 => "Page Expired",
19            429 => "Too Many Requests",
20            500 => "Server Error",
21            503 => "Service Unavailable",
22            _ => "Error",
23        };
24
25        (status, render("errors/minimal.rb.html", json!({
26            "code": code,
27            "title": title,
28            "message": message
29        }))).into_response()
30    }
31
32    /// Khusus untuk 404 Not Found (digunakan sebagai fallback)
33    pub async fn not_found() -> Response {
34        Self::show(404, "Maaf, halaman yang Anda cari tidak ditemukan.")
35    }
36}