2022-07-26 12:50:28 +00:00
|
|
|
//! Contains error types and other error handling tools.
|
|
|
|
|
2022-08-02 06:02:10 +00:00
|
|
|
use core::fmt;
|
|
|
|
|
2023-03-28 01:16:47 +00:00
|
|
|
use internals::write_err;
|
2022-08-02 06:02:10 +00:00
|
|
|
|
|
|
|
use crate::consensus::encode;
|
2022-07-26 12:50:28 +00:00
|
|
|
pub use crate::parse::ParseIntError;
|
|
|
|
|
2022-08-02 06:02:10 +00:00
|
|
|
/// A general error code, other errors should implement conversions to/from this
|
|
|
|
/// if appropriate.
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum Error {
|
|
|
|
/// Encoding error
|
|
|
|
Encode(encode::Error),
|
|
|
|
/// The header hash is not below the target
|
|
|
|
BlockBadProofOfWork,
|
|
|
|
/// The `target` field of a block header did not match the expected difficulty
|
|
|
|
BlockBadTarget,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Error::Encode(ref e) => write_err!(f, "encoding error"; e),
|
|
|
|
Error::BlockBadProofOfWork => f.write_str("block target correct but not attained"),
|
|
|
|
Error::BlockBadTarget => f.write_str("block target incorrect"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl std::error::Error for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
use self::Error::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Encode(e) => Some(e),
|
|
|
|
BlockBadProofOfWork | BlockBadTarget => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl From<encode::Error> for Error {
|
|
|
|
fn from(e: encode::Error) -> Error { Error::Encode(e) }
|
|
|
|
}
|
|
|
|
|
2022-07-26 12:50:28 +00:00
|
|
|
/// Impls std::error::Error for the specified type with appropriate attributes, possibly returning
|
|
|
|
/// source.
|
|
|
|
macro_rules! impl_std_error {
|
|
|
|
// No source available
|
|
|
|
($type:ty) => {
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl std::error::Error for $type {}
|
|
|
|
};
|
|
|
|
// Struct with $field as source
|
|
|
|
($type:ty, $field:ident) => {
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl std::error::Error for $type {
|
2022-08-02 06:00:11 +00:00
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.$field) }
|
2022-07-26 12:50:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-08-09 12:40:09 +00:00
|
|
|
pub(crate) use impl_std_error;
|