Plume/src/routes/errors.rs

65 lines
2.0 KiB
Rust
Raw Normal View History

2020-01-21 07:02:03 +01:00
use crate::template_utils::{IntoContext, Ructe};
2021-01-30 13:44:29 +01:00
use plume_models::{db_conn::DbConn, Error, PlumeRocket};
use rocket::{
response::{self, Responder},
2019-03-20 17:56:17 +01:00
Request,
};
2021-01-11 00:38:41 +01:00
use tracing::warn;
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 {
2020-01-21 07:02:03 +01:00
fn respond_to(self, req: &Request<'_>) -> response::Result<'r> {
2021-01-30 13:44:29 +01:00
let conn = req.guard::<DbConn>().unwrap();
let rockets = req.guard::<PlumeRocket>().unwrap();
match self.0 {
2021-01-30 13:44:29 +01:00
Error::NotFound => {
render!(errors::not_found(&(&conn, &rockets).to_context())).respond_to(req)
}
Error::Unauthorized => {
2021-01-30 13:44:29 +01:00
render!(errors::not_found(&(&conn, &rockets).to_context())).respond_to(req)
}
2021-01-30 13:44:29 +01:00
_ => render!(errors::not_found(&(&conn, &rockets).to_context())).respond_to(req),
}
}
}
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 {
2021-01-30 13:44:29 +01:00
let conn = req.guard::<DbConn>().unwrap();
let rockets = req.guard::<PlumeRocket>().unwrap();
2021-01-30 13:44:29 +01:00
render!(errors::not_found(&(&conn, &rockets).to_context()))
2018-06-18 17:59:49 +02:00
}
#[catch(422)]
2020-01-21 07:02:03 +01:00
pub fn unprocessable_entity(req: &Request<'_>) -> Ructe {
2021-01-30 13:44:29 +01:00
let conn = req.guard::<DbConn>().unwrap();
let rockets = req.guard::<PlumeRocket>().unwrap();
2021-01-30 13:44:29 +01:00
render!(errors::unprocessable_entity(
&(&conn, &rockets).to_context()
))
}
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 {
2021-01-30 13:44:29 +01:00
let conn = req.guard::<DbConn>().unwrap();
let rockets = req.guard::<PlumeRocket>().unwrap();
2021-01-30 13:44:29 +01:00
render!(errors::server_error(&(&conn, &rockets).to_context()))
2018-06-18 17:59:49 +02:00
}
2018-06-24 18:58:57 +02:00
#[post("/csrf-violation?<target>")]
2021-01-30 13:44:29 +01:00
pub fn csrf_violation(target: Option<String>, conn: DbConn, rockets: PlumeRocket) -> Ructe {
if let Some(uri) = target {
warn!("Csrf violation while accessing \"{}\"", uri)
2018-06-24 18:58:57 +02:00
}
2021-01-30 13:44:29 +01:00
render!(errors::csrf(&(&conn, &rockets).to_context()))
2018-06-24 18:58:57 +02:00
}