Avoid panics (#392)
- Use `Result` as much as possible - Display errors instead of panicking TODO (maybe in another PR? this one is already quite big): - Find a way to merge Ructe/ErrorPage types, so that we can have routes returning `Result<X, ErrorPage>` instead of panicking when we have an `Error` - Display more details about the error, to make it easier to debug (sorry, this isn't going to be fun to review, the diff is huge, but it is always the same changes)
This commit is contained in:
+35
-3
@@ -1,10 +1,42 @@
|
||||
use rocket::Request;
|
||||
use rocket::request::FromRequest;
|
||||
use rocket::{
|
||||
Request,
|
||||
request::FromRequest,
|
||||
response::{self, Responder},
|
||||
};
|
||||
use rocket_i18n::I18n;
|
||||
use plume_models::db_conn::DbConn;
|
||||
use plume_models::{Error, db_conn::DbConn};
|
||||
use plume_models::users::User;
|
||||
use template_utils::Ructe;
|
||||
|
||||
#[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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[catch(404)]
|
||||
pub fn not_found(req: &Request) -> Ructe {
|
||||
let conn = req.guard::<DbConn>().succeeded();
|
||||
|
||||
Reference in New Issue
Block a user