From e5684ad947267815986af7df7d6e018212ae92ae Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Wed, 9 Oct 2019 18:15:07 -0700 Subject: [PATCH] Add error for malformed project files --- src/snapshot_middleware/error.rs | 13 +++++++++++++ src/snapshot_middleware/project.rs | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/snapshot_middleware/error.rs b/src/snapshot_middleware/error.rs index 486b8324..3f1805de 100644 --- a/src/snapshot_middleware/error.rs +++ b/src/snapshot_middleware/error.rs @@ -39,6 +39,16 @@ impl SnapshotError { path: Some(path.into()), } } + + pub(crate) fn malformed_project( + inner: serde_json::Error, + path: impl Into, + ) -> SnapshotError { + SnapshotError { + detail: SnapshotErrorDetail::MalformedProject { inner }, + path: Some(path.into()), + } + } } impl Error for SnapshotError { @@ -72,6 +82,7 @@ pub enum SnapshotErrorDetail { FileDidNotExist, FileNameBadUnicode, FileContentsBadUnicode { inner: std::str::Utf8Error }, + MalformedProject { inner: serde_json::Error }, } impl SnapshotErrorDetail { @@ -81,6 +92,7 @@ impl SnapshotErrorDetail { match self { IoError { inner } => Some(inner), FileContentsBadUnicode { inner } => Some(inner), + MalformedProject { inner } => Some(inner), _ => None, } } @@ -97,6 +109,7 @@ impl fmt::Display for SnapshotErrorDetail { FileContentsBadUnicode { inner } => { write!(formatter, "file had malformed unicode: {}", inner) } + MalformedProject { inner } => write!(formatter, "{}", inner), } } } diff --git a/src/snapshot_middleware/project.rs b/src/snapshot_middleware/project.rs index 64f3a71a..d110d99b 100644 --- a/src/snapshot_middleware/project.rs +++ b/src/snapshot_middleware/project.rs @@ -10,6 +10,7 @@ use crate::{ }; use super::{ + error::SnapshotError, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, snapshot_from_imfs, }; @@ -42,8 +43,7 @@ impl SnapshotMiddleware for SnapshotProject { } let project = Project::load_from_slice(entry.contents(imfs)?, entry.path()) - // TODO: Turn this into an error object - .expect("Invalid project file"); + .map_err(|err| SnapshotError::malformed_project(err, entry.path()))?; // Snapshotting a project should always return an instance, so this // unwrap is safe.