Plume/src/routes/errors.rs

41 lines
1.1 KiB
Rust
Raw Normal View History

2018-06-18 17:59:49 +02:00
use rocket_contrib::Template;
2018-06-21 11:58:54 +02:00
use rocket::Request;
use rocket::request::FromRequest;
2018-09-03 15:59:02 +02:00
use plume_models::db_conn::DbConn;
use plume_models::users::User;
2018-06-18 17:59:49 +02:00
#[catch(404)]
2018-06-21 11:58:54 +02:00
fn not_found(req: &Request) -> Template {
let conn = req.guard::<DbConn>().succeeded();
2018-06-21 11:58:54 +02:00
let user = User::from_request(req).succeeded();
2018-06-18 17:59:49 +02:00
Template::render("errors/404", json!({
2018-06-21 11:58:54 +02:00
"error_message": "Page not found",
"account": user.and_then(|u| conn.map(|conn| u.to_json(&*conn)))
2018-06-18 17:59:49 +02:00
}))
}
#[catch(500)]
2018-06-21 11:58:54 +02:00
fn server_error(req: &Request) -> Template {
let conn = req.guard::<DbConn>().succeeded();
2018-06-21 11:58:54 +02:00
let user = User::from_request(req).succeeded();
2018-06-18 17:59:49 +02:00
Template::render("errors/500", json!({
2018-06-21 11:58:54 +02:00
"error_message": "Server error",
"account": user.and_then(|u| conn.map(|conn| u.to_json(&*conn)))
2018-06-18 17:59:49 +02:00
}))
}
2018-06-24 18:58:57 +02:00
#[derive(FromForm)]
pub struct Uri {
target: String,
}
#[post("/csrf-violation?<uri>")]
fn csrf_violation(uri: Option<Uri>) -> Template {
if let Some(uri) = uri {
eprintln!("Csrf violation while acceding \"{}\"", uri.target)
}
Template::render("errors/csrf", json!({
"error_message":""
}))
}