Replace assert!()s with proper errors in parsers.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-19 23:41:54 +01:00
commit fc7a0517d3
8 changed files with 23 additions and 8 deletions

View file

@ -15,7 +15,7 @@ impl MessagePayload for Body {}
pub fn parse_body(root: &Element) -> Result<Body, Error> { pub fn parse_body(root: &Element) -> Result<Body, Error> {
if !root.is("body", JABBER_CLIENT_NS) { if !root.is("body", JABBER_CLIENT_NS) {
return Err(Error::ParseError("Not a body element.")); return Err(Error::ParseError("This is not a body element."));
} }
for _ in root.children() { for _ in root.children() {
return Err(Error::ParseError("Unknown child in body element.")); return Err(Error::ParseError("Unknown child in body element."));

View file

@ -31,7 +31,7 @@ pub fn parse_chatstate(root: &Element) -> Result<ChatState, Error> {
} else if root.is("paused", CHATSTATES_NS) { } else if root.is("paused", CHATSTATES_NS) {
Ok(ChatState::Paused) Ok(ChatState::Paused)
} else { } else {
Err(Error::ParseError("Unknown chatstate element.")) Err(Error::ParseError("This is not a chatstate element."))
} }
} }

View file

@ -52,7 +52,10 @@ pub struct DataForm {
} }
pub fn parse_data_form(root: &Element) -> Result<DataForm, Error> { pub fn parse_data_form(root: &Element) -> Result<DataForm, Error> {
assert!(root.is("x", DATA_FORMS_NS)); if !root.is("x", DATA_FORMS_NS) {
return Err(Error::ParseError("This is not a data form element.")),
}
let type_: DataFormType = match root.attr("type") { let type_: DataFormType = match root.attr("type") {
Some(type_) => type_.parse()?, Some(type_) => type_.parse()?,
None => return Err(Error::ParseError("Type attribute on data form is mandatory.")), None => return Err(Error::ParseError("Type attribute on data form is mandatory.")),

View file

@ -29,7 +29,10 @@ pub struct Disco {
} }
pub fn parse_disco(root: &Element) -> Result<Disco, Error> { pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
assert!(root.is("query", DISCO_INFO_NS)); if !root.is("query", DISCO_INFO_NS) {
return Err(Error::ParseError("This is not a disco#info element.")),
}
let mut identities: Vec<Identity> = vec!(); let mut identities: Vec<Identity> = vec!();
let mut features: Vec<Feature> = vec!(); let mut features: Vec<Feature> = vec!();
let mut extensions: Vec<DataForm> = vec!(); let mut extensions: Vec<DataForm> = vec!();

View file

@ -64,7 +64,7 @@ pub fn parse_ibb(root: &Element) -> Result<IBB, Error> {
stanza: stanza stanza: stanza
}) })
} else { } else {
Err(Error::ParseError("Unknown ibb element.")) Err(Error::ParseError("This is not an ibb element."))
} }
} }

View file

@ -209,7 +209,10 @@ pub struct Jingle {
} }
pub fn parse_jingle(root: &Element) -> Result<Jingle, Error> { pub fn parse_jingle(root: &Element) -> Result<Jingle, Error> {
assert!(root.is("jingle", JINGLE_NS)); if !root.is("jingle", JINGLE_NS) {
return Err(Error::ParseError("This is not a Jingle element.")),
}
let mut contents: Vec<Content> = vec!(); let mut contents: Vec<Content> = vec!();
let action = root.attr("action") let action = root.attr("action")

View file

@ -18,7 +18,10 @@ pub struct MediaElement {
} }
pub fn parse_media_element(root: &Element) -> Result<MediaElement, Error> { pub fn parse_media_element(root: &Element) -> Result<MediaElement, Error> {
assert!(root.is("media", MEDIA_ELEMENT_NS)); if !root.is("media", MEDIA_ELEMENT_NS) {
return Err(Error::ParseError("This is not a media element.")),
}
let width = root.attr("width").and_then(|width| width.parse().ok()); let width = root.attr("width").and_then(|width| width.parse().ok());
let height = root.attr("height").and_then(|height| height.parse().ok()); let height = root.attr("height").and_then(|height| height.parse().ok());
let mut uris = vec!(); let mut uris = vec!();

View file

@ -9,7 +9,10 @@ pub struct Ping {
} }
pub fn parse_ping(root: &Element) -> Result<Ping, Error> { pub fn parse_ping(root: &Element) -> Result<Ping, Error> {
assert!(root.is("ping", PING_NS)); if !root.is("ping", PING_NS) {
return Err(Error::ParseError("This is not a ping element.")),
}
for _ in root.children() { for _ in root.children() {
return Err(Error::ParseError("Unknown child in ping element.")); return Err(Error::ParseError("Unknown child in ping element."));
} }