1use crate::IngressECIESValue;
2use std::fmt;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
7pub struct ECIESError {
8 inner: Box<ECIESErrorImpl>,
9}
10
11impl ECIESError {
12 pub fn into_inner(self) -> ECIESErrorImpl {
14 *self.inner
15 }
16
17 pub const fn inner(&self) -> &ECIESErrorImpl {
19 &self.inner
20 }
21}
22
23impl fmt::Display for ECIESError {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 fmt::Display::fmt(&*self.inner, f)
26 }
27}
28
29#[derive(Debug, Error)]
31pub enum ECIESErrorImpl {
32 #[error(transparent)]
34 IO(std::io::Error),
35 #[error("tag check failure in decrypt_message")]
37 TagCheckDecryptFailed,
38 #[error("tag check failure in read_header")]
40 TagCheckHeaderFailed,
41 #[error("tag check failure in read_body")]
43 TagCheckBodyFailed,
44 #[error("invalid auth data")]
46 InvalidAuthData,
47 #[error("invalid ack data")]
49 InvalidAckData,
50 #[error("invalid header")]
52 InvalidHeader,
53 #[error(transparent)]
55 Secp256k1(secp256k1::Error),
56 #[error(transparent)]
58 RLPDecoding(alloy_rlp::Error),
59 #[error(transparent)]
61 FromInt(std::num::TryFromIntError),
62 #[error("encrypted data is not large enough for all fields")]
64 EncryptedDataTooSmall,
65 #[error("initial header body is {body_size} but the max is {max_body_size}")]
67 InitialHeaderBodyTooLarge {
68 body_size: usize,
70 max_body_size: usize,
72 },
73 #[error("frame body is {size} but the max is {max_size}")]
75 FrameTooLarge {
76 size: usize,
78 max_size: usize,
80 },
81 #[error("requested {idx} but array len is {len}")]
83 OutOfBounds {
84 idx: usize,
86 len: usize,
88 },
89 #[error("invalid handshake: expected {expected:?}, got {msg:?} instead")]
91 InvalidHandshake {
92 expected: IngressECIESValue,
94 msg: Option<IngressECIESValue>,
96 },
97 #[error("stream closed due to not being readable")]
104 UnreadableStream,
105 #[error("never received data from remote peer")]
107 StreamTimeout,
108}
109
110impl From<ECIESErrorImpl> for ECIESError {
111 fn from(source: ECIESErrorImpl) -> Self {
112 Self { inner: Box::new(source) }
113 }
114}
115
116impl From<std::io::Error> for ECIESError {
117 fn from(source: std::io::Error) -> Self {
118 ECIESErrorImpl::IO(source).into()
119 }
120}
121
122impl From<secp256k1::Error> for ECIESError {
123 fn from(source: secp256k1::Error) -> Self {
124 ECIESErrorImpl::Secp256k1(source).into()
125 }
126}
127
128impl From<alloy_rlp::Error> for ECIESError {
129 fn from(source: alloy_rlp::Error) -> Self {
130 ECIESErrorImpl::RLPDecoding(source).into()
131 }
132}
133
134impl From<std::num::TryFromIntError> for ECIESError {
135 fn from(source: std::num::TryFromIntError) -> Self {
136 ECIESErrorImpl::FromInt(source).into()
137 }
138}