use std::convert::Infallible;
use grovedb_costs::CostResult;
use crate::PathQuery;
#[cfg(any(feature = "minimal", feature = "verify"))]
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("infallible")]
Infallible,
#[error("cyclic reference path")]
CyclicReference,
#[error("reference hops limit exceeded")]
ReferenceLimit,
#[error("missing reference {0}")]
MissingReference(String),
#[error("internal error: {0}")]
InternalError(String),
#[error("invalid proof: {1}")]
InvalidProof(PathQuery, String),
#[error("invalid input: {0}")]
InvalidInput(&'static str),
#[error("wrong element type: {0}")]
WrongElementType(&'static str),
#[error("path key not found: {0}")]
PathKeyNotFound(String),
#[error("path not found: {0}")]
PathNotFound(String),
#[error("path parent layer not found: {0}")]
PathParentLayerNotFound(String),
#[error("corrupted referenced path key not found: {0}")]
CorruptedReferencePathKeyNotFound(String),
#[error("corrupted referenced path not found: {0}")]
CorruptedReferencePathNotFound(String),
#[error("corrupted referenced path key not found: {0}")]
CorruptedReferencePathParentLayerNotFound(String),
#[error("invalid parent layer path: {0}")]
InvalidParentLayerPath(String),
#[error("invalid path: {0}")]
InvalidPath(String),
#[error("corrupted path: {0}")]
CorruptedPath(String),
#[error("invalid query: {0}")]
InvalidQuery(&'static str),
#[error("missing parameter: {0}")]
MissingParameter(&'static str),
#[error("invalid parameter: {0}")]
InvalidParameter(&'static str),
#[cfg(feature = "minimal")]
#[error("storage_cost error: {0}")]
StorageError(#[from] grovedb_storage::error::Error),
#[error("data corruption error: {0}")]
CorruptedData(String),
#[error("data storage error: {0}")]
CorruptedStorage(String),
#[error("invalid code execution error: {0}")]
InvalidCodeExecution(&'static str),
#[error("corrupted code execution error: {0}")]
CorruptedCodeExecution(&'static str),
#[error("invalid batch operation error: {0}")]
InvalidBatchOperation(&'static str),
#[error("delete up tree stop height more than initial path size error: {0}")]
DeleteUpTreeStopHeightMoreThanInitialPathSize(String),
#[error("deleting non empty tree error: {0}")]
DeletingNonEmptyTree(&'static str),
#[error("clearing tree with subtrees not allowed error: {0}")]
ClearingTreeWithSubtreesNotAllowed(&'static str),
#[error("just in time element flags client error: {0}")]
JustInTimeElementFlagsClientError(String),
#[error("split removal bytes client error: {0}")]
SplitRemovalBytesClientError(String),
#[error("client returned non client error: {0}")]
ClientReturnedNonClientError(String),
#[error("override not allowed error: {0}")]
OverrideNotAllowed(&'static str),
#[error("path not found in cache for estimated costs: {0}")]
PathNotFoundInCacheForEstimatedCosts(String),
#[error("not supported: {0}")]
NotSupported(String),
#[error("merk error: {0}")]
MerkError(grovedb_merk::error::Error),
#[error(transparent)]
VersionError(grovedb_version::error::GroveVersionError),
#[error("cyclic error")]
CyclicError(&'static str),
}
impl Error {
pub fn add_context(&mut self, append: impl AsRef<str>) {
match self {
Self::MissingReference(s)
| Self::InternalError(s)
| Self::InvalidProof(_, s)
| Self::PathKeyNotFound(s)
| Self::PathNotFound(s)
| Self::PathParentLayerNotFound(s)
| Self::CorruptedReferencePathKeyNotFound(s)
| Self::CorruptedReferencePathNotFound(s)
| Self::CorruptedReferencePathParentLayerNotFound(s)
| Self::InvalidParentLayerPath(s)
| Self::InvalidPath(s)
| Self::CorruptedPath(s)
| Self::CorruptedData(s)
| Self::CorruptedStorage(s)
| Self::DeleteUpTreeStopHeightMoreThanInitialPathSize(s)
| Self::JustInTimeElementFlagsClientError(s)
| Self::SplitRemovalBytesClientError(s)
| Self::ClientReturnedNonClientError(s)
| Self::PathNotFoundInCacheForEstimatedCosts(s)
| Self::NotSupported(s) => {
s.push_str(", ");
s.push_str(append.as_ref());
}
_ => {}
}
}
}
pub trait GroveDbErrorExt {
fn add_context(self, append: impl AsRef<str>) -> Self;
}
impl<T> GroveDbErrorExt for CostResult<T, Error> {
fn add_context(self, append: impl AsRef<str>) -> Self {
self.map_err(|mut e| {
e.add_context(append.as_ref());
e
})
}
}
impl From<Infallible> for Error {
fn from(_value: Infallible) -> Self {
Self::Infallible
}
}
impl From<grovedb_merk::error::Error> for Error {
fn from(value: grovedb_merk::Error) -> Self {
Error::MerkError(value)
}
}
impl From<grovedb_version::error::GroveVersionError> for Error {
fn from(value: grovedb_version::error::GroveVersionError) -> Self {
Error::VersionError(value)
}
}