chiark / gitweb /
zipfile: Better reporting of missing members
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 4 May 2021 19:36:05 +0000 (20:36 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 4 May 2021 21:01:58 +0000 (22:01 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bundles.rs

index 7a8eb0a953edf5bcb65f566a479966deef6ee2de..b6ca2e1f3d858bad424388f60b5d818d38dad0f5 100644 (file)
@@ -189,13 +189,27 @@ where S: Display + Debug
           index, suffix)
 }
 
+#[derive(Error,Debug)]
+pub enum LoadError {
+  BadBundle(BadBundle),
+  IE(#[from] IE),
+}
+display_as_debug!{LoadError}
+use LoadError as LE;
+
+impl From<ZipError> for LoadError {
+  fn from(ze: ZipError) -> LoadError {
+    LE::BadBundle(format!("bad zipfile: {}", ze))
+  }
+}
+
 #[ext(pub)]
 impl<R> ZipArchive<R> where R: Read + io::Seek {
-  fn by_name_caseless<'a>(&'a mut self, name: &str)
-                          -> Result<ZipFile<'a>, ZipError>
+  #[throws(LoadError)]
+  fn by_name_caseless<'a>(&'a mut self, name: &str) -> ZipFile<'a>
   {
     fn search<'a,R>(za: &'a mut ZipArchive<R>, name: &str)
-                    -> Result<usize, ZipError>
+                    -> Result<usize, LoadError>
     where R: Read + io::Seek
     {
       for i in 0..za.len() {
@@ -204,10 +218,10 @@ impl<R> ZipArchive<R> where R: Read + io::Seek {
         let m = m?;
         if m.name_raw().eq_ignore_ascii_case(name.as_bytes()) { return Ok(i) }
       }
-      return Err(ZipError::FileNotFound);
+      return Err(LE::BadBundle(format!("bundle missing {}", name)));
     }
     let i = search(self, name)?;
-    self.by_index(i)
+    self.by_index(i)?
   }
 }
 
@@ -282,20 +296,6 @@ pub struct Uploading {
 #[throws(IE)]
 fn load_bundle(ib: &mut InstanceBundles, ig: &mut Instance,
                id: Id, path: &str) {
-  #[derive(Error,Debug)]
-  enum LoadError {
-    BadBundle(BadBundle),
-    IE(#[from] IE),
-  }
-  display_as_debug!{LoadError}
-  use LoadError as LE;
-
-  impl From<ZipError> for LoadError {
-    fn from(ze: ZipError) -> LoadError {
-      LE::BadBundle(format!("bad zipfile: {}", ze))
-    }
-  }
-
   let iu: usize = id.index.into();
 
   match ib.bundles.get(iu) {