-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add ErrorKind::DeserializeError to specialize ErrorKind::Message (extract::path::ErrorKind
)
#2720
base: main
Are you sure you want to change the base?
Conversation
0bf2e30
to
ffdd81a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, just a few minor points.
axum/src/extract/path/mod.rs
Outdated
key, | ||
value, | ||
message, | ||
} => write!(f, "Cannot parse `{key}` with value `{value:?}`: {message}"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use debug for value
, it would add redundant quotes. You can also that in the new tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just mirroring the practice established by the other formats for the other variants:
axum/axum/src/extract/path/mod.rs
Lines 330 to 349 in e3bb708
ErrorKind::ParseErrorAtKey { | |
key, | |
value, | |
expected_type, | |
} => write!( | |
f, | |
"Cannot parse `{key}` with value `{value:?}` to a `{expected_type}`" | |
), | |
ErrorKind::ParseError { | |
value, | |
expected_type, | |
} => write!(f, "Cannot parse `{value:?}` to a `{expected_type}`"), | |
ErrorKind::ParseErrorAtIndex { | |
index, | |
value, | |
expected_type, | |
} => write!( | |
f, | |
"Cannot parse value at index {index} with value `{value:?}` to a `{expected_type}`" | |
), |
I was a bit perplexed about this as well, and thought it did not make sense to use debug format for value
.
Should I change it do non-Debug for this new variant only, or change the other variants as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added an additional commit that changes the format for all variants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Sorry I didn't notice you just wrote it consistently with the rest, otherwise I wouldn't bring it up here.
85e31f3
to
53e8e14
Compare
LGTM, @jplatte can you take a look when you have some free time? |
53e8e14
to
f7c94df
Compare
Rebased against latest main for an up-to-date PR. |
@mladedav anything holding up this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was holding it up by not reviewing, sorry. I did so now, and the change looks good to me.
One question, and a request though:
- Is
ErrorKind::Message
still constructed anywhere? If not, would make sense to remove it, I wouldn't want to release this in a patch of 0.7 anyways. - Could you please add a changelog entry?
f7c94df
to
245bceb
Compare
It is indirectly constructed through the
Done! Added it under the "# Unreleased" section |
245bceb
to
e1e10ad
Compare
…rorKind::Message This commit introduces another `extract::path::ErrorKind` variant that captures the serde error nominally captured through the `serde::de::Error` trait impl on `PathDeserializeError`. We augment the deserialization error with the captured (key, value), allowing `extract::Path`, and wrapping extractors, to gain programmatic access to the key name, and attempted deserialized value. The `PathDeserializationError::custom` is used two places in addition to capture the deserialization error. These usages should still be unaffected.
e1e10ad
to
0e4c62f
Compare
I have a feeling that the failing CI job is unrelated to this PR. |
Yeah, it seems unrelated. I don't know the public api check but when I get some time, I'll try to figure out what's happening and then we'll merge this. Other branches seem to have also failed, it's possible one of our dependencies we're exposing started exposing another dependency we didn't mean to expose. Sorry the whole thing takes so long. |
Motivation
Expose the
key
andvalue
of theextract::Path
extrator for when the serde deserialization fails. This allow custom extractors wrapping thePath
extractor to specialize their responses with more context about the failed operation. Currently, all failed deserializations end up as part of theextract::path::ErrorKind::Message
variant.I encountered this situation when attempting to create my own extrator, using it to deserialize
uuid::Uuid
resources, since I require specializing the response for these error conditions.Solution
This commit introduces another
extract::path::ErrorKind
variant that captures the serde error nominally captured through theserde::de::Error
trait impl onPathDeserializeError
. We augment the deserialization error with the captured (key, value), allowingextract::Path
, and wrapping extractors, to gain programmatic access to the key name, and attempted deserialized value.This allows me to expand the handling in the wrapping Path extractor.