Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoseph Frazer <joseph.frazer@ryansg.com>2020-03-07 11:18:56 -0500
committerJoseph Frazer <joseph.frazer@ryansg.com>2020-03-09 08:23:13 -0400
commit01e7d3e56059fefeb862028e5868f947a042dcd4 (patch)
tree92d37e43ade05c026e96fcba88643ae55aa327ec
parentf373a00a80a4a366573d178911f455561fc2c0ff (diff)
downloadtame-01e7d3e56059fefeb862028e5868f947a042dcd4.tar.gz
tame-01e7d3e56059fefeb862028e5868f947a042dcd4.tar.bz2
tame-01e7d3e56059fefeb862028e5868f947a042dcd4.zip
[DEV-7134] Propagate errors from the writer
When an error occurs during the XML writing, they should be shown to the user.
-rw-r--r--tamer/src/ld/poc.rs28
-rw-r--r--tamer/src/obj/xmle/writer/writer.rs23
2 files changed, 36 insertions, 15 deletions
diff --git a/tamer/src/ld/poc.rs b/tamer/src/ld/poc.rs
index 6e0a7fe..52f4366 100644
--- a/tamer/src/ld/poc.rs
+++ b/tamer/src/ld/poc.rs
@@ -294,14 +294,14 @@ fn get_interner_value<'a, 'i, I: Interner<'i>>(
depgraph: &'a LinkerAsg<'i>,
interner: &'i I,
name: &str,
-) -> &'a Object<'i> {
- depgraph
- .get(
- depgraph
- .lookup(interner.intern(name))
- .unwrap_or_else(|| panic!("missing identifier: {}", name)),
- )
- .expect("Could not get interner value")
+) -> Result<&'a Object<'i>, Box<dyn Error>> {
+ match depgraph.lookup(interner.intern(name)) {
+ Some(frag) => match depgraph.get(frag) {
+ Some(result) => Ok(result),
+ None => Err(XmloError::MissingFragment(String::from(name)).into()),
+ },
+ None => Err(XmloError::MissingFragment(String::from(name)).into()),
+ }
}
fn output_xmle<'a, 'i, I: Interner<'i>>(
@@ -317,12 +317,12 @@ fn output_xmle<'a, 'i, I: Interner<'i>>(
depgraph,
interner,
&String::from(":map:___head"),
- ));
+ )?);
sorted.map.push_tail(get_interner_value(
depgraph,
interner,
&String::from(":map:___tail"),
- ));
+ )?);
}
if !sorted.retmap.is_empty() {
@@ -330,19 +330,17 @@ fn output_xmle<'a, 'i, I: Interner<'i>>(
depgraph,
interner,
&String::from(":retmap:___head"),
- ));
+ )?);
sorted.retmap.push_tail(get_interner_value(
depgraph,
interner,
&String::from(":retmap:___tail"),
- ));
+ )?);
}
let file = fs::File::create(output)?;
let mut xmle_writer = XmleWriter::new(file);
- xmle_writer
- .write(&sorted, name, &relroot)
- .expect("Could not write xmle output");
+ xmle_writer.write(&sorted, name, &relroot)?;
Ok(())
}
diff --git a/tamer/src/obj/xmle/writer/writer.rs b/tamer/src/obj/xmle/writer/writer.rs
index 76f21a8..7c592cf 100644
--- a/tamer/src/obj/xmle/writer/writer.rs
+++ b/tamer/src/obj/xmle/writer/writer.rs
@@ -187,12 +187,35 @@ impl<'a, 'i> Sections<'a, 'i> {
/// Error implementations for the writer
#[derive(Debug)]
pub enum WriterError {
+ /// Propagated IO error
Io(IoError),
+ /// Propagated UTF8 error
Utf8(Utf8Error),
+ /// Propagated XML error
XmlError(XmlError),
+ /// Something other than a fragment was given when a fragment was expected
ExpectedFragment(String),
}
+impl std::fmt::Display for WriterError {
+ fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
+ match self {
+ Self::Io(inner) => inner.fmt(fmt),
+ Self::Utf8(inner) => inner.fmt(fmt),
+ Self::XmlError(inner) => inner.fmt(fmt),
+ Self::ExpectedFragment(msg) => {
+ write!(fmt, "expected fragment: {}", msg)
+ }
+ }
+ }
+}
+
+impl std::error::Error for WriterError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ None
+ }
+}
+
impl From<IoError> for WriterError {
fn from(err: IoError) -> Self {
WriterError::Io(err)