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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::{Error, ErrorKind};
use failure::{err_msg, Backtrace, Context, Fail};
use std::fmt::{self, Debug, Display};

#[derive(Debug)]
pub struct InternalError {
    kind: Context<InternalErrorKind>,
}

#[derive(Debug, PartialEq, Eq, Clone, Display)]
pub enum InternalErrorKind {
    /// An arithmetic overflow occurs during capacity calculation,
    /// e.g. `Capacity::safe_add`
    CapacityOverflow,

    /// Persistent data had corrupted
    DataCorrupted,

    /// Database exception
    Database,

    /// VM internal error
    VM,

    /// Unknown system error
    System,

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

impl fmt::Display for InternalError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(cause) = self.cause() {
            write!(f, "{}({})", self.kind(), cause)
        } else {
            write!(f, "{}", self.kind())
        }
    }
}

impl From<InternalError> for Error {
    fn from(error: InternalError) -> Self {
        error.context(ErrorKind::Internal).into()
    }
}

impl From<InternalErrorKind> for InternalError {
    fn from(kind: InternalErrorKind) -> Self {
        InternalError {
            kind: Context::new(kind),
        }
    }
}

impl From<InternalErrorKind> for Error {
    fn from(kind: InternalErrorKind) -> Self {
        Into::<InternalError>::into(kind).into()
    }
}

impl InternalErrorKind {
    pub fn cause<F: Fail>(self, cause: F) -> InternalError {
        InternalError {
            kind: cause.context(self),
        }
    }

    pub fn reason<S: Display + Debug + Sync + Send + 'static>(self, reason: S) -> InternalError {
        InternalError {
            kind: err_msg(reason).compat().context(self),
        }
    }
}

impl InternalError {
    pub fn kind(&self) -> &InternalErrorKind {
        &self.kind.get_context()
    }
}

impl Fail for InternalError {
    fn cause(&self) -> Option<&dyn Fail> {
        self.kind.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.kind.backtrace()
    }
}