Skip to content
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

Improve serde error output #982

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2621,7 +2621,7 @@
where
D: serde::Deserializer<'de>,
{
use serde::de::{Deserialize, Error, Unexpected};
use serde::de::{Deserialize, Error};
let (
serialization,
scheme_end,
Expand All @@ -2647,10 +2647,8 @@
fragment_start,
};
if cfg!(debug_assertions) {
url.check_invariants().map_err(|reason| {
let reason: &str = &reason;
Error::invalid_value(Unexpected::Other("value"), &reason)
})?
url.check_invariants()
.map_err(|reason| Error::custom(reason))?

Check warning on line 2651 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L2650-L2651

Added lines #L2650 - L2651 were not covered by tests
}
Ok(url)
}
Expand Down Expand Up @@ -2857,7 +2855,7 @@
where
D: serde::Deserializer<'de>,
{
use serde::de::{Error, Unexpected, Visitor};
use serde::de::{Error, Visitor};

struct UrlVisitor;

Expand All @@ -2872,10 +2870,7 @@
where
E: Error,
{
Url::parse(s).map_err(|err| {
let err_s = format!("{}", err);
Error::invalid_value(Unexpected::Str(s), &err_s.as_str())
})
Url::parse(s).map_err(|err| Error::custom(format!("{}: {:?}", err, s)))

Check warning on line 2873 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L2873

Added line #L2873 was not covered by tests
}
}

Expand Down
17 changes: 17 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1362,3 +1362,20 @@ fn issue_974() {
let _ = url::quirks::set_port(&mut url, "\u{0000}9000");
assert_eq!(url.port(), Some(8000));
}

#[cfg(feature = "serde")]
#[test]
fn serde_error_message() {
use serde::Deserialize;
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct TypeWithUrl {
url: Url,
}

let err = serde_json::from_str::<TypeWithUrl>(r#"{"url": "§invalid#+#*Ä"}"#).unwrap_err();
assert_eq!(
err.to_string(),
r#"relative URL without a base: "§invalid#+#*Ä" at line 1 column 25"#
);
}
Loading