peach-workspace/peach-web/src/routes/catchers.rs

60 lines
1.5 KiB
Rust
Raw Normal View History

2021-11-03 09:52:02 +00:00
use log::debug;
2021-11-04 21:30:29 +00:00
use rocket::{catch};
2021-11-04 11:03:40 +00:00
use rocket_dyn_templates::Template;
use rocket::response::Redirect;
2021-10-28 08:01:31 +00:00
use serde::Serialize;
2021-11-03 09:52:02 +00:00
// HELPERS AND ROUTES FOR 404 ERROR
2021-10-28 08:01:31 +00:00
#[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)
}
2021-11-03 09:52:02 +00:00
// HELPERS AND ROUTES FOR 500 ERROR
2021-10-28 08:01:31 +00:00
#[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")
}