Description
- I have looked for existing issues (including closed) about this
Bug Report
Version
├── axum v0.6.20
│ ├── axum-core v0.3.4
│ ├── axum v0.6.20 (*)
(but I checked the source code for the latest version on docs.rs to notice this)
Crates
Description
I'm looking at the code in async fn hello()
below.
pub async fn serve(bind: SocketAddr) -> eyre::Result<()> {
let router = Router::new()
.route("/", get(hello));
info!("Listening on {bind:?}");
axum::Server::try_bind(&bind)
.context("could not bind listen address")?
.serve(router.into_make_service_with_connect_info::<SocketAddr>())
.await?;
Ok(())
}
async fn hello() -> impl IntoResponse {
let mut map = HashMap::new();
map.insert((1, 2), 3);
(StatusCode::IM_A_TEAPOT, Json(map))
}
The general idea is: we're emitting a JSON payload with a custom status code.
But along the lines, something has gone wrong and the JSON serialisation fails (in this case: because the map has keys which are not strings and are not coerced into strings by serde_json
).
Had I just used Json(map)
as the return value, then I'd get a 500 Internal Server Error response — very reasonable!
However, instead I am getting the originally desired status code but with a text error message:
$ curl -i http://127.0.0.1:8080
HTTP/1.1 418 I'm a teapot
content-type: text/plain; charset=utf-8
content-length: 20
date: Tue, 24 Oct 2023 21:35:49 GMT
key must be a string
I find this a little bit unsettling and like it might turn out to be a bit of a footgun.
It's frankly not very likely with emitting Json (but not impossible) but when writing a custom IntoResponse
to render a template or something like that, it seems quite reasonable to anticipate errors!
On the other hand, I don't know how/if you can fix this with the current API surface. But it felt like it was worth documenting.
(I should note this is entirely academic only and hasn't caused me any trouble in a real project or anything, so of course don't waste too much time on me!)