Plume/src/routes/errors.rs

97 lines
2.5 KiB
Rust
Raw Normal View History

2019-03-20 17:56:17 +01:00
use plume_models::users::User;
use plume_models::{db_conn::DbConn, Error};
use rocket::{
request::FromRequest,
response::{self, Responder},
2019-03-20 17:56:17 +01:00
Request,
};
use rocket_i18n::I18n;
use template_utils::Ructe;
2018-06-18 17:59:49 +02:00
#[derive(Debug)]
pub struct ErrorPage(Error);
impl From<Error> for ErrorPage {
fn from(err: Error) -> ErrorPage {
ErrorPage(err)
}
}
impl<'r> Responder<'r> for ErrorPage {
fn respond_to(self, req: &Request) -> response::Result<'r> {
let conn = req.guard::<DbConn>().succeeded();
let intl = req.guard::<I18n>().succeeded();
let user = User::from_request(req).succeeded();
match self.0 {
2019-03-20 17:56:17 +01:00
Error::NotFound => render!(errors::not_found(&(
&*conn.unwrap(),
&intl.unwrap().catalog,
user
)))
.respond_to(req),
Error::Unauthorized => render!(errors::not_found(&(
&*conn.unwrap(),
&intl.unwrap().catalog,
user
)))
.respond_to(req),
_ => render!(errors::not_found(&(
&*conn.unwrap(),
&intl.unwrap().catalog,
user
)))
.respond_to(req),
}
}
}
2018-06-18 17:59:49 +02:00
#[catch(404)]
pub fn not_found(req: &Request) -> Ructe {
let conn = req.guard::<DbConn>().succeeded();
let intl = req.guard::<I18n>().succeeded();
2018-06-21 11:58:54 +02:00
let user = User::from_request(req).succeeded();
2019-03-20 17:56:17 +01:00
render!(errors::not_found(&(
&*conn.unwrap(),
&intl.unwrap().catalog,
user
)))
2018-06-18 17:59:49 +02:00
}
#[catch(422)]
pub fn unprocessable_entity(req: &Request) -> Ructe {
let conn = req.guard::<DbConn>().succeeded();
let intl = req.guard::<I18n>().succeeded();
let user = User::from_request(req).succeeded();
2019-03-20 17:56:17 +01:00
render!(errors::unprocessable_entity(&(
&*conn.unwrap(),
&intl.unwrap().catalog,
user
)))
}
2018-06-18 17:59:49 +02:00
#[catch(500)]
pub fn server_error(req: &Request) -> Ructe {
let conn = req.guard::<DbConn>().succeeded();
let intl = req.guard::<I18n>().succeeded();
2018-06-21 11:58:54 +02:00
let user = User::from_request(req).succeeded();
2019-03-20 17:56:17 +01:00
render!(errors::server_error(&(
&*conn.unwrap(),
&intl.unwrap().catalog,
user
)))
2018-06-18 17:59:49 +02:00
}
2018-06-24 18:58:57 +02:00
#[post("/csrf-violation?<target>")]
2019-03-20 17:56:17 +01:00
pub fn csrf_violation(
target: Option<String>,
conn: DbConn,
intl: I18n,
user: Option<User>,
) -> Ructe {
if let Some(uri) = target {
eprintln!("Csrf violation while acceding \"{}\"", uri)
2018-06-24 18:58:57 +02:00
}
2019-03-20 17:56:17 +01:00
render!(errors::csrf(&(&*conn, &intl.catalog, user)))
2018-06-24 18:58:57 +02:00
}