Replies: 2 comments
-
|
The second point seems like another case of a failing extractor and I don't understand the third one. I believe we currently just send either a 400 or 500 with information about which extractor failed, what would you like axum to send by default? Or am I mistaken and we actually send more information? There's |
Beta Was this translation helpful? Give feedback.
-
|
I dug through axum's source to put together the complete list. Extractor rejectionsEvery built-in extractor exposes detail in its default rejection body before your handler runs. Rejection types that wrap an inner error append it after
The To remap any extractor's rejection to your own error type, use use axum_extra::extract::WithRejection;
async fn handler(
WithRejection(Json(payload), _): WithRejection<Json<MyPayload>, MyError>,
) -> impl IntoResponse {
// ...
}Method not allowed (405)A 405 response includes an let app = Router::new()
.route("/users", get(get_users).post(create_user))
.method_not_allowed_fallback(|| async {
(StatusCode::METHOD_NOT_ALLOWED, Json(json!({
"error": "method_not_allowed"
})))
});Axum only injects the Not found (404)The default 404 handler returns an empty body. To return structured errors, use let app = Router::new()
.route("/users", get(get_users))
.fallback(|| async {
(StatusCode::NOT_FOUND, Json(json!({"error": "not_found"})))
});Body too large (413)The default body limit is 2 MB. When exceeded, axum returns a 413 with body: Configure the limit per-route with use axum::extract::DefaultBodyLimit;
let app = Router::new()
.route("/upload", post(upload))
.layer(DefaultBodyLimit::max(50 * 1024 * 1024));PanicsWithout use tower_http::catch_panic::CatchPanicLayer;
let app = Router::new()
.route("/", get(handler))
.layer(CatchPanicLayer::new());The default response is a 500 with an empty body. Middleware short-circuitsAny Tower middleware layer can return a response before your handler runs. The most common ones:
These bypass your error handling layer entirely unless your middleware explicitly routes through it. What to fix first
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I am finalizing my app and want to get rid of all implementation detail leaks. It would be really nice to have a list. Some that I have ran into:
Body<String>containing non utf8 dataGETon apost(handler)routeIdeally axum should not expose application internal details such as errors by default or have a way to turn the behaviour off
axum version
0.8.8
Beta Was this translation helpful? Give feedback.
All reactions