Skip to main content

rustbasic_core/
errors.rs

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