Skip to content

Commit 0375df2

Browse files
b3hr4dielashi
andauthored
feat: Improve Error Handling by Adding Display Implementations (#140)
This pull request aims to improve the error-handling mechanism of the codebase by providing more descriptive error messages. It involves changes across multiple files (`cell.rs`, `log.rs`) to add implementations for the `fmt::Display` trait for `InitError`. #### Key Changes: 1. **Consistent Formatting**: Replaced `std::fmt` with `fmt` to maintain consistency in the code. - File: `btreemap.rs` 2. **Enhanced Error Messages for `InitError` in `cell.rs`**: - Describes errors related to incompatible versions and value sizes. 3. **Detailed Error Messages for `InitError` in `log.rs`**: - Explains issues with incompatible data and index versions, as well as invalid indices. #### Why this is important: Better error messages enhance the debugging experience and allow developers to understand the issues quickly, thereby saving time and effort. #### Files Changed: - `src/btreemap.rs` - `src/cell.rs` - `src/log.rs` #### Types of changes: - [x] Bugfix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation Update (if none of the other choices apply) Co-authored-by: Islam El-Ashi <[email protected]>
1 parent f0f716a commit 0375df2

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/btreemap.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ use allocator::Allocator;
3636
pub use iter::Iter;
3737
use iter::{Cursor, Index};
3838
use node::{DerivedPageSize, Entry, Node, NodeType, PageSize, Version};
39-
use std::borrow::Cow;
4039
use std::marker::PhantomData;
4140
use std::ops::{Bound, RangeBounds};
41+
use std::{borrow::Cow, fmt};
4242

4343
#[cfg(test)]
4444
mod proptests;
@@ -1194,8 +1194,8 @@ pub enum InsertError {
11941194
ValueTooLarge { given: usize, max: usize },
11951195
}
11961196

1197-
impl std::fmt::Display for InsertError {
1198-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1197+
impl fmt::Display for InsertError {
1198+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11991199
match self {
12001200
Self::KeyTooLarge { given, max } => {
12011201
write!(

src/cell.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use crate::storable::Storable;
33
use crate::{Memory, WASM_PAGE_SIZE};
44
use std::borrow::{Borrow, Cow};
5+
use std::fmt;
56

67
#[cfg(test)]
78
mod tests;
@@ -45,6 +46,26 @@ pub enum InitError {
4546
ValueTooLarge { value_size: u64 },
4647
}
4748

49+
impl fmt::Display for InitError {
50+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51+
match self {
52+
InitError::IncompatibleVersion {
53+
last_supported_version,
54+
decoded_version,
55+
} => write!(
56+
f,
57+
"Incompatible version: last supported version is {}, but the memory contains version {}",
58+
last_supported_version, decoded_version
59+
),
60+
InitError::ValueTooLarge { value_size } => write!(
61+
f,
62+
"The initial value is too large to fit into the memory: {} bytes",
63+
value_size
64+
),
65+
}
66+
}
67+
}
68+
4869
/// Indicates a failure to set cell's value.
4970
#[derive(Debug, PartialEq, Eq)]
5071
pub enum ValueError {

src/log.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
use crate::{read_u64, safe_write, write_u64, Address, GrowFailed, Memory, Storable};
5858
use std::borrow::Cow;
5959
use std::cell::RefCell;
60+
use std::fmt;
6061
use std::marker::PhantomData;
6162
use std::thread::LocalKey;
6263

@@ -98,6 +99,30 @@ pub enum InitError {
9899
InvalidIndex,
99100
}
100101

102+
impl fmt::Display for InitError {
103+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104+
match self {
105+
InitError::IncompatibleDataVersion {
106+
last_supported_version,
107+
decoded_version,
108+
} => write!(
109+
f,
110+
"Incompatible data version: last supported version is {}, but decoded version is {}",
111+
last_supported_version, decoded_version
112+
),
113+
InitError::IncompatibleIndexVersion {
114+
last_supported_version,
115+
decoded_version,
116+
} => write!(
117+
f,
118+
"Incompatible index version: last supported version is {}, but decoded version is {}",
119+
last_supported_version, decoded_version
120+
),
121+
InitError::InvalidIndex => write!(f, "Invalid index"),
122+
}
123+
}
124+
}
125+
101126
#[derive(Debug, PartialEq, Eq)]
102127
pub enum WriteError {
103128
GrowFailed { current_size: u64, delta: u64 },

0 commit comments

Comments
 (0)