Improve error reporting for missing values

This commit is contained in:
Dirkjan Ochtman 2022-11-26 12:52:57 -08:00
parent f6a9d66288
commit 69eca9c2a5
5 changed files with 9 additions and 7 deletions

View File

@ -63,7 +63,7 @@ fn deserialize_scalar_enum(
let value = match deserializer.take_str() {
#variants
_ => return Err(Error::UnexpectedValue),
_ => return Err(Error::UnexpectedValue("enum variant not found")),
};
*into = Some(value);

View File

@ -25,7 +25,7 @@ impl<'xml, T: FromStr> FromXml<'xml> for FromXmlStr<T> {
*into = Some(FromXmlStr(value));
Ok(())
}
Err(_) => Err(Error::UnexpectedValue),
Err(_) => Err(Error::UnexpectedValue("unable to parse value")),
}
}
@ -205,7 +205,9 @@ impl<'xml> FromXml<'xml> for &'xml str {
*into = Some(s);
Ok(())
}
Some(Cow::Owned(_)) => Err(Error::UnexpectedValue),
Some(Cow::Owned(_)) => Err(Error::UnexpectedValue(
"string with escape characters cannot be deserialized as &str",
)),
None => Err(Error::MissingValue(&Kind::Scalar)),
}
}

View File

@ -60,7 +60,7 @@ pub fn from_str<'xml, T: FromXml<'xml>>(input: &'xml str) -> Result<T, Error> {
};
if id != expected {
return Err(Error::UnexpectedValue);
return Err(Error::UnexpectedValue("unexpected root"));
}
let mut value = None;
@ -99,7 +99,7 @@ pub enum Error {
#[error("unexpected end of stream")]
UnexpectedEndOfStream,
#[error("unexpected value")]
UnexpectedValue,
UnexpectedValue(&'static str),
#[error("unexpected tag")]
UnexpectedTag,
#[error("missing tag")]

View File

@ -39,7 +39,7 @@ fn default_namespaces() {
from_str(
"<NestedDe xmlns=\"WRONG\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedDe>"
),
Err::<NestedDe, _>(Error::UnexpectedValue)
Err::<NestedDe, _>(Error::UnexpectedValue("unexpected root"))
);
// Correct child namespace

View File

@ -30,7 +30,7 @@ fn escape_back() {
from_str(
"<StructSpecialEntities xmlns=\"URI\"><string>&lt;&gt;&amp;&quot;&apos;adsad&quot;</string><str>str&amp;</str></StructSpecialEntities>"
),
Err::<StructSpecialEntities, _>(Error::UnexpectedValue)
Err::<StructSpecialEntities, _>(Error::UnexpectedValue("string with escape characters cannot be deserialized as &str"))
);
// Borrowed