Skip to content

Commit

Permalink
feat!: Rename BlockstoreError to Error (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique authored Apr 15, 2024
1 parent d64bc87 commit dc6a1cc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 36 deletions.
3 changes: 1 addition & 2 deletions src/block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use cid::CidGeneric;
use thiserror::Error;

/// Error returned when trying to compute new or parse existing CID. Note that errors here can be
/// specific to particular [`Block`] impl, and don't necessarily indicate invalid CID in general.
#[derive(Debug, Error, PartialEq)]
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum CidError {
/// Coded specified in CID is not supported in this context
#[error("Invalid CID codec {0}")]
Expand Down
10 changes: 5 additions & 5 deletions src/indexed_db_blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use js_sys::Uint8Array;
use rexie::{KeyRange, ObjectStore, Rexie, Store, TransactionMode};
use wasm_bindgen::{JsCast, JsValue};

use crate::{Blockstore, BlockstoreError, Result};
use crate::{Blockstore, Error, Result};

/// indexeddb version, needs to be incremented on every schema change
const DB_VERSION: u32 = 1;
Expand Down Expand Up @@ -36,7 +36,7 @@ impl IndexedDbBlockstore {
.add_object_store(ObjectStore::new(BLOCK_STORE).auto_increment(false))
.build()
.await
.map_err(|e| BlockstoreError::FatalDatabaseError(e.to_string()))?;
.map_err(|e| Error::FatalDatabaseError(e.to_string()))?;

Ok(Self { db })
}
Expand All @@ -56,7 +56,7 @@ impl Blockstore for IndexedDbBlockstore {
Ok(None)
} else {
let arr = block.dyn_ref::<Uint8Array>().ok_or_else(|| {
BlockstoreError::StoredDataError(format!(
Error::StoredDataError(format!(
"expected 'Uint8Array', got '{}'",
block
.js_typeof()
Expand Down Expand Up @@ -95,9 +95,9 @@ impl Blockstore for IndexedDbBlockstore {
}
}

impl From<rexie::Error> for BlockstoreError {
impl From<rexie::Error> for Error {
fn from(value: rexie::Error) -> Self {
BlockstoreError::FatalDatabaseError(value.to_string())
Error::FatalDatabaseError(value.to_string())
}
}

Expand Down
26 changes: 14 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::future::Future;

use cid::CidGeneric;
use multihash::Multihash;
use thiserror::Error;

use crate::block::{Block, CidError};
use crate::cond_send::{CondSend, CondSync};
Expand Down Expand Up @@ -38,8 +37,8 @@ pub use crate::redb_blockstore::RedbBlockstore;
pub use crate::sled_blockstore::SledBlockstore;

/// Error returned when performing operations on [`Blockstore`]
#[derive(Debug, Error)]
pub enum BlockstoreError {
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Provided CID is larger than max length supported by the blockstore
#[error("CID length larger that max allowed by the store")]
CidTooLarge,
Expand All @@ -65,12 +64,15 @@ pub enum BlockstoreError {
FatalDatabaseError(String),
}

type Result<T, E = BlockstoreError> = std::result::Result<T, E>;
/// Alias for a [`Result`] with the error type [`blockstore::Error`].
///
/// [`blockstore::Error`]: crate::Error
pub type Result<T, E = Error> = std::result::Result<T, E>;

#[cfg(all(not(target_arch = "wasm32"), any(feature = "sled", feature = "redb")))]
impl From<tokio::task::JoinError> for BlockstoreError {
fn from(e: tokio::task::JoinError) -> BlockstoreError {
BlockstoreError::ExecutorError(e.to_string())
impl From<tokio::task::JoinError> for Error {
fn from(e: tokio::task::JoinError) -> Error {
Error::ExecutorError(e.to_string())
}
}

Expand All @@ -79,7 +81,7 @@ impl From<tokio::task::JoinError> for BlockstoreError {
/// Implementations can impose limit on supported CID length, and any operations on longer CIDs
/// will fail with [`CidTooLarge`].
///
/// [`CidTooLarge`]: BlockstoreError::CidTooLarge
/// [`CidTooLarge`]: Error::CidTooLarge
pub trait Blockstore: CondSync {
/// Gets the block from the blockstore
fn get<const S: usize>(
Expand Down Expand Up @@ -161,7 +163,7 @@ pub(crate) fn convert_cid<const S: usize, const NEW_S: usize>(
cid: &CidGeneric<S>,
) -> Result<CidGeneric<NEW_S>> {
let hash = Multihash::<NEW_S>::wrap(cid.hash().code(), cid.hash().digest())
.map_err(|_| BlockstoreError::CidTooLarge)?;
.map_err(|_| Error::CidTooLarge)?;

// Safe to unwrap because check was done from previous construction.
let cid = CidGeneric::new(cid.version(), cid.codec(), hash).expect("malformed cid");
Expand Down Expand Up @@ -281,15 +283,15 @@ pub(crate) mod tests {

store.put_keyed(&small_cid, b"1").await.unwrap();
let put_err = store.put_keyed(&big_cid, b"1").await.unwrap_err();
assert!(matches!(put_err, BlockstoreError::CidTooLarge));
assert!(matches!(put_err, Error::CidTooLarge));

store.get(&small_cid).await.unwrap();
let get_err = store.get(&big_cid).await.unwrap_err();
assert!(matches!(get_err, BlockstoreError::CidTooLarge));
assert!(matches!(get_err, Error::CidTooLarge));

store.has(&small_cid).await.unwrap();
let has_err = store.has(&big_cid).await.unwrap_err();
assert!(matches!(has_err, BlockstoreError::CidTooLarge));
assert!(matches!(has_err, Error::CidTooLarge));
}

#[rstest]
Expand Down
24 changes: 12 additions & 12 deletions src/redb_blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use redb::{
};
use tokio::task::spawn_blocking;

use crate::{Blockstore, BlockstoreError, Result};
use crate::{Blockstore, Error, Result};

const BLOCKS_TABLE: TableDefinition<'static, &[u8], &[u8]> =
TableDefinition::new("BLOCKSTORE.BLOCKS");
Expand All @@ -26,7 +26,7 @@ impl RedbBlockstore {

let db = spawn_blocking(|| Database::create(path))
.await?
.map_err(|e| BlockstoreError::FatalDatabaseError(e.to_string()))?;
.map_err(|e| Error::FatalDatabaseError(e.to_string()))?;

Ok(RedbBlockstore::new(Arc::new(db)))
}
Expand All @@ -35,7 +35,7 @@ impl RedbBlockstore {
pub fn in_memory() -> Result<Self> {
let db = Database::builder()
.create_with_backend(redb::backends::InMemoryBackend::new())
.map_err(|e| BlockstoreError::FatalDatabaseError(e.to_string()))?;
.map_err(|e| Error::FatalDatabaseError(e.to_string()))?;

Ok(RedbBlockstore::new(Arc::new(db)))
}
Expand Down Expand Up @@ -158,18 +158,18 @@ impl Blockstore for RedbBlockstore {
}
}

impl From<TransactionError> for BlockstoreError {
impl From<TransactionError> for Error {
fn from(e: TransactionError) -> Self {
match e {
TransactionError::ReadTransactionStillInUse(_) => {
unreachable!("redb::ReadTransaction::close is never used")
}
e => BlockstoreError::FatalDatabaseError(e.to_string()),
e => Error::FatalDatabaseError(e.to_string()),
}
}
}

impl From<TableError> for BlockstoreError {
impl From<TableError> for Error {
fn from(e: TableError) -> Self {
match e {
TableError::Storage(e) => e.into(),
Expand All @@ -179,23 +179,23 @@ impl From<TableError> for BlockstoreError {
e @ TableError::TableDoesNotExist(_) => {
unreachable!("redb::ReadTransaction::open_table result not handled correctly: {e}");
}
e => BlockstoreError::StoredDataError(e.to_string()),
e => Error::StoredDataError(e.to_string()),
}
}
}

impl From<StorageError> for BlockstoreError {
impl From<StorageError> for Error {
fn from(e: StorageError) -> Self {
match e {
StorageError::ValueTooLarge(_) => BlockstoreError::ValueTooLarge,
e => BlockstoreError::FatalDatabaseError(e.to_string()),
StorageError::ValueTooLarge(_) => Error::ValueTooLarge,
e => Error::FatalDatabaseError(e.to_string()),
}
}
}

impl From<CommitError> for BlockstoreError {
impl From<CommitError> for Error {
fn from(e: CommitError) -> Self {
BlockstoreError::FatalDatabaseError(e.to_string())
Error::FatalDatabaseError(e.to_string())
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/sled_blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cid::CidGeneric;
use sled::{Db, Error as SledError, Tree};
use tokio::task::spawn_blocking;

use crate::{Blockstore, BlockstoreError, Result};
use crate::{Blockstore, Error, Result};

const BLOCKS_TREE_ID: &[u8] = b"BLOCKSTORE.BLOCKS";

Expand Down Expand Up @@ -89,14 +89,14 @@ impl Blockstore for SledBlockstore {
}

// divide errors into recoverable and not avoiding directly relying on passing sled types
impl From<SledError> for BlockstoreError {
fn from(error: SledError) -> BlockstoreError {
impl From<SledError> for Error {
fn from(error: SledError) -> Error {
match error {
e @ SledError::CollectionNotFound(_) => BlockstoreError::StoredDataError(e.to_string()),
e @ SledError::CollectionNotFound(_) => Error::StoredDataError(e.to_string()),
e @ SledError::Unsupported(_)
| e @ SledError::ReportableBug(_)
| e @ SledError::Corruption { .. }
| e @ SledError::Io(_) => BlockstoreError::FatalDatabaseError(e.to_string()),
| e @ SledError::Io(_) => Error::FatalDatabaseError(e.to_string()),
}
}
}
Expand Down

0 comments on commit dc6a1cc

Please sign in to comment.