iroh_blobs/get/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! Error returned from get operations
use std::io;

use iroh::endpoint::{self, ClosedStream};
use n0_snafu::SpanTrace;
use nested_enum_utils::common_fields;
use quinn::{ConnectionError, ReadError, WriteError};
use snafu::{Backtrace, IntoError, Snafu};

use crate::{
    api::ExportBaoError,
    get::fsm::{AtBlobHeaderNextError, ConnectedNextError, DecodeError},
};

#[derive(Debug, Snafu)]
pub enum NotFoundCases {
    #[snafu(transparent)]
    AtBlobHeaderNext { source: AtBlobHeaderNextError },
    #[snafu(transparent)]
    Decode { source: DecodeError },
}

#[derive(Debug, Snafu)]
pub enum NoncompliantNodeCases {
    #[snafu(transparent)]
    Connection { source: ConnectionError },
    #[snafu(transparent)]
    Decode { source: DecodeError },
}

#[derive(Debug, Snafu)]
pub enum RemoteResetCases {
    #[snafu(transparent)]
    Read { source: ReadError },
    #[snafu(transparent)]
    Write { source: WriteError },
    #[snafu(transparent)]
    Connection { source: ConnectionError },
}

#[derive(Debug, Snafu)]
pub enum BadRequestCases {
    #[snafu(transparent)]
    Anyhow { source: anyhow::Error },
    #[snafu(transparent)]
    Postcard { source: postcard::Error },
    #[snafu(transparent)]
    ConnectedNext { source: ConnectedNextError },
}

#[derive(Debug, Snafu)]
pub enum LocalFailureCases {
    #[snafu(transparent)]
    Io {
        source: io::Error,
    },
    #[snafu(transparent)]
    Anyhow {
        source: anyhow::Error,
    },
    #[snafu(transparent)]
    IrpcSend {
        source: irpc::channel::SendError,
    },
    #[snafu(transparent)]
    Irpc {
        source: irpc::Error,
    },
    #[snafu(transparent)]
    ExportBao {
        source: ExportBaoError,
    },
    TokioSend {},
}

impl<T> From<tokio::sync::mpsc::error::SendError<T>> for LocalFailureCases {
    fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self {
        LocalFailureCases::TokioSend {}
    }
}

#[derive(Debug, Snafu)]
pub enum IoCases {
    #[snafu(transparent)]
    Io { source: io::Error },
    #[snafu(transparent)]
    ConnectionError { source: endpoint::ConnectionError },
    #[snafu(transparent)]
    ReadError { source: endpoint::ReadError },
    #[snafu(transparent)]
    WriteError { source: endpoint::WriteError },
    #[snafu(transparent)]
    ClosedStream { source: endpoint::ClosedStream },
    #[snafu(transparent)]
    ConnectedNextError { source: ConnectedNextError },
    #[snafu(transparent)]
    AtBlobHeaderNextError { source: AtBlobHeaderNextError },
}

/// Failures for a get operation
#[common_fields({
    backtrace: Option<Backtrace>,
    #[snafu(implicit)]
    span_trace: SpanTrace,
})]
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum GetError {
    /// Hash not found, or a requested chunk for the hash not found.
    #[snafu(display("Data for hash not found"))]
    NotFound {
        #[snafu(source(from(NotFoundCases, Box::new)))]
        source: Box<NotFoundCases>,
    },
    /// Remote has reset the connection.
    #[snafu(display("Remote has reset the connection"))]
    RemoteReset {
        #[snafu(source(from(RemoteResetCases, Box::new)))]
        source: Box<RemoteResetCases>,
    },
    /// Remote behaved in a non-compliant way.
    #[snafu(display("Remote behaved in a non-compliant way"))]
    NoncompliantNode {
        #[snafu(source(from(NoncompliantNodeCases, Box::new)))]
        source: Box<NoncompliantNodeCases>,
    },

    /// Network or IO operation failed.
    #[snafu(display("A network or IO operation failed"))]
    Io {
        #[snafu(source(from(IoCases, Box::new)))]
        source: Box<IoCases>,
    },
    /// Our download request is invalid.
    #[snafu(display("Our download request is invalid"))]
    BadRequest {
        #[snafu(source(from(BadRequestCases, Box::new)))]
        source: Box<BadRequestCases>,
    },
    /// Operation failed on the local node.
    #[snafu(display("Operation failed on the local node"))]
    LocalFailure {
        #[snafu(source(from(LocalFailureCases, Box::new)))]
        source: Box<LocalFailureCases>,
    },
}

pub type GetResult<T> = std::result::Result<T, GetError>;

impl From<irpc::channel::SendError> for GetError {
    fn from(value: irpc::channel::SendError) -> Self {
        LocalFailureSnafu.into_error(value.into())
    }
}

impl<T: Send + Sync + 'static> From<tokio::sync::mpsc::error::SendError<T>> for GetError {
    fn from(value: tokio::sync::mpsc::error::SendError<T>) -> Self {
        LocalFailureSnafu.into_error(value.into())
    }
}

