Skip to content

Commit 5dfb429

Browse files
committed
Warn when sending bare 404 response
Signed-off-by: itowlson <[email protected]>
1 parent 0013bd9 commit 5dfb429

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

crates/trigger-http/src/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod wagi;
66

77
use std::{
88
collections::HashMap,
9+
io::IsTerminal,
910
net::{Ipv4Addr, SocketAddr, ToSocketAddrs},
1011
path::PathBuf,
1112
str::FromStr,
@@ -246,7 +247,7 @@ impl HttpTrigger {
246247
return match well_known {
247248
"health" => Ok(Response::new(body::full(Bytes::from_static(b"OK")))),
248249
"info" => self.app_info(),
249-
_ => Self::not_found(),
250+
_ => Self::not_found(NotFoundRouteKind::WellKnown),
250251
};
251252
}
252253

@@ -294,7 +295,7 @@ impl HttpTrigger {
294295
}
295296
}
296297
}
297-
Err(_) => Self::not_found(),
298+
Err(_) => Self::not_found(NotFoundRouteKind::Normal(path.to_string())),
298299
}
299300
}
300301

@@ -320,7 +321,16 @@ impl HttpTrigger {
320321
}
321322

322323
/// Creates an HTTP 404 response.
323-
fn not_found() -> Result<Response<Body>> {
324+
fn not_found(kind: NotFoundRouteKind) -> Result<Response<Body>> {
325+
use std::sync::atomic::{AtomicBool, Ordering};
326+
static SHOWN_GENERIC_404_WARNING: AtomicBool = AtomicBool::new(false);
327+
if let NotFoundRouteKind::Normal(route) = kind {
328+
if !SHOWN_GENERIC_404_WARNING.fetch_or(true, Ordering::Relaxed)
329+
&& std::io::stderr().is_terminal()
330+
{
331+
terminal::warn!("Request to {route} matched no pattern, and received a generic 404 response. To serve a more informative 404 page, add a catch-all (/...) route.");
332+
}
333+
}
324334
Ok(Response::builder()
325335
.status(StatusCode::NOT_FOUND)
326336
.body(body::empty())?)
@@ -680,6 +690,12 @@ impl OutboundWasiHttpHandler for HttpRuntimeData {
680690
}
681691
}
682692

693+
#[derive(Debug, PartialEq)]
694+
enum NotFoundRouteKind {
695+
Normal(String),
696+
WellKnown,
697+
}
698+
683699
#[cfg(test)]
684700
mod tests {
685701
use anyhow::Result;

0 commit comments

Comments
 (0)