Skip to main content

whatsapp_rust/socket/
error.rs

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