From 866ca84e67f555b79807e63c830c9b7dfdbd672f Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Thu, 14 Feb 2019 15:51:53 -0600 Subject: [PATCH] log the URL that caused a 404 --- src/web/mod.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/web/mod.rs b/src/web/mod.rs index 50e22e95c..8de4021c3 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -45,12 +45,12 @@ mod builds; mod error; mod sitemap; -use std::env; +use std::{env, fmt}; use std::error::Error; use std::time::Duration; use std::path::PathBuf; use iron::prelude::*; -use iron::{Handler, status}; +use iron::{self, Handler, status}; use iron::headers::{CacheControl, CacheDirective, ContentType}; use router::{Router, NoRoute}; use staticfile::Static; @@ -237,6 +237,34 @@ impl Handler for CratesfyiHandler { } else { panic!("all cratesfyi errors should be of type Nope"); }; + + match err { + error::Nope::ResourceNotFound => { + // print the path of the URL that triggered a 404 error + struct DebugPath<'a>(&'a iron::Url); + impl<'a> fmt::Display for DebugPath<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + for path_elem in self.0.path() { + write!(f, "/{}", path_elem)?; + } + + if let Some(query) = self.0.query() { + write!(f, "?{}", query)?; + } + + if let Some(hash) = self.0.fragment() { + write!(f, "#{}", hash)?; + } + + Ok(()) + } + } + + debug!("Path: {}", DebugPath(&req.url)); + } + _ => {} + } + Self::chain(err).handle(req) }) }