1use alloc::string::String;
2use alloc::string::ToString;
3use bitcoin::consensus::encode::Error as BitcoinError;
4use core::fmt::{Display, Formatter};
5use serde_bolt::bitcoin;
6
7#[derive(Debug, Clone, PartialEq)]
9pub enum Error {
10 UnexpectedType(u16),
11 BadFraming,
12 Bitcoin(String),
14 TrailingBytes(usize, u16),
16 UnknownMessageType(u16, usize),
20 ShortRead,
21 MessageTooLarge,
22 Eof,
23 Io(String),
24 DeveloperField,
25}
26
27impl From<BitcoinError> for Error {
29 fn from(e: BitcoinError) -> Self {
30 Error::Bitcoin(e.to_string())
31 }
32}
33
34impl From<serde_bolt::io::Error> for Error {
35 fn from(e: serde_bolt::io::Error) -> Self {
36 Error::Io(e.to_string())
37 }
38}
39
40pub type Result<T> = core::result::Result<T, Error>;
42
43impl Display for Error {
44 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
45 match self {
46 Error::UnexpectedType(t) => write!(f, "unexpected message type #{}", t),
47 Error::BadFraming => write!(f, "bad framing"),
48 Error::Bitcoin(e) => write!(f, "bitcoin consensus decode error: {}", e),
49 Error::TrailingBytes(n, t) => {
50 write!(f, "{} trailing bytes after message #{}", n, t)
51 }
52 Error::UnknownMessageType(t, body_len) => {
53 write!(f, "UNHANDLED MESSAGE #{} ({} body bytes)", t, body_len)
54 }
55 Error::ShortRead => write!(f, "short read"),
56 Error::MessageTooLarge => write!(f, "message too large"),
57 Error::Eof => write!(f, "unexpected EOF"),
58 Error::Io(e) => write!(f, "I/O error: {}", e),
59 Error::DeveloperField => write!(f, "developer field not allowed"),
60 }
61 }
62}
63
64#[cfg(feature = "std")]
65impl std::error::Error for Error {}