60 lines
1.5 KiB
Rust
60 lines
1.5 KiB
Rust
|
use log::debug;
|
||
|
use rocket::{catch};
|
||
|
use rocket_dyn_templates::Template;
|
||
|
use rocket::response::Redirect;
|
||
|
use serde::Serialize;
|
||
|
|
||
|
// HELPERS AND ROUTES FOR 404 ERROR
|
||
|
|
||
|
#[derive(Debug, Serialize)]
|
||
|
pub struct ErrorContext {
|
||
|
pub back: Option<String>,
|
||
|
pub flash_name: Option<String>,
|
||
|
pub flash_msg: Option<String>,
|
||
|
pub title: Option<String>,
|
||
|
}
|
||
|
|
||
|
impl ErrorContext {
|
||
|
pub fn build() -> ErrorContext {
|
||
|
ErrorContext {
|
||
|
back: None,
|
||
|
flash_name: None,
|
||
|
flash_msg: None,
|
||
|
title: None,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[catch(404)]
|
||
|
pub fn not_found() -> Template {
|
||
|
debug!("404 Page Not Found");
|
||
|
let mut context = ErrorContext::build();
|
||
|
context.back = Some("/".to_string());
|
||
|
context.title = Some("404: Page Not Found".to_string());
|
||
|
context.flash_name = Some("error".to_string());
|
||
|
context.flash_msg = Some("No resource found for given URL".to_string());
|
||
|
|
||
|
Template::render("not_found", context)
|
||
|
}
|
||
|
|
||
|
// HELPERS AND ROUTES FOR 500 ERROR
|
||
|
|
||
|
#[catch(500)]
|
||
|
pub fn internal_error() -> Template {
|
||
|
debug!("500 Internal Server Error");
|
||
|
let mut context = ErrorContext::build();
|
||
|
context.back = Some("/".to_string());
|
||
|
context.title = Some("500: Internal Server Error".to_string());
|
||
|
context.flash_name = Some("error".to_string());
|
||
|
context.flash_msg = Some("Internal server error".to_string());
|
||
|
|
||
|
Template::render("internal_error", context)
|
||
|
}
|
||
|
|
||
|
// HELPERS AND ROUNTES FOR 403 FORBIDDEN
|
||
|
|
||
|
#[catch(403)]
|
||
|
pub fn forbidden() -> Redirect {
|
||
|
debug!("403 Forbidden");
|
||
|
Redirect::to("/login")
|
||
|
}
|