axum-impl-middleware

Fix Axum middleware composition errors with correct from_fn argument ordering and route_layer usage.

Updated May 20, 2026
One-click install
npx skills add https://github.com/Impertio-Studio/Axum-Claude-Skill-Package --skill axum-impl-middleware
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: axum-impl-middleware
Source: https://github.com/Impertio-Studio/Axum-Claude-Skill-Package/tree/main/skills/source/axum-impl/axum-impl-middleware
Command: npx skills add https://github.com/Impertio-Studio/Axum-Claude-Skill-Package --skill axum-impl-middleware

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill prevents common Axum/Tower middleware wiring mistakes that either fail to compile or silently skip your handler logic. It also avoids subtle routing bugs where an auth gate incorrectly converts missing routes into unauthorized responses.

Core Features & Use Cases

  • Correct middleware helper selection: choose map_request/map_response for one-way transforms and from_fn/from_fn_with_state when you need request+response handling or short-circuiting.
  • Guaranteed from_fn argument ordering: enforces the required shape of [FromRequestParts..., FromRequest, Next] to avoid opaque Handler is not satisfied trait-bound errors.
  • Safe control flow with Next: ensures middleware either calls next.run(request).await or deliberately short-circuits with an IntoResponse, avoiding “handler never runs” incidents.
  • Correct .layer() vs .route_layer() semantics: uses .route_layer() for auth/authz so unmatched paths still return real 404 instead of being masked as 401/403.
  • Layer stacking without reversal confusion: clarifies how Router’s chained .layer() order differs from tower::ServiceBuilder, preventing request/response execution order surprises.

Quick Start

Add middleware to your Axum routes by asking for a from_fn or from_fn_with_state example that uses the correct argument order, short-circuit behavior, and .route_layer() for auth gates on Axum 0.7/0.8.

Frequently Asked Questions about axum-impl-middleware

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why does my Axum middleware return 401 instead of 404 for unmatched routes?

Using `.layer()` for an auth gate masks unmatched routes, returning 401/403 instead of 404. Apply auth middleware with `.route_layer()` so unmatched paths bypass the authorization logic and correctly return 404.

How do I fix the `Handler is not satisfied` trait bound error in Axum `from_fn` middleware?

The `from_fn` middleware function must follow the exact argument ordering: place all `FromRequestParts` extractors first, then one `FromRequest` extractor, and finally `Next` as the last argument to satisfy Axum's handler traits.

What's the best way to structure Axum middleware execution order without reversing layers?

Use `tower::ServiceBuilder` to stack layers clearly, as Router's chained `.layer()` method reverses execution order relative to request flow, preventing surprising request and response execution order in middleware composition.

Should I use `map_request` or `from_fn` for Axum middleware that needs to short-circuit?

Use `from_fn` or `from_fn_with_state` when your middleware needs to short-circuit by returning an `IntoResponse` early. `map_request` is only suited for one-way request transformations without response handling.

Does this Axum middleware approach work with state-backed extractors on version 0.8?

Yes, the middleware patterns apply across Axum 0.7 and 0.8. Use `from_fn_with_state` to pass shared state into your middleware, ensuring state-backed extractors retrieve context correctly during request processing.