ckb_script_ipc_common/
error.rs

1use ckb_rust_std::io::Error as CoreIoError;
2use ckb_std::error::SysError;
3use core::fmt::{self, Debug, Display};
4use enumn::N;
5
6// use core::error::Error when Rust 1.81 is used.
7pub trait Error: Debug + Display {
8    fn source(&self) -> Option<&(dyn Error + 'static)> {
9        None
10    }
11}
12
13#[derive(Debug, Clone)]
14pub enum IpcError {
15    CkbSysError(SysError),
16    UnexpectedEof,
17    IncompleteVlqSeq,
18    DecodeVlqOverflow,
19    ReadVlqError,
20    SerializeError,
21    DeserializeError,
22    SliceWriteError,
23    ReadUntilError,
24    ReadExactError,
25    BufReaderError,
26    GeneralIoError,
27    ProtocolError(ProtocolErrorCode),
28}
29
30impl Display for IpcError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "{self:?}")
33    }
34}
35
36impl Error for IpcError {
37    fn source(&self) -> Option<&(dyn Error + 'static)> {
38        None
39    }
40}
41
42impl From<CoreIoError> for IpcError {
43    fn from(_: CoreIoError) -> Self {
44        // TODO
45        IpcError::GeneralIoError
46    }
47}
48
49/// Protocol error code used in wire protocol.
50/// Its range from 1 to 2^64 - 1.
51/// 1~20 are with same values used in syscall error.
52#[derive(Debug, Clone, N)]
53#[repr(u64)]
54pub enum ProtocolErrorCode {
55    Ok = 0,
56    /// Index out of bound
57    IndexOutOfBound = 1,
58    /// Field is missing for the target
59    ItemMissing = 2,
60    /// Buffer length is not enough, error contains actual data length
61    LengthNotEnough = 3,
62    /// Data encoding error(molecule)
63    InvalidData = 4,
64    /// Failed to wait.
65    WaitFailure = 5,
66    /// Invalid file descriptor.
67    InvalidFd = 6,
68    /// Reading from or writing to file descriptor failed due to other end closed.
69    OtherEndClosed = 7,
70    /// Max vms has been spawned.
71    MaxVmsSpawned = 8,
72    /// Max fds has been spawned.
73    MaxFdsCreated = 9,
74
75    /// Unknown error code
76    UnknownError = 20,
77    /// Unknown error from SysError in ckb-std
78    UnknownSysError = 21,
79    /// Unexpected EOF
80    UnexpectedEof = 22,
81    /// VLQ error: incomplete VLQ sequence
82    IncompleteVlqSeq = 23,
83    /// VLQ error: decoding overflow
84    DecodeVlqOverflow = 24,
85    /// VLQ error: reading error
86    ReadVlqError = 25,
87    /// Serialize error
88    SerializeError = 26,
89    /// Deserialize error
90    DeserializeError = 27,
91    /// general IO error
92    GeneralIoError = 28,
93
94    // increase when appending new error codes
95    EndOfError = 29,
96}
97
98impl From<IpcError> for ProtocolErrorCode {
99    fn from(err: IpcError) -> Self {
100        match err {
101            IpcError::CkbSysError(err) => match err {
102                SysError::IndexOutOfBound => ProtocolErrorCode::IndexOutOfBound,
103                SysError::ItemMissing => ProtocolErrorCode::ItemMissing,
104                SysError::LengthNotEnough(_) => ProtocolErrorCode::LengthNotEnough,
105                SysError::Encoding => ProtocolErrorCode::InvalidData,
106                SysError::WaitFailure => ProtocolErrorCode::WaitFailure,
107                SysError::InvalidFd => ProtocolErrorCode::InvalidFd,
108                SysError::OtherEndClosed => ProtocolErrorCode::OtherEndClosed,
109                SysError::MaxVmsSpawned => ProtocolErrorCode::MaxVmsSpawned,
110                SysError::MaxFdsCreated => ProtocolErrorCode::MaxFdsCreated,
111                _ => ProtocolErrorCode::UnknownSysError,
112            },
113            IpcError::UnexpectedEof => ProtocolErrorCode::UnexpectedEof,
114            IpcError::IncompleteVlqSeq => ProtocolErrorCode::IncompleteVlqSeq,
115            IpcError::DecodeVlqOverflow => ProtocolErrorCode::DecodeVlqOverflow,
116            IpcError::ReadVlqError => ProtocolErrorCode::ReadVlqError,
117            IpcError::SerializeError => ProtocolErrorCode::SerializeError,
118            IpcError::DeserializeError => ProtocolErrorCode::DeserializeError,
119            IpcError::SliceWriteError
120            | IpcError::BufReaderError
121            | IpcError::ReadUntilError
122            | IpcError::ReadExactError => ProtocolErrorCode::GeneralIoError,
123            IpcError::GeneralIoError => ProtocolErrorCode::GeneralIoError,
124            IpcError::ProtocolError(e) => e,
125        }
126    }
127}
128
129impl From<u64> for ProtocolErrorCode {
130    fn from(e: u64) -> Self {
131        Self::n(e).unwrap()
132    }
133}