From fc7a0517d31f44302a772fd06e7fa00a2f9d52dc Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 19 Apr 2017 23:41:54 +0100 Subject: [PATCH] Replace assert!()s with proper errors in parsers. --- src/body.rs | 2 +- src/chatstates.rs | 2 +- src/data_forms.rs | 5 ++++- src/disco.rs | 5 ++++- src/ibb.rs | 2 +- src/jingle.rs | 5 ++++- src/media_element.rs | 5 ++++- src/ping.rs | 5 ++++- 8 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/body.rs b/src/body.rs index 0bf2f498..43bd6914 100644 --- a/src/body.rs +++ b/src/body.rs @@ -15,7 +15,7 @@ impl MessagePayload for Body {} pub fn parse_body(root: &Element) -> Result { 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() { return Err(Error::ParseError("Unknown child in body element.")); diff --git a/src/chatstates.rs b/src/chatstates.rs index 8941408d..d84880c6 100644 --- a/src/chatstates.rs +++ b/src/chatstates.rs @@ -31,7 +31,7 @@ pub fn parse_chatstate(root: &Element) -> Result { } else if root.is("paused", CHATSTATES_NS) { Ok(ChatState::Paused) } else { - Err(Error::ParseError("Unknown chatstate element.")) + Err(Error::ParseError("This is not a chatstate element.")) } } diff --git a/src/data_forms.rs b/src/data_forms.rs index f4eb70c8..68e0e00d 100644 --- a/src/data_forms.rs +++ b/src/data_forms.rs @@ -52,7 +52,10 @@ pub struct DataForm { } pub fn parse_data_form(root: &Element) -> Result { - 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") { Some(type_) => type_.parse()?, None => return Err(Error::ParseError("Type attribute on data form is mandatory.")), diff --git a/src/disco.rs b/src/disco.rs index e436b262..d51636e2 100644 --- a/src/disco.rs +++ b/src/disco.rs @@ -29,7 +29,10 @@ pub struct Disco { } pub fn parse_disco(root: &Element) -> Result { - 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 = vec!(); let mut features: Vec = vec!(); let mut extensions: Vec = vec!(); diff --git a/src/ibb.rs b/src/ibb.rs index 1868772e..64363c35 100644 --- a/src/ibb.rs +++ b/src/ibb.rs @@ -64,7 +64,7 @@ pub fn parse_ibb(root: &Element) -> Result { stanza: stanza }) } else { - Err(Error::ParseError("Unknown ibb element.")) + Err(Error::ParseError("This is not an ibb element.")) } } diff --git a/src/jingle.rs b/src/jingle.rs index 37912a02..00b390e4 100644 --- a/src/jingle.rs +++ b/src/jingle.rs @@ -209,7 +209,10 @@ pub struct Jingle { } pub fn parse_jingle(root: &Element) -> Result { - 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 = vec!(); let action = root.attr("action") diff --git a/src/media_element.rs b/src/media_element.rs index 86f286ee..d912f126 100644 --- a/src/media_element.rs +++ b/src/media_element.rs @@ -18,7 +18,10 @@ pub struct MediaElement { } pub fn parse_media_element(root: &Element) -> Result { - 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 height = root.attr("height").and_then(|height| height.parse().ok()); let mut uris = vec!(); diff --git a/src/ping.rs b/src/ping.rs index 7176fa42..39145e22 100644 --- a/src/ping.rs +++ b/src/ping.rs @@ -9,7 +9,10 @@ pub struct Ping { } pub fn parse_ping(root: &Element) -> Result { - 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() { return Err(Error::ParseError("Unknown child in ping element.")); }