referrerpolicy=no-referrer-when-downgrade

polkadot_node_subsystem_types/
errors.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Error types for the subsystem requests.
18
19use ::orchestra::OrchestraError as OverseerError;
20use fatality::fatality;
21
22/// A description of an error causing the runtime API request to be unservable.
23#[derive(thiserror::Error, Debug, Clone)]
24pub enum RuntimeApiError {
25	/// The runtime API cannot be executed due to a runtime error.
26	#[error("The runtime API '{runtime_api_name}' cannot be executed: {source}")]
27	Execution {
28		/// The runtime API being called
29		runtime_api_name: &'static str,
30		/// The wrapped error. Marked as source for tracking the error chain.
31		#[source]
32		source: std::sync::Arc<dyn 'static + std::error::Error + Send + Sync>,
33	},
34
35	/// The runtime API request in question cannot be executed because the runtime at the requested
36	/// relay-parent is an old version.
37	#[error("The API is not supported by the runtime at the relay-parent")]
38	NotSupported {
39		/// The runtime API being called
40		runtime_api_name: &'static str,
41	},
42}
43
44/// A description of an error causing the chain API request to be unservable.
45#[derive(Debug, Clone)]
46pub struct ChainApiError {
47	msg: String,
48}
49
50impl From<&str> for ChainApiError {
51	fn from(s: &str) -> Self {
52		s.to_owned().into()
53	}
54}
55
56impl From<String> for ChainApiError {
57	fn from(msg: String) -> Self {
58		Self { msg }
59	}
60}
61
62impl core::fmt::Display for ChainApiError {
63	fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
64		write!(f, "{}", self.msg)
65	}
66}
67
68impl std::error::Error for ChainApiError {}
69
70/// An error that may happen during Availability Recovery process.
71#[derive(PartialEq, Clone)]
72#[fatality(splitable)]
73#[allow(missing_docs)]
74pub enum RecoveryError {
75	#[error("Invalid data")]
76	Invalid,
77
78	#[error("Data is unavailable")]
79	Unavailable,
80
81	#[fatal]
82	#[error("Erasure task channel closed")]
83	ChannelClosed,
84}
85
86/// An error type that describes faults that may happen
87///
88/// These are:
89///   * Channels being closed
90///   * Subsystems dying when they are not expected to
91///   * Subsystems not dying when they are told to die
92///   * etc.
93#[derive(thiserror::Error, Debug)]
94#[allow(missing_docs)]
95pub enum SubsystemError {
96	#[error(transparent)]
97	NotifyCancellation(#[from] futures::channel::oneshot::Canceled),
98
99	#[error(transparent)]
100	QueueError(#[from] futures::channel::mpsc::SendError),
101
102	#[error(transparent)]
103	Io(#[from] std::io::Error),
104
105	#[error(transparent)]
106	Infallible(#[from] std::convert::Infallible),
107
108	#[error(transparent)]
109	Prometheus(#[from] prometheus_endpoint::PrometheusError),
110
111	#[error("Failed to {0}")]
112	Context(String),
113
114	#[error("Subsystem stalled: {0}")]
115	SubsystemStalled(&'static str),
116
117	/// Generated by the `#[overseer(..)]` proc-macro
118	#[error(transparent)]
119	Generated(#[from] OverseerError),
120
121	/// Per origin (or subsystem) annotations to wrap an error.
122	#[error("Error originated in {origin}")]
123	FromOrigin {
124		/// An additional annotation tag for the origin of `source`.
125		origin: &'static str,
126		/// The wrapped error. Marked as source for tracking the error chain.
127		#[source]
128		source: Box<dyn 'static + std::error::Error + Send + Sync>,
129	},
130}
131
132// impl AnnotateErrorOrigin for SubsystemError {
133// 	fn with_origin(self, origin: &'static str) -> Self {
134// 		Self::FromOrigin {
135// 			origin,
136// 			source: Box::new(self),
137// 		}
138// 	}
139// }
140
141impl SubsystemError {
142	/// Adds a `str` as `origin` to the given error `err`.
143	pub fn with_origin<E: 'static + Send + Sync + std::error::Error>(
144		origin: &'static str,
145		err: E,
146	) -> Self {
147		Self::FromOrigin { origin, source: Box::new(err) }
148	}
149}
150
151/// Ease the use of subsystem errors.
152pub type SubsystemResult<T> = Result<T, self::SubsystemError>;