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 /// An invalid `webrtc-direct` address.
66 #[error(
67 "Invalid WebRTC address `{address}`: expected `/<host>/udp/<port>/webrtc-direct` and \
68 nothing after it."
69 )]
70 InvalidWebRtcAddress {
71 /// The invalid address.
72 address: Multiaddr,
73 },
74 /// A public `webrtc-direct` address was configured without a WebRTC transport to back it.
75 #[error(
76 "A WebRTC public address has been configured, but the node does not listen on any \
77 `webrtc-direct` address"
78 )]
79 WebRtcTransportNotConfigured,
80 /// A `webrtc-direct` address was configured on a backend that cannot serve WebRTC.
81 #[error(
82 "A WebRTC address is configured, \
83 but WebRTC is only supported by the litep2p network backend."
84 )]
85 WebRtcNotSupportedByBackend,
86 /// The same request-response protocol has been registered multiple times.
87 #[error("Request-response protocol registered multiple times: {protocol}")]
88 DuplicateRequestResponseProtocol {
89 /// Name of the protocol registered multiple times.
90 protocol: ProtocolName,
91 },
92 /// Peer does not exist.
93 #[error("Peer `{0}` does not exist.")]
94 PeerDoesntExist(PeerId),
95 /// Channel closed.
96 #[error("Channel closed")]
97 ChannelClosed,
98 /// Connection closed.
99 #[error("Connection closed")]
100 ConnectionClosed,
101 /// Litep2p error.
102 #[error("Litep2p error: `{0}`")]
103 Litep2p(litep2p::Error),
104}
105
106// Make `Debug` use the `Display` implementation.
107impl fmt::Debug for Error {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 fmt::Display::fmt(self, f)
110 }
111}