From 6e7dccbbfbc21ae2273bd9714c430d6b2647071a Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Fri, 18 Oct 2019 19:16:06 -0600 Subject: [PATCH] Add an error enum to snapshot_utils (#6453) --- core/src/result.rs | 7 +++++++ core/src/snapshot_utils.rs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/core/src/result.rs b/core/src/result.rs index c1a0422259..f6aa73572e 100644 --- a/core/src/result.rs +++ b/core/src/result.rs @@ -3,6 +3,7 @@ use crate::cluster_info; use crate::packet; use crate::poh_recorder; +use crate::snapshot_utils; use bincode; use serde_json; use solana_ledger::blocktree; @@ -32,6 +33,7 @@ pub enum Error { BlocktreeError(blocktree::BlocktreeError), FsExtra(fs_extra::error::Error), ToBlobError, + SnapshotError(snapshot_utils::SnapshotError), } pub type Result = std::result::Result; @@ -134,6 +136,11 @@ impl std::convert::From for Error { Error::BlocktreeError(e) } } +impl std::convert::From for Error { + fn from(e: snapshot_utils::SnapshotError) -> Error { + Error::SnapshotError(e) + } +} #[cfg(test)] mod tests { diff --git a/core/src/snapshot_utils.rs b/core/src/snapshot_utils.rs index e426ae8e79..d499f00c2f 100644 --- a/core/src/snapshot_utils.rs +++ b/core/src/snapshot_utils.rs @@ -1,4 +1,3 @@ -use crate::result::{Error, Result}; use crate::snapshot_package::SnapshotPackage; use bincode::{deserialize_from, serialize_into}; use bzip2::bufread::BzDecoder; @@ -24,6 +23,32 @@ pub struct SlotSnapshotPaths { pub snapshot_file_path: PathBuf, } +#[derive(Debug)] +pub enum SnapshotError { + IO(std::io::Error), + Serialize(std::boxed::Box), + FsExtra(fs_extra::error::Error), +} +pub type Result = std::result::Result; + +impl std::convert::From for SnapshotError { + fn from(e: std::io::Error) -> SnapshotError { + SnapshotError::IO(e) + } +} + +impl std::convert::From> for SnapshotError { + fn from(e: std::boxed::Box) -> SnapshotError { + SnapshotError::Serialize(e) + } +} + +impl std::convert::From for SnapshotError { + fn from(e: fs_extra::error::Error) -> SnapshotError { + SnapshotError::FsExtra(e) + } +} + impl PartialOrd for SlotSnapshotPaths { fn partial_cmp(&self, other: &Self) -> Option { Some(self.slot.cmp(&other.slot)) @@ -127,7 +152,7 @@ pub fn add_snapshot>(snapshot_path: P, bank: &Bank) -> Result<()> let slot = bank.slot(); // snapshot_path/slot let slot_snapshot_dir = get_bank_snapshot_dir(snapshot_path, slot); - fs::create_dir_all(slot_snapshot_dir.clone()).map_err(Error::from)?; + fs::create_dir_all(slot_snapshot_dir.clone())?; // the snapshot is stored as snapshot_path/slot/slot let snapshot_file_path = slot_snapshot_dir.join(get_snapshot_file_name(slot)); @@ -276,9 +301,9 @@ fn get_bank_snapshot_dir>(path: P, slot: u64) -> PathBuf { path.as_ref().join(slot.to_string()) } -fn get_io_error(error: &str) -> Error { +fn get_io_error(error: &str) -> SnapshotError { warn!("Snapshot Error: {:?}", error); - Error::IO(IOError::new(ErrorKind::Other, error)) + SnapshotError::IO(IOError::new(ErrorKind::Other, error)) } #[cfg(test)]