2020-01-21 07:02:03 +01:00
|
|
|
use crate::template_utils::{IntoContext, Ructe};
|
2019-04-30 12:04:25 +02:00
|
|
|
use plume_models::{Error, PlumeRocket};
|
2018-12-29 09:36:07 +01:00
|
|
|
use rocket::{
|
|
|
|
response::{self, Responder},
|
2019-03-20 17:56:17 +01:00
|
|
|
Request,
|
2018-12-29 09:36:07 +01:00
|
|
|
};
|
2018-06-18 17:59:49 +02:00
|
|
|
|
2018-12-29 09:36:07 +01: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 {
|
2020-01-21 07:02:03 +01:00
|
|
|
fn respond_to(self, req: &Request<'_>) -> response::Result<'r> {
|
2019-04-30 12:04:25 +02:00
|
|
|
let rockets = req.guard::<PlumeRocket>().unwrap();
|
2018-12-29 09:36:07 +01:00
|
|
|
|
|
|
|
match self.0 {
|
2019-04-30 12:04:25 +02:00
|
|
|
Error::NotFound => render!(errors::not_found(&rockets.to_context())).respond_to(req),
|
|
|
|
Error::Unauthorized => {
|
|
|
|
render!(errors::not_found(&rockets.to_context())).respond_to(req)
|
|
|
|
}
|
|
|
|
_ => render!(errors::not_found(&rockets.to_context())).respond_to(req),
|
2018-12-29 09:36:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:59:49 +02:00
|
|
|
#[catch(404)]
|
2020-01-21 07:02:03 +01:00
|
|
|
pub fn not_found(req: &Request<'_>) -> Ructe {
|
2019-04-30 12:04:25 +02:00
|
|
|
let rockets = req.guard::<PlumeRocket>().unwrap();
|
|
|
|
render!(errors::not_found(&rockets.to_context()))
|
2018-06-18 17:59:49 +02:00
|
|
|
}
|
|
|
|
|
2019-01-27 10:55:22 +01:00
|
|
|
#[catch(422)]
|
2020-01-21 07:02:03 +01:00
|
|
|
pub fn unprocessable_entity(req: &Request<'_>) -> Ructe {
|
2019-04-30 12:04:25 +02:00
|
|
|
let rockets = req.guard::<PlumeRocket>().unwrap();
|
|
|
|
render!(errors::unprocessable_entity(&rockets.to_context()))
|
2019-01-27 10:55:22 +01:00
|
|
|
}
|
|
|
|
|
2018-06-18 17:59:49 +02:00
|
|
|
#[catch(500)]
|
2020-01-21 07:02:03 +01:00
|
|
|
pub fn server_error(req: &Request<'_>) -> Ructe {
|
2019-04-30 12:04:25 +02:00
|
|
|
let rockets = req.guard::<PlumeRocket>().unwrap();
|
|
|
|
render!(errors::server_error(&rockets.to_context()))
|
2018-06-18 17:59:49 +02:00
|
|
|
}
|
2018-06-24 18:58:57 +02:00
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[post("/csrf-violation?<target>")]
|
2019-04-30 12:04:25 +02:00
|
|
|
pub fn csrf_violation(target: Option<String>, rockets: PlumeRocket) -> Ructe {
|
2018-12-06 18:54:16 +01:00
|
|
|
if let Some(uri) = target {
|
2021-01-05 12:52:48 +01:00
|
|
|
eprintln!("Csrf violation while accessing \"{}\"", uri)
|
2018-06-24 18:58:57 +02:00
|
|
|
}
|
2019-04-30 12:04:25 +02:00
|
|
|
render!(errors::csrf(&rockets.to_context()))
|
2018-06-24 18:58:57 +02:00
|
|
|
}
|