Skip to content

Improve logging & cexpect BuildPage.metadata #976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/web/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) struct Build {

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct BuildsPage {
metadata: Option<MetaData>,
metadata: MetaData,
builds: Vec<Build>,
build_details: Option<Build>,
limits: Limits,
Expand Down Expand Up @@ -59,11 +59,11 @@ pub fn build_list_handler(req: &mut Request) -> IronResult<Response> {
builds.build_status,
builds.build_time,
builds.output
FROM builds
INNER JOIN releases ON releases.id = builds.rid
INNER JOIN crates ON releases.crate_id = crates.id
WHERE crates.name = $1 AND releases.version = $2
ORDER BY id DESC",
FROM builds
INNER JOIN releases ON releases.id = builds.rid
INNER JOIN crates ON releases.crate_id = crates.id
WHERE crates.name = $1 AND releases.version = $2
ORDER BY id DESC",
&[&name, &version]
)
);
Expand Down Expand Up @@ -111,7 +111,7 @@ pub fn build_list_handler(req: &mut Request) -> IronResult<Response> {
Ok(resp)
} else {
BuildsPage {
metadata: MetaData::from_crate(&mut conn, &name, &version),
metadata: cexpect!(req, MetaData::from_crate(&mut conn, &name, &version)),
builds,
build_details,
limits,
Expand Down
19 changes: 15 additions & 4 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ use log::{debug, info};

/// ctry! (cratesfyitry) is extremely similar to try! and itry!
/// except it returns an error page response instead of plain Err.
#[macro_export]
macro_rules! ctry {
($req:expr, $result:expr $(,)?) => {
match $result {
Ok(success) => success,
Err(error) => {
::log::error!("{}\n{:?}", error, ::backtrace::Backtrace::new());
let request: &::iron::Request = $req;

::log::error!(
"called ctry!() on an `Err` value: {}\nnote: while attempting to fetch the route {:?}\n{:?}",
error,
request.url,
::backtrace::Backtrace::new(),
);

// This is very ugly, but it makes it impossible to get a type inference error
// from this macro
Expand All @@ -23,7 +31,7 @@ macro_rules! ctry {
status: ::iron::status::BadRequest,
};

return $crate::web::page::WebPage::into_response(error, $req);
return $crate::web::page::WebPage::into_response(error, request);
}
}
};
Expand All @@ -36,8 +44,11 @@ macro_rules! cexpect {
match $option {
Some(success) => success,
None => {
let request: &::iron::Request = $req;

::log::error!(
"called cexpect!() on a `None` value\n{:?}",
"called cexpect!() on a `None` value while attempting to fetch the route {:?}\n{:?}",
request.url,
::backtrace::Backtrace::new(),
);

Expand All @@ -49,7 +60,7 @@ macro_rules! cexpect {
status: ::iron::status::BadRequest,
};

return $crate::web::page::WebPage::into_response(error, $req);
return $crate::web::page::WebPage::into_response(error, request);
}
}
};
Expand Down
19 changes: 11 additions & 8 deletions src/web/page/web_page.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::TemplateData;
use crate::ctry;
use iron::{headers::ContentType, response::Response, status::Status, IronResult, Request};
use serde::Serialize;
use std::borrow::Cow;
Expand All @@ -10,6 +11,7 @@ macro_rules! impl_webpage {
($page:ty = $template:literal $(, status = $status:expr)? $(, content_type = $content_type:expr)? $(,)?) => {
impl_webpage!($page = |_| ::std::borrow::Cow::Borrowed($template) $(, status = $status)? $(, content_type = $content_type)?);
};

($page:ty = $template:expr $(, status = $status:expr)? $(, content_type = $content_type:expr)? $(,)?) => {
impl $crate::web::page::WebPage for $page {
fn template(&self) -> ::std::borrow::Cow<'static, str> {
Expand Down Expand Up @@ -39,14 +41,15 @@ pub trait WebPage: Serialize + Sized {
// TODO: We could cache similar pages using the `&Context`
fn into_response(self, req: &Request) -> IronResult<Response> {
let ctx = Context::from_serialize(&self).unwrap();
let rendered = req
.extensions
.get::<TemplateData>()
.expect("missing TemplateData from the request extensions")
.templates
.load()
.render(&self.template(), &ctx)
.unwrap();
let rendered = ctry!(
req,
req.extensions
.get::<TemplateData>()
.expect("missing TemplateData from the request extensions")
.templates
.load()
.render(&self.template(), &ctx)
);

let mut response = Response::with((self.get_status(), rendered));
response.headers.set(Self::content_type());
Expand Down