whatsapp_rust/socket/
error.rs

1use thiserror::Error;
2use tokio::task::JoinError;
3
4#[derive(Debug, Error)]
5pub enum SocketError {
6    #[error("Socket is closed")]
7    SocketClosed,
8    #[error("Noise handshake failed: {0}")]
9    NoiseHandshake(String),
10    #[error("I/O error: {0}")]
11    Io(#[from] std::io::Error),
12    #[error("Crypto error: {0}")]
13    Crypto(String),
14}
15
16pub type Result<T> = std::result::Result<T, SocketError>;
17
18#[derive(Debug, thiserror::Error)]
19pub enum EncryptSendErrorKind {
20    #[error("cryptography error")]
21    Crypto,
22    #[error("framing error")]
23    Framing,
24    #[error("transport error")]
25    Transport,
26    #[error("tokio join error")]
27    Join,
28    #[error("sender channel closed")]
29    ChannelClosed,
30}
31
32#[derive(Debug, thiserror::Error)]
33#[error("{kind}")]
34pub struct EncryptSendError {
35    pub kind: EncryptSendErrorKind,
36    #[source]
37    pub source: anyhow::Error,
38    pub plaintext_buf: Vec<u8>,
39    pub out_buf: Vec<u8>,
40}
41
42impl EncryptSendError {
43    pub fn crypto(
44        source: impl Into<anyhow::Error>,
45        plaintext_buf: Vec<u8>,
46        out_buf: Vec<u8>,
47    ) -> Self {
48        Self {
49            kind: EncryptSendErrorKind::Crypto,
50            source: source.into(),
51            plaintext_buf,
52            out_buf,
53        }
54    }
55
56    pub fn framing(
57        source: impl Into<anyhow::Error>,
58        plaintext_buf: Vec<u8>,
59        out_buf: Vec<u8>,
60    ) -> Self {
61        Self {
62            kind: EncryptSendErrorKind::Framing,
63            source: source.into(),
64            plaintext_buf,
65            out_buf,
66        }
67    }
68
69    pub fn transport(
70        source: impl Into<anyhow::Error>,
71        plaintext_buf: Vec<u8>,
72        out_buf: Vec<u8>,
73    ) -> Self {
74        Self {
75            kind: EncryptSendErrorKind::Transport,
76            source: source.into(),
77            plaintext_buf,
78            out_buf,
79        }
80    }
81
82    pub fn join(source: JoinError, plaintext_buf: Vec<u8>, out_buf: Vec<u8>) -> Self {
83        Self {
84            kind: EncryptSendErrorKind::Join,
85            source: source.into(),
86            plaintext_buf,
87            out_buf,
88        }
89    }
90
91    pub fn channel_closed(plaintext_buf: Vec<u8>, out_buf: Vec<u8>) -> Self {
92        Self {
93            kind: EncryptSendErrorKind::ChannelClosed,
94            source: anyhow::anyhow!("sender task channel closed unexpectedly"),
95            plaintext_buf,
96            out_buf,
97        }
98    }
99}