ckb_error/
internal.rs

1use crate::{
2    def_error_base_on_kind, impl_error_conversion_with_adaptor, impl_error_conversion_with_kind,
3};
4use derive_more::Display;
5use std::fmt;
6use thiserror::Error;
7
8/// An error with no reason.
9#[derive(Error, Debug, Clone, Copy)]
10#[error("No reason provided")]
11pub struct SilentError;
12
13/// An error with only a string as the reason.
14#[derive(Error, Debug, Clone)]
15#[error("{0}")]
16pub struct OtherError(String);
17
18/// A list specifying categories of ckb internal error.
19///
20/// This list is intended to grow over time and it is not recommended to exhaustively match against it.
21///
22/// It is used with the [`InternalError`].
23///
24/// [`InternalError`]: ../ckb_error/struct.InternalError.html
25#[derive(Debug, PartialEq, Eq, Clone, Copy, Display)]
26pub enum InternalErrorKind {
27    /// An arithmetic overflow occurs during capacity calculation, e.g. `Capacity::safe_add`
28    CapacityOverflow,
29
30    /// Persistent data had corrupted
31    DataCorrupted,
32
33    /// Error occurs during database operations
34    Database,
35
36    /// It indicates that the underlying error is [`BlockAssemblerError`]
37    ///
38    /// [`BlockAssemblerError`]: ../ckb_tx_pool/error/enum.BlockAssemblerError.html
39    BlockAssembler,
40
41    /// VM internal error
42    VM,
43
44    /// MMR internal error
45    MMR,
46
47    /// Unknown system error
48    System,
49
50    /// The feature is disabled or is conflicted with the configuration
51    Config,
52
53    /// Interrupts, such as a Ctrl-C signal
54    Interrupts,
55
56    /// Other system error
57    Other,
58}
59
60def_error_base_on_kind!(InternalError, InternalErrorKind, "Internal error.");
61
62impl_error_conversion_with_kind!(InternalError, crate::ErrorKind::Internal, crate::Error);
63
64impl_error_conversion_with_kind!(OtherError, InternalErrorKind::Other, InternalError);
65impl_error_conversion_with_adaptor!(OtherError, InternalError, crate::Error);
66
67impl OtherError {
68    /// Creates an error with only a string as the reason.
69    pub fn new<T>(reason: T) -> Self
70    where
71        T: fmt::Display,
72    {
73        Self(reason.to_string())
74    }
75}