Skip to main content

asimov_protocol/
errors.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::Message;
4use alloc::boxed::Box;
5use core::error::Error;
6use heapless::CapacityError;
7use known_errors::sysexits::SysexitsError;
8use thiserror::Error;
9
10#[derive(Debug, Error)]
11#[non_exhaustive]
12pub enum AcceptError {
13    #[error(transparent)]
14    Transport(#[from] iroh::protocol::AcceptError),
15
16    #[error(transparent)]
17    Connection(#[from] iroh::endpoint::ConnectionError),
18
19    #[error(transparent)]
20    RecvHello(#[from] RecvError),
21
22    #[error(transparent)]
23    SendHello(#[from] SendError),
24
25    #[error("invalid message from peer")]
26    InvalidMessage(Message),
27}
28
29impl From<AcceptError> for SysexitsError {
30    fn from(_: AcceptError) -> Self {
31        SysexitsError::EX_SOFTWARE // TODO
32    }
33}
34
35#[derive(Debug, Error)]
36#[non_exhaustive]
37pub enum BindError {
38    #[error(transparent)]
39    Transport(#[from] iroh::endpoint::BindError),
40}
41
42impl From<BindError> for SysexitsError {
43    fn from(_: BindError) -> Self {
44        SysexitsError::EX_SOFTWARE // TODO
45    }
46}
47
48#[derive(Debug, Error)]
49#[non_exhaustive]
50pub enum ConnectError {
51    #[error(transparent)]
52    Transport(#[from] iroh::endpoint::ConnectError),
53
54    #[error(transparent)]
55    Connection(#[from] iroh::endpoint::ConnectionError),
56
57    #[error(transparent)]
58    SendHello(#[from] SendError),
59
60    #[error(transparent)]
61    RecvHello(#[from] RecvError),
62
63    #[error("invalid message from peer")]
64    InvalidMessage(Message),
65}
66
67impl From<ConnectError> for SysexitsError {
68    fn from(_: ConnectError) -> Self {
69        SysexitsError::EX_SOFTWARE // TODO
70    }
71}
72
73#[derive(Debug, Error)]
74#[non_exhaustive]
75pub enum PingError {
76    #[error(transparent)]
77    ConnectPeer(#[from] ConnectError),
78
79    #[error(transparent)]
80    SendPing(#[from] SendError),
81
82    #[error(transparent)]
83    RecvPing(#[from] RecvError),
84
85    #[error(transparent)]
86    Other(#[from] Box<dyn Error>),
87}
88
89impl From<PingError> for SysexitsError {
90    fn from(_: PingError) -> Self {
91        SysexitsError::EX_SOFTWARE // TODO
92    }
93}
94
95#[derive(Debug, Error)]
96#[non_exhaustive]
97pub enum PublishError {
98    #[error(transparent)]
99    Gossip(#[from] iroh_gossip::api::ApiError),
100
101    #[error(transparent)]
102    Other(#[from] Box<dyn Error>),
103}
104
105impl From<PublishError> for SysexitsError {
106    fn from(_: PublishError) -> Self {
107        SysexitsError::EX_SOFTWARE // TODO
108    }
109}
110
111#[derive(Debug, Error)]
112#[non_exhaustive]
113pub enum StartError {
114    #[error(transparent)]
115    Other(#[from] Box<dyn Error>),
116}
117
118impl From<StartError> for SysexitsError {
119    fn from(_: StartError) -> Self {
120        SysexitsError::EX_SOFTWARE // TODO
121    }
122}
123
124#[derive(Debug, Error)]
125#[non_exhaustive]
126pub enum SubscribeError {
127    #[error(transparent)]
128    Gossip(#[from] iroh_gossip::api::ApiError),
129
130    #[error(transparent)]
131    Other(#[from] Box<dyn Error>),
132}
133
134impl From<SubscribeError> for SysexitsError {
135    fn from(_: SubscribeError) -> Self {
136        SysexitsError::EX_SOFTWARE // TODO
137    }
138}
139
140#[derive(Debug, Error)]
141#[non_exhaustive]
142pub enum TerminateError {
143    #[error(transparent)]
144    Join(#[from] tokio::task::JoinError),
145}
146
147impl From<TerminateError> for SysexitsError {
148    fn from(_: TerminateError) -> Self {
149        SysexitsError::EX_SOFTWARE // TODO
150    }
151}
152
153#[derive(Debug, Error)]
154#[non_exhaustive]
155pub enum SendError {
156    #[error(transparent)]
157    Serialize(#[from] postcard::Error),
158
159    #[error(transparent)]
160    Transport(#[from] iroh::endpoint::WriteError),
161}
162
163impl From<SendError> for SysexitsError {
164    fn from(_: SendError) -> Self {
165        SysexitsError::EX_SOFTWARE // TODO
166    }
167}
168
169#[derive(Debug, Error)]
170#[non_exhaustive]
171pub enum RecvError {
172    #[error(transparent)]
173    Transport(#[from] iroh::endpoint::ReadExactError),
174
175    #[error(transparent)]
176    Capacity(#[from] CapacityError),
177
178    #[error(transparent)]
179    Deserialize(#[from] postcard::Error),
180}
181
182impl From<RecvError> for SysexitsError {
183    fn from(_: RecvError) -> Self {
184        SysexitsError::EX_SOFTWARE // TODO
185    }
186}