diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-04-24 08:18:56 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-04-29 10:51:31 +0300 |
commit | 80ae98bf87c57aa361a13c3dd925455fb67e3f03 (patch) | |
tree | aca3860996a6ebd622802d6a41493008189203ab /src/chunker.rs | |
parent | bf645f3645fd2ee57495eafd1ccfb4afbe917bec (diff) | |
download | obnam2-80ae98bf87c57aa361a13c3dd925455fb67e3f03.tar.gz |
feat: improve error messages
All unclear error messages should now be clearer. For example, all the
ones related to a file mention the file name and the attempted
operation that failed.
Diffstat (limited to 'src/chunker.rs')
-rw-r--r-- | src/chunker.rs | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/chunker.rs b/src/chunker.rs index f424833..eeeed8d 100644 --- a/src/chunker.rs +++ b/src/chunker.rs @@ -2,29 +2,32 @@ use crate::checksummer::sha256; use crate::chunk::DataChunk; use crate::chunkmeta::ChunkMeta; use std::io::prelude::*; +use std::path::{Path, PathBuf}; pub struct Chunker { chunk_size: usize, buf: Vec<u8>, + filename: PathBuf, handle: std::fs::File, } #[derive(Debug, thiserror::Error)] pub enum ChunkerError { - #[error(transparent)] - IoError(#[from] std::io::Error), + #[error("failed to read file {0}: {1}")] + FileRead(PathBuf, std::io::Error), } pub type ChunkerResult<T> = Result<T, ChunkerError>; impl Chunker { - pub fn new(chunk_size: usize, handle: std::fs::File) -> Self { + pub fn new(chunk_size: usize, handle: std::fs::File, filename: &Path) -> Self { let mut buf = vec![]; buf.resize(chunk_size, 0); Self { chunk_size, buf, handle, + filename: filename.to_path_buf(), } } @@ -32,7 +35,10 @@ impl Chunker { let mut used = 0; loop { - let n = self.handle.read(&mut self.buf.as_mut_slice()[used..])?; + let n = self + .handle + .read(&mut self.buf.as_mut_slice()[used..]) + .map_err(|err| ChunkerError::FileRead(self.filename.to_path_buf(), err))?; used += n; if n == 0 || used == self.chunk_size { break; |