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/chunk.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/chunk.rs')
-rw-r--r-- | src/chunk.rs | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/src/chunk.rs b/src/chunk.rs index a67ed8c..0eed38a 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -36,8 +36,11 @@ pub enum GenerationChunkError { #[error(transparent)] Utf8Error(#[from] std::str::Utf8Error), - #[error(transparent)] - SerdeJsonError(#[from] serde_json::Error), + #[error("failed to parse JSON: {0}")] + JsonParse(serde_json::Error), + + #[error("failed to serialize to JSON: {0}")] + JsonGenerate(serde_json::Error), } /// A result from a chunk operation. @@ -51,7 +54,7 @@ impl GenerationChunk { pub fn from_data_chunk(chunk: &DataChunk) -> GenerationChunkResult<Self> { let data = chunk.data(); let data = std::str::from_utf8(data)?; - Ok(serde_json::from_str(data)?) + serde_json::from_str(data).map_err(GenerationChunkError::JsonParse) } pub fn is_empty(&self) -> bool { @@ -67,7 +70,7 @@ impl GenerationChunk { } pub fn to_data_chunk(&self) -> GenerationChunkResult<DataChunk> { - let json = serde_json::to_string(self)?; + let json = serde_json::to_string(self).map_err(GenerationChunkError::JsonGenerate)?; Ok(DataChunk::new(json.as_bytes().to_vec())) } } |