referrerpolicy=no-referrer-when-downgrade
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
//

//! Error handling related code and Error/Result definitions.

use fatality::Nested;
use polkadot_node_network_protocol::request_response::outgoing::RequestError;
use polkadot_primitives::SessionIndex;

use futures::channel::oneshot;

use polkadot_node_subsystem::{ChainApiError, SubsystemError};
use polkadot_node_subsystem_util::runtime;

use crate::LOG_TARGET;

#[allow(missing_docs)]
#[fatality::fatality(splitable)]
pub enum Error {
	#[fatal]
	#[error("Spawning subsystem task failed: {0}")]
	SpawnTask(#[source] SubsystemError),

	#[fatal]
	#[error("Erasure chunk requester stream exhausted")]
	RequesterExhausted,

	#[fatal]
	#[error("Receive channel closed: {0}")]
	IncomingMessageChannel(#[source] SubsystemError),

	#[fatal(forward)]
	#[error("Error while accessing runtime information: {0}")]
	Runtime(#[from] runtime::Error),

	#[fatal]
	#[error("Oneshot for receiving response from Chain API got cancelled")]
	ChainApiSenderDropped(#[from] oneshot::Canceled),

	#[fatal]
	#[error("Retrieving response from Chain API unexpectedly failed with error: {0}")]
	ChainApi(#[from] ChainApiError),

	// av-store will drop the sender on any error that happens.
	#[error("Response channel to obtain chunk failed")]
	QueryChunkResponseChannel(#[source] oneshot::Canceled),

	// av-store will drop the sender on any error that happens.
	#[error("Response channel to obtain available data failed")]
	QueryAvailableDataResponseChannel(#[source] oneshot::Canceled),

	// We tried accessing a session that was not cached.
	#[error("Session {missing_session} is not cached, cached sessions: {available_sessions:?}.")]
	NoSuchCachedSession { available_sessions: Vec<SessionIndex>, missing_session: SessionIndex },

	// Sending request response failed (Can happen on timeouts for example).
	#[error("Sending a request's response failed.")]
	SendResponse,

	#[error("FetchPoV request error: {0}")]
	FetchPoV(#[source] RequestError),

	#[error("Fetched PoV does not match expected hash")]
	UnexpectedPoV,

	#[error("Remote responded with `NoSuchPoV`")]
	NoSuchPoV,

	#[error("Given validator index could not be found in current session")]
	InvalidValidatorIndex,

	#[error("Erasure coding error: {0}")]
	ErasureCoding(#[from] polkadot_erasure_coding::Error),
}

/// General result abbreviation type alias.
pub type Result<T> = std::result::Result<T, Error>;

/// Utility for eating top level errors and log them.
///
/// We basically always want to try and continue on error. This utility function is meant to
/// consume top-level errors by simply logging them
pub fn log_error(
	result: Result<()>,
	ctx: &'static str,
	warn_freq: &mut gum::Freq,
) -> std::result::Result<(), FatalError> {
	match result.into_nested()? {
		Ok(()) => Ok(()),
		Err(jfyi) => {
			match jfyi {
				JfyiError::UnexpectedPoV |
				JfyiError::InvalidValidatorIndex |
				JfyiError::NoSuchCachedSession { .. } |
				JfyiError::QueryAvailableDataResponseChannel(_) |
				JfyiError::QueryChunkResponseChannel(_) |
				JfyiError::ErasureCoding(_) => gum::warn!(target: LOG_TARGET, error = %jfyi, ctx),
				JfyiError::FetchPoV(_) |
				JfyiError::SendResponse |
				JfyiError::NoSuchPoV |
				JfyiError::Runtime(_) =>
					gum::warn_if_frequent!(freq: warn_freq, max_rate: gum::Times::PerHour(100), target: LOG_TARGET, error = ?jfyi, ctx),
			}
			Ok(())
		},
	}
}