Skip to content
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

Improve error message of MissingExtension rejections #72

Merged
merged 2 commits into from
Aug 1, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,12 @@ where
.extensions()
.ok_or(ExtensionsAlreadyExtracted)?
.get::<T>()
.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::<T>()
))
})
.map(|x| x.clone())?;

Ok(Extension(value))
Expand Down
2 changes: 1 addition & 1 deletion src/extract/rejection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down