2021-11-03 09:52:02 +00:00
|
|
|
use log::debug;
|
2021-11-15 15:32:00 +00:00
|
|
|
use rocket::catch;
|
2021-11-08 14:00:27 +00:00
|
|
|
use rocket::response::Redirect;
|
2021-11-15 15:32:00 +00:00
|
|
|
use rocket_dyn_templates::Template;
|
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());
|
|
|
|
|
2021-11-15 15:32:00 +00:00
|
|
|
Template::render("catchers/not_found", context)
|
2021-10-28 08:01:31 +00:00
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
|
2021-11-15 15:32:00 +00:00
|
|
|
Template::render("catchers/internal_error", context)
|
2021-10-28 08:01:31 +00:00
|
|
|
}
|
2021-11-08 14:00:27 +00:00
|
|
|
|
2021-11-08 15:54:18 +00:00
|
|
|
// HELPERS AND ROUTES FOR 403 FORBIDDEN
|
2021-11-08 14:00:27 +00:00
|
|
|
|
|
|
|
#[catch(403)]
|
|
|
|
pub fn forbidden() -> Redirect {
|
|
|
|
debug!("403 Forbidden");
|
|
|
|
Redirect::to("/login")
|
2021-11-15 15:32:00 +00:00
|
|
|
}
|