From 2cf28c6794aaf3ad3e9a028fc1ba73bf13c42e3b Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 1 Aug 2021 15:50:57 +0200 Subject: [PATCH] Improve error message of `MissingExtension` rejections (#72) Now includes the name of missing type. --- CHANGELOG.md | 1 + src/extract/mod.rs | 7 ++++++- src/extract/rejection.rs | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92a40d68..ca71a176 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Implement `Deref` most extractors ([#56](https://github.com/tokio-rs/axum/pull/56)) - Return `405 Method Not Allowed` for unsupported method for route ([#63](https://github.com/tokio-rs/axum/pull/63)) - Add extractor for remote connection info ([#55](https://github.com/tokio-rs/axum/pull/55)) +- Improve error message of `MissingExtension` rejections ([#72](https://github.com/tokio-rs/axum/pull/72)) - Improve documentation for routing ([#71](https://github.com/tokio-rs/axum/pull/71)) - Clarify required response body type when routing to `tower::Service`s ([#69](https://github.com/tokio-rs/axum/pull/69)) - Add `axum::body::box_body` to converting an `http_body::Body` to `axum::body::BoxBody` ([#69](https://github.com/tokio-rs/axum/pull/69)) diff --git a/src/extract/mod.rs b/src/extract/mod.rs index 6d59636b..0f286f2c 100644 --- a/src/extract/mod.rs +++ b/src/extract/mod.rs @@ -831,7 +831,12 @@ where .extensions() .ok_or(ExtensionsAlreadyExtracted)? .get::() - .ok_or(MissingExtension) + .ok_or_else(|| { + MissingExtension::from_err(format!( + "Extension of type `{}` was not found. Perhaps you forgot to add it?", + std::any::type_name::() + )) + }) .map(|x| x.clone())?; Ok(Extension(value)) diff --git a/src/extract/rejection.rs b/src/extract/rejection.rs index 2668616c..bfa72a95 100644 --- a/src/extract/rejection.rs +++ b/src/extract/rejection.rs @@ -66,7 +66,7 @@ define_rejection! { #[body = "Missing request extension"] /// Rejection type for [`Extension`](super::Extension) if an expected /// request extension was not found. - pub struct MissingExtension; + pub struct MissingExtension(BoxError); } define_rejection! {