use std::error::Error as StdError;
use anyhow::anyhow;
use cid::Error as CidError;
use fvm_ipld_encoding::Error as EncodingError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("index {0} out of range for the amt")]
OutOfRange(u64),
#[error("failed to load AMT: height out of bounds: {0} > {1}")]
MaxHeight(u32, u32),
#[error(transparent)]
Cid(#[from] CidError),
#[error("Tried to serialize without saving cache, run flush() on Amt before serializing")]
Cached,
#[error("Vector length does not match bitmap")]
InvalidVecLength,
#[error("Serialized node cannot contain both links and values")]
LinksAndValues,
#[error("Cid ({0}) did not match any in database")]
CidNotFound(String),
#[error("{0}")]
Dynamic(anyhow::Error),
}
impl From<String> for Error {
fn from(e: String) -> Self {
Self::Dynamic(anyhow::anyhow!(e))
}
}
impl From<&'static str> for Error {
fn from(e: &'static str) -> Self {
Self::Dynamic(anyhow::anyhow!(e))
}
}
impl From<anyhow::Error> for Error {
fn from(e: anyhow::Error) -> Self {
e.downcast::<Error>().unwrap_or_else(Self::Dynamic)
}
}
impl From<EncodingError> for Error {
fn from(e: EncodingError) -> Self {
Self::Dynamic(anyhow!(e))
}
}
impl From<Box<dyn StdError + Send + Sync>> for Error {
fn from(e: Box<dyn StdError + Send + Sync>) -> Self {
Self::Dynamic(anyhow!(e))
}
}