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
use crate::peer_manager::PeerManagerError;
use std::io;
use thiserror::Error;
#[derive(Debug, Error)]
#[error(transparent)]
pub struct NetworkError(anyhow::Error);
#[derive(Copy, Clone, Eq, PartialEq, Debug, Error)]
pub enum NetworkErrorKind {
#[error("IO error")]
IoError,
#[error("Bcs error")]
BcsError,
#[error("PeerManager error")]
PeerManagerError,
#[error("Peer not connected")]
NotConnected,
}
impl From<NetworkErrorKind> for NetworkError {
fn from(kind: NetworkErrorKind) -> NetworkError {
NetworkError(anyhow::Error::new(kind))
}
}
impl From<anyhow::Error> for NetworkError {
fn from(err: anyhow::Error) -> NetworkError {
NetworkError(err)
}
}
impl From<io::Error> for NetworkError {
fn from(err: io::Error) -> NetworkError {
anyhow::Error::new(err)
.context(NetworkErrorKind::IoError)
.into()
}
}
impl From<bcs::Error> for NetworkError {
fn from(err: bcs::Error) -> NetworkError {
anyhow::Error::new(err)
.context(NetworkErrorKind::BcsError)
.into()
}
}
impl From<PeerManagerError> for NetworkError {
fn from(err: PeerManagerError) -> NetworkError {
match err {
PeerManagerError::IoError(_) => anyhow::Error::new(err)
.context(NetworkErrorKind::IoError)
.into(),
PeerManagerError::NotConnected(_) => anyhow::Error::new(err)
.context(NetworkErrorKind::NotConnected)
.into(),
err => anyhow::Error::new(err)
.context(NetworkErrorKind::PeerManagerError)
.into(),
}
}
}