impl From<endpoint::ConnectionError> for GetError {
    fn from(value: endpoint::ConnectionError) -> Self {
        // explicit match just to be sure we are taking everything into account
        use endpoint::ConnectionError;
        match value {
            e @ ConnectionError::VersionMismatch => {
                // > The peer doesn't implement any supported version
                // unsupported version is likely a long time error, so this peer is not usable
                NoncompliantNodeSnafu.into_error(e.into())
            }
            e @ ConnectionError::TransportError(_) => {
                // > The peer violated the QUIC specification as understood by this implementation
                // bad peer we don't want to keep around
                NoncompliantNodeSnafu.into_error(e.into())
            }
            e @ ConnectionError::ConnectionClosed(_) => {
                // > The peer's QUIC stack aborted the connection automatically
                // peer might be disconnecting or otherwise unavailable, drop it
                IoSnafu.into_error(e.into())
            }
            e @ ConnectionError::ApplicationClosed(_) => {
                // > The peer closed the connection
                // peer might be disconnecting or otherwise unavailable, drop it
                IoSnafu.into_error(e.into())
            }
            e @ ConnectionError::Reset => {
                // > The peer is unable to continue processing this connection, usually due to having restarted
                RemoteResetSnafu.into_error(e.into())
            }
            e @ ConnectionError::TimedOut => {
                // > Communication with the peer has lapsed for longer than the negotiated idle timeout
                IoSnafu.into_error(e.into())
            }
            e @ ConnectionError::LocallyClosed => {
                // > The local application closed the connection
                // TODO(@divma): don't see how this is reachable but let's just not use the peer
                IoSnafu.into_error(e.into())
            }
            e @ ConnectionError::CidsExhausted => {
                // > The connection could not be created because not enough of the CID space
                // > is available
                IoSnafu.into_error(e.into())
            }
        }
    }
}

impl From<endpoint::ReadError> for GetError {
    fn from(value: endpoint::ReadError) -> Self {
        use endpoint::ReadError;
        match value {
            e @ ReadError::Reset(_) => RemoteResetSnafu.into_error(e.into()),
            ReadError::ConnectionLost(conn_error) => conn_error.into(),
            ReadError::ClosedStream
            | ReadError::IllegalOrderedRead
            | ReadError::ZeroRttRejected => {
                // all these errors indicate the peer is not usable at this moment
                IoSnafu.into_error(value.into())
            }
        }
    }
}
impl From<ClosedStream> for GetError {
    fn from(value: ClosedStream) -> Self {
        IoSnafu.into_error(value.into())
    }
}

impl From<quinn::WriteError> for GetError {
    fn from(value: quinn::WriteError) -> Self {
        use quinn::WriteError;
        match value {
            e @ WriteError::Stopped(_) => RemoteResetSnafu.into_error(e.into()),
            WriteError::ConnectionLost(conn_error) => conn_error.into(),
            WriteError::ClosedStream | WriteError::ZeroRttRejected => {
                // all these errors indicate the peer is not usable at this moment
                IoSnafu.into_error(value.into())
            }
        }
    }
}

impl From<crate::get::fsm::ConnectedNextError> for GetError {
    fn from(value: crate::get::fsm::ConnectedNextError) -> Self {
        use crate::get::fsm::ConnectedNextError::*;
        match value {
            e @ PostcardSer { .. } => {
                // serialization errors indicate something wrong with the request itself
                BadRequestSnafu.into_error(e.into())
            }
            e @ RequestTooBig { .. } => {
                // request will never be sent, drop it
                BadRequestSnafu.into_error(e.into())
            }
            Write { source, .. } => source.into(),
            Closed { source, .. } => source.into(),
            e @ Io { .. } => {
                // io errors are likely recoverable
                IoSnafu.into_error(e.into())
            }
        }
    }
}

impl From<crate::get::fsm::AtBlobHeaderNextError> for GetError {
    fn from(value: crate::get::fsm::AtBlobHeaderNextError) -> Self {
        use crate::get::fsm::AtBlobHeaderNextError::*;
        match value {
            e @ NotFound { .. } => {
                // > This indicates that the provider does not have the requested data.
                // peer might have the data later, simply retry it
                NotFoundSnafu.into_error(e.into())
            }
            EndpointRead { source, .. } => source.into(),
            e @ Io { .. } => {
                // io errors are likely recoverable
                IoSnafu.into_error(e.into())
            }
        }
    }
}

impl From<crate::get::fsm::DecodeError> for GetError {
    fn from(value: crate::get::fsm::DecodeError) -> Self {
        use crate::get::fsm::DecodeError::*;

        match value {
            e @ ChunkNotFound { .. } => NotFoundSnafu.into_error(e.into()),
            e @ ParentNotFound { .. } => NotFoundSnafu.into_error(e.into()),
            e @ LeafNotFound { .. } => NotFoundSnafu.into_error(e.into()),
            e @ ParentHashMismatch { .. } => {
                // TODO(@divma): did the peer sent wrong data? is it corrupted? did we sent a wrong
                // request?
                NoncompliantNodeSnafu.into_error(e.into())
            }
            e @ LeafHashMismatch { .. } => {
                // TODO(@divma): did the peer sent wrong data? is it corrupted? did we sent a wrong
                // request?
                NoncompliantNodeSnafu.into_error(e.into())
            }
            Read { source, .. } => source.into(),
            DecodeIo { source, .. } => source.into(),
        }
    }
}

impl From<std::io::Error> for GetError {
    fn from(value: std::io::Error) -> Self {
        // generally consider io errors recoverable
        // we might want to revisit this at some point
        IoSnafu.into_error(value.into())
    }
}