Getting helpful error message from Json
with_rejection
#1610
Replies: 3 comments 8 replies
-
Calling |
Beta Was this translation helpful? Give feedback.
-
I would recommend copy-pasting our |
Beta Was this translation helpful? Give feedback.
-
@jplatte or @davidpdrsn Do we have an example for this use case, please ❔ I'll still search for it.
Update: |
Beta Was this translation helpful? Give feedback.
-
Hi all,
If I give an invalid JSON input in a request body, I get the following error in the response body:
This is a super helpful error as it tells you exactly where you are going wrong.
I however want to return this error as JSON in my API.
I've followed a similar pattern to the with_rejection example here: https://github.com/tokio-rs/axum/blob/main/examples/customize-extractor-error/src/with_rejection.rs and am now up to a point where I have a JsonRejection. The type here of JsonRejection here is a JsonDataError. If I return something (in json) like
e.to_string()
, wheree
is JsonDataError the message isFailed to deserialize the JSON body into the target type
If I log out
e
as Debug, I get aJsonDataError(Error { inner: Error { path: Path { segments: [Map { key: "accountProvider" }] }, original: Error("unknown variant
STRIPEa, expected
STRIPE", line: 2, column: 30) } })
My question is - how do I get the text from the
original
section of that debug output? I can downcast as axum::Error which takes me one step closer, but I am not seeing how to get the actual text I want out of it.--
My current thoughts is to use a custom extractor ( https://github.com/tokio-rs/axum/blob/main/examples/customize-extractor-error/src/custom_extractor.rs ) instead and do the serde derserialization myself, but I would like to avoid that if possible and let axum do most of the work for me seeing as it is already implemented.
Beta Was this translation helpful? Give feedback.
All reactions