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