From 80ae98bf87c57aa361a13c3dd925455fb67e3f03 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 24 Apr 2021 08:18:56 +0300 Subject: 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. --- src/chunk.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/chunk.rs') 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 { 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 { - 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())) } } -- cgit v1.2.1