bitcoin_cash/
error.rs

1use crate::address::CashAddrError;
2use error_chain::error_chain;
3
4#[derive(Clone, Copy, Debug, PartialEq)]
5pub enum BitcoinCodeError {
6    DeserializeAnyNotSupported,
7    InvalidBoolEncoding(u8),
8    LeftoverBytes,
9    DataTypeNotSupported(&'static str),
10    MethodNotSupported(&'static str),
11    SequenceMustHaveLength,
12}
13
14impl BitcoinCodeError {
15    pub fn into_err<T>(self) -> Result<T> {
16        Err(ErrorKind::BitcoinCodeDeserialize(self).into())
17    }
18}
19
20#[derive(Clone, Copy, Debug, PartialEq)]
21pub enum ScriptSerializeError {
22    PushTooLarge,
23    InvalidInteger,
24    UnknownOpcode,
25}
26
27impl ScriptSerializeError {
28    pub fn into_err<T>(self) -> Result<T> {
29        Err(ErrorKind::ScriptSerialize(self).into())
30    }
31}
32
33error_chain! {
34    links {
35        Json(
36            crate::serialize_json::error::Error,
37            crate::serialize_json::error::ErrorKind
38        );
39    }
40
41    foreign_links {
42        FromHex(hex::FromHexError);
43        Io(std::io::Error);
44        Utf8(std::str::Utf8Error);
45        SerdeJson(serde_json::Error);
46    }
47
48    errors {
49        InvalidSize(expected: usize, actual: usize) {
50            description("invalid size")
51            display("invalid size, expected {}, got {}", expected, actual)
52        }
53
54        InvalidCashAddr(err: CashAddrError) {}
55
56        BitcoinCodeDeserialize(err: BitcoinCodeError) {}
57
58        ScriptSerialize(err: ScriptSerializeError) {}
59
60        InsufficientInputAmount(amount: u64) {}
61
62        InvalidSignatureFormat {}
63
64        InvalidPubkey {}
65    }
66}
67
68impl From<bitcoin_cash_base::FromSliceError> for Error {
69    fn from(error: bitcoin_cash_base::FromSliceError) -> Self {
70        ErrorKind::InvalidSize(error.expected, error.actual).into()
71    }
72}