use std::{error::Error as StdError, fmt, ops::Deref, sync::Arc};
mod convert;
mod internal;
pub mod prelude;
pub mod util;
use derive_more::Display;
pub use internal::{InternalError, InternalErrorKind, OtherError, SilentError};
use prelude::*;
#[derive(Clone)]
pub struct AnyError(Arc<anyhow::Error>);
#[derive(Debug, Clone, Copy, Eq, PartialEq, Display)]
pub enum ErrorKind {
OutPoint,
Transaction,
SubmitTransaction,
Script,
Header,
Block,
Internal,
Dao,
Spec,
}
def_error_base_on_kind!(Error, ErrorKind, "Top-level ckb error type.");
impl<E> From<E> for AnyError
where
E: StdError + Send + Sync + 'static,
{
fn from(error: E) -> Self {
Self(Arc::new(error.into()))
}
}
impl Deref for AnyError {
type Target = Arc<anyhow::Error>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Display for AnyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Debug for AnyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
derive_more::Display::fmt(self, f)
}
}
pub fn is_internal_db_error(error: &Error) -> bool {
if error.kind() == ErrorKind::Internal {
let error_kind = error
.downcast_ref::<InternalError>()
.expect("error kind checked")
.kind();
if error_kind == InternalErrorKind::DataCorrupted {
panic!("{}", error)
} else {
return error_kind == InternalErrorKind::Database
|| error_kind == InternalErrorKind::System;
}
}
false
}