Skip to content

Commit

Permalink
refactor(core): add FileSystemError
Browse files Browse the repository at this point in the history
Adds an error type for wrapping std::io::Error and providing path
and context metadata.

Initially it is used by SimpleFileStorage but it is intended for use
by any fn that calls std lib file functions with a path.
  • Loading branch information
dan-da committed Dec 5, 2022
1 parent 10a387e commit 8eaa5f1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
25 changes: 19 additions & 6 deletions kindelia_core/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use kindelia_lang::ast::Func;
use crate::bits::ProtoSerialize;
use crate::node::{self, HashedBlock};
use crate::runtime::{compile_func, CompFunc};
use crate::util::{self, bitvec_to_bytes};
use crate::util::{self, bitvec_to_bytes, FileSystemError};

/// Trait that represents serialization of a type to memory.
/// `disk_serialize` expects a sink to write to and returns the amount of bytes written
Expand Down Expand Up @@ -367,8 +367,15 @@ impl SimpleFileStorage {
let (tx, rx) = mpsc::channel::<FileWritterChannelInfo>();
// blocks are stored in `blocks` dir
let blocks_path = path.join(BLOCKS_DIR);
std::fs::create_dir_all(&blocks_path)
.map_err(|e| BlockStorageError::Write { source: Box::new(e) })?;
std::fs::create_dir_all(&blocks_path).map_err(|e| {
BlockStorageError::Write {
source: Box::new(FileSystemError {
path: blocks_path.clone(),
source: e,
context: Some("creating blocks directory".to_string()),
}),
}
})?;

let moved_path = blocks_path.clone();
// spawn thread for write the blocks files
Expand All @@ -381,9 +388,15 @@ impl SimpleFileStorage {
// create file buffer
let file_buff = bitvec_to_bytes(&block.proto_serialized());
// write file
if let Err(e) = std::fs::write(file_path, file_buff)
.map_err(|e| BlockStorageError::Write { source: Box::new(e) })
{
if let Err(e) = std::fs::write(&file_path, file_buff).map_err(|e| {
BlockStorageError::Write {
source: Box::new(FileSystemError {
path: file_path,
source: e,
context: Some("writing block to file".to_string()),
}),
}
}) {
eprintln!("Couldn't save block to disk.\n{}", e);
}
}
Expand Down
26 changes: 26 additions & 0 deletions kindelia_core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use kindelia_common::nohash_hasher::NoHashHasher;
use kindelia_common::{Name, U120, U256};

use crate::runtime::Loc;
use std::path::PathBuf;

use thiserror::Error;

pub type U64Map <T> = HashMap<u64 , T, std::hash::BuildHasherDefault<NoHashHasher<u64 >>>;
pub type U120Map<T> = HashMap<U120, T, std::hash::BuildHasherDefault<NoHashHasher<U120>>>;
Expand Down Expand Up @@ -181,3 +184,26 @@ macro_rules! print_with_timestamp {
println!("{} ~~ {}", get_time_micro(), format!($($arg)*));
};
}


// Errors
// ======

/// An error for providing metadata (path, context) for
/// filesystem errors.
///
/// This is useful because std::io::error does not provide
/// the path, and so error messages can be quite unhelpful
/// as one does not even know the path being operated on.
///
/// See: https://github.com/rust-lang/rfcs/issues/2885
///
/// It is intended that any calls to std lib filesystem calls
/// will map_err() the result to FileSystemError.
#[derive(Error, Debug)]
#[error("Filesystem error")]
pub struct FileSystemError {
pub path: PathBuf,
pub context: Option<String>,
pub source: std::io::Error,
}

0 comments on commit 8eaa5f1

Please sign in to comment.