Skip to content

Commit cee3f82

Browse files
authored
Merge pull request #300 from QuietMisdreavus/404-logging
log the URL that caused a 404
2 parents 94aa716 + 866ca84 commit cee3f82

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/web/mod.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ mod builds;
4545
mod error;
4646
mod sitemap;
4747

48-
use std::env;
48+
use std::{env, fmt};
4949
use std::error::Error;
5050
use std::time::Duration;
5151
use std::path::PathBuf;
5252
use iron::prelude::*;
53-
use iron::{Handler, status};
53+
use iron::{self, Handler, status};
5454
use iron::headers::{CacheControl, CacheDirective, ContentType};
5555
use router::{Router, NoRoute};
5656
use staticfile::Static;
@@ -237,6 +237,34 @@ impl Handler for CratesfyiHandler {
237237
} else {
238238
panic!("all cratesfyi errors should be of type Nope");
239239
};
240+
241+
match err {
242+
error::Nope::ResourceNotFound => {
243+
// print the path of the URL that triggered a 404 error
244+
struct DebugPath<'a>(&'a iron::Url);
245+
impl<'a> fmt::Display for DebugPath<'a> {
246+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
247+
for path_elem in self.0.path() {
248+
write!(f, "/{}", path_elem)?;
249+
}
250+
251+
if let Some(query) = self.0.query() {
252+
write!(f, "?{}", query)?;
253+
}
254+
255+
if let Some(hash) = self.0.fragment() {
256+
write!(f, "#{}", hash)?;
257+
}
258+
259+
Ok(())
260+
}
261+
}
262+
263+
debug!("Path: {}", DebugPath(&req.url));
264+
}
265+
_ => {}
266+
}
267+
240268
Self::chain(err).handle(req)
241269
})
242270
}

0 commit comments

Comments
 (0)