Skip to content

Commit

Permalink
feat: Improve Error Handling by Adding Display Implementations (#140)
Browse files Browse the repository at this point in the history
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 <islam.elashi@dfinity.org>
  • Loading branch information
b3hr4d and ielashi authored Sep 21, 2023
1 parent f0f716a commit 0375df2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/btreemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ use allocator::Allocator;
pub use iter::Iter;
use iter::{Cursor, Index};
use node::{DerivedPageSize, Entry, Node, NodeType, PageSize, Version};
use std::borrow::Cow;
use std::marker::PhantomData;
use std::ops::{Bound, RangeBounds};
use std::{borrow::Cow, fmt};

#[cfg(test)]
mod proptests;
Expand Down Expand Up @@ -1194,8 +1194,8 @@ pub enum InsertError {
ValueTooLarge { given: usize, max: usize },
}

impl std::fmt::Display for InsertError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for InsertError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::KeyTooLarge { given, max } => {
write!(
Expand Down
21 changes: 21 additions & 0 deletions src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::storable::Storable;
use crate::{Memory, WASM_PAGE_SIZE};
use std::borrow::{Borrow, Cow};
use std::fmt;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -45,6 +46,26 @@ pub enum InitError {
ValueTooLarge { value_size: u64 },
}

impl fmt::Display for InitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InitError::IncompatibleVersion {
last_supported_version,
decoded_version,
} => write!(
f,
"Incompatible version: last supported version is {}, but the memory contains version {}",
last_supported_version, decoded_version
),
InitError::ValueTooLarge { value_size } => write!(
f,
"The initial value is too large to fit into the memory: {} bytes",
value_size
),
}
}
}

/// Indicates a failure to set cell's value.
#[derive(Debug, PartialEq, Eq)]
pub enum ValueError {
Expand Down
25 changes: 25 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
use crate::{read_u64, safe_write, write_u64, Address, GrowFailed, Memory, Storable};
use std::borrow::Cow;
use std::cell::RefCell;
use std::fmt;
use std::marker::PhantomData;
use std::thread::LocalKey;

Expand Down Expand Up @@ -98,6 +99,30 @@ pub enum InitError {
InvalidIndex,
}

impl fmt::Display for InitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InitError::IncompatibleDataVersion {
last_supported_version,
decoded_version,
} => write!(
f,
"Incompatible data version: last supported version is {}, but decoded version is {}",
last_supported_version, decoded_version
),
InitError::IncompatibleIndexVersion {
last_supported_version,
decoded_version,
} => write!(
f,
"Incompatible index version: last supported version is {}, but decoded version is {}",
last_supported_version, decoded_version
),
InitError::InvalidIndex => write!(f, "Invalid index"),
}
}
}

#[derive(Debug, PartialEq, Eq)]
pub enum WriteError {
GrowFailed { current_size: u64, delta: u64 },
Expand Down

0 comments on commit 0375df2

Please # to comment.