sc_network/error.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Substrate network possible errors.
20
21use crate::{config::TransportConfig, types::ProtocolName};
22
23use sc_network_types::{multiaddr::Multiaddr, PeerId};
24
25use std::fmt;
26
27/// Result type alias for the network.
28pub type Result<T> = std::result::Result<T, Error>;
29
30/// Error type for the network.
31#[derive(thiserror::Error)]
32pub enum Error {
33 /// Io error
34 #[error(transparent)]
35 Io(#[from] std::io::Error),
36
37 /// Client error
38 #[error(transparent)]
39 Client(#[from] Box<sp_blockchain::Error>),
40 /// The same bootnode (based on address) is registered with two different peer ids.
41 #[error(
42 "The same bootnode (`{address}`) is registered with two different peer ids: `{first_id}` and `{second_id}`"
43 )]
44 DuplicateBootnode {
45 /// The address of the bootnode.
46 address: Multiaddr,
47 /// The first peer id that was found for the bootnode.
48 first_id: PeerId,
49 /// The second peer id that was found for the bootnode.
50 second_id: PeerId,
51 },
52 /// Prometheus metrics error.
53 #[error(transparent)]
54 Prometheus(#[from] prometheus_endpoint::PrometheusError),
55 /// The network addresses are invalid because they don't match the transport.
56 #[error(
57 "The following addresses are invalid because they don't match the transport: {addresses:?}"
58 )]
59 AddressesForAnotherTransport {
60 /// Transport used.
61 transport: TransportConfig,
62 /// The invalid addresses.
63 addresses: Vec<Multiaddr>,
64 },
65 /// A configured address carries an identity component the node key does not produce.
66 #[error(
67 "Address `{address}` carries `{configured}`, but {node_key_origin} yields `{expected}`."
68 )]
69 MismatchedAddressIdentity {
70 /// The address as configured.
71 address: Multiaddr,
72 /// The component found in the address, rendered as `/p2p/…` or `/certhash/…`.
73 configured: String,
74 /// The component the node key yields.
75 expected: String,
76 /// Where the node key it was compared against comes from.
77 node_key_origin: String,
78 },
79 /// A configured address carries an identity component where none belongs.
80 #[error("Address `{address}` is malformed at `{component}`.")]
81 MalformedAddressIdentity {
82 /// The address as configured.
83 address: Multiaddr,
84 /// The component that doesn't belong, rendered as `/p2p/…` or `/certhash/…`.
85 component: String,
86 },
87 /// A configured address carries an identity component, but there is no node key to check it
88 /// against: one generated now could not yield it.
89 #[error(
90 "An address carries `/p2p` or `/certhash`, but there is no node key to check it \
91 against: a key generated now would not yield it."
92 )]
93 AddressIdentityWithoutNodeKey,
94 /// An invalid `webrtc-direct` address.
95 #[error(
96 "Invalid WebRTC address `{address}`: expected `/<host>/udp/<port>/webrtc-direct`, followed \
97 at most by this node's own `/certhash/<hash>` and `/p2p/<peer id>`."
98 )]
99 InvalidWebRtcAddress {
100 /// The invalid address.
101 address: Multiaddr,
102 },
103 /// A public `webrtc-direct` address was configured without a WebRTC transport to back it.
104 #[error(
105 "A WebRTC public address has been configured, but the node does not listen on any \
106 `webrtc-direct` address"
107 )]
108 WebRtcTransportNotConfigured,
109 /// A `webrtc-direct` address was configured on a backend that cannot serve WebRTC.
110 #[error(
111 "A WebRTC address is configured, \
112 but WebRTC is only supported by the litep2p network backend."
113 )]
114 WebRtcNotSupportedByBackend,
115 /// The same request-response protocol has been registered multiple times.
116 #[error("Request-response protocol registered multiple times: {protocol}")]
117 DuplicateRequestResponseProtocol {
118 /// Name of the protocol registered multiple times.
119 protocol: ProtocolName,
120 },
121 /// Peer does not exist.
122 #[error("Peer `{0}` does not exist.")]
123 PeerDoesntExist(PeerId),
124 /// Channel closed.
125 #[error("Channel closed")]
126 ChannelClosed,
127 /// Connection closed.
128 #[error("Connection closed")]
129 ConnectionClosed,
130 /// Litep2p error.
131 #[error("Litep2p error: `{0}`")]
132 Litep2p(litep2p::Error),
133}
134
135// Make `Debug` use the `Display` implementation.
136impl fmt::Debug for Error {
137 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138 fmt::Display::fmt(self, f)
139 }
140}