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 /// The same request-response protocol has been registered multiple times.
66 #[error("Request-response protocol registered multiple times: {protocol}")]
67 DuplicateRequestResponseProtocol {
68 /// Name of the protocol registered multiple times.
69 protocol: ProtocolName,
70 },
71 /// Peer does not exist.
72 #[error("Peer `{0}` does not exist.")]
73 PeerDoesntExist(PeerId),
74 /// Channel closed.
75 #[error("Channel closed")]
76 ChannelClosed,
77 /// Connection closed.
78 #[error("Connection closed")]
79 ConnectionClosed,
80 /// Litep2p error.
81 #[error("Litep2p error: `{0}`")]
82 Litep2p(litep2p::Error),
83}
84
85// Make `Debug` use the `Display` implementation.
86impl fmt::Debug for Error {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 fmt::Display::fmt(self, f)
89 }
90}