ckb_error/
internal.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::{
    def_error_base_on_kind, impl_error_conversion_with_adaptor, impl_error_conversion_with_kind,
};
use derive_more::Display;
use std::fmt;
use thiserror::Error;

/// An error with no reason.
#[derive(Error, Debug, Clone, Copy)]
#[error("No reason provided")]
pub struct SilentError;

/// An error with only a string as the reason.
#[derive(Error, Debug, Clone)]
#[error("{0}")]
pub struct OtherError(String);

/// A list specifying categories of ckb internal error.
///
/// This list is intended to grow over time and it is not recommended to exhaustively match against it.
///
/// It is used with the [`InternalError`].
///
/// [`InternalError`]: ../ckb_error/struct.InternalError.html
#[derive(Debug, PartialEq, Eq, Clone, Copy, Display)]
pub enum InternalErrorKind {
    /// An arithmetic overflow occurs during capacity calculation, e.g. `Capacity::safe_add`
    CapacityOverflow,

    /// Persistent data had corrupted
    DataCorrupted,

    /// Error occurs during database operations
    Database,

    /// It indicates that the underlying error is [`BlockAssemblerError`]
    ///
    /// [`BlockAssemblerError`]: ../ckb_tx_pool/error/enum.BlockAssemblerError.html
    BlockAssembler,

    /// VM internal error
    VM,

    /// MMR internal error
    MMR,

    /// Unknown system error
    System,

    /// The feature is disabled or is conflicted with the configuration
    Config,

    /// Other system error
    Other,
}

def_error_base_on_kind!(InternalError, InternalErrorKind, "Internal error.");

impl_error_conversion_with_kind!(InternalError, crate::ErrorKind::Internal, crate::Error);

impl_error_conversion_with_kind!(OtherError, InternalErrorKind::Other, InternalError);
impl_error_conversion_with_adaptor!(OtherError, InternalError, crate::Error);

impl OtherError {
    /// Creates an error with only a string as the reason.
    pub fn new<T>(reason: T) -> Self
    where
        T: fmt::Display,
    {
        Self(reason.to_string())
    }
}