fatality based errors (#4448)

* seed commit for fatality based errors

* fatality

* first draft of fatality

* cleanup

* differnt approach

* simplify

* first working version for enums, with documentation

* add split

* fix simple split test case

* extend README.md

* update fatality impl

* make tests passed

* apply fatality to first subsystem

* fatality fixes

* use fatality in a subsystem

* fix subsystemg

* fixup proc macro

* fix/test: log::*! do not execute when log handler is missing

* fix spelling

* rename Runtime2 to something sane

* allow nested split with `forward` annotations

* add free license

* enable and fixup all tests

* use external fatality

Makes this more reviewable.

* bump fatality dep

Avoid duplicate expander compilations.

* migrate availability distribution

* more fatality usage

* chore: bump fatality to 0.0.6

* fixup remaining subsystems

* chore: fmt

* make cargo spellcheck happy

* remove single instance of `#[fatal(false)]`

* last quality sweep

* fixup
This commit is contained in:
Bernhard Schuster
2022-02-25 18:25:26 +01:00
committed by GitHub
parent 85fa087405
commit d946582707
48 changed files with 425 additions and 659 deletions
@@ -50,8 +50,9 @@ use polkadot_subsystem::{
overseer, FromOverseer, OverseerSignal, PerLeafSpan, SubsystemContext,
};
use super::{Result, LOG_TARGET};
use crate::error::{log_error, Fatal, FatalResult, NonFatal};
use super::LOG_TARGET;
use crate::error::{log_error, Error, FatalError, Result};
use fatality::Split;
#[cfg(test)]
mod tests;
@@ -762,7 +763,7 @@ where
let statement = runtime
.check_signature(ctx.sender(), relay_parent, statement)
.await?
.map_err(NonFatal::InvalidStatementSignature)?;
.map_err(Error::InvalidStatementSignature)?;
let removed =
state.collation_result_senders.remove(&statement.payload().candidate_hash());
@@ -978,7 +979,7 @@ pub(crate) async fn run<Context>(
collator_pair: CollatorPair,
mut req_receiver: IncomingRequestReceiver<request_v1::CollationFetchingRequest>,
metrics: Metrics,
) -> FatalResult<()>
) -> std::result::Result<(), FatalError>
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
@@ -992,7 +993,7 @@ where
let recv_req = req_receiver.recv(|| vec![COST_INVALID_REQUEST]).fuse();
pin_mut!(recv_req);
select! {
msg = ctx.recv().fuse() => match msg.map_err(Fatal::SubsystemReceive)? {
msg = ctx.recv().fuse() => match msg.map_err(FatalError::SubsystemReceive)? {
FromOverseer::Communication { msg } => {
log_error(
process_msg(&mut ctx, &mut runtime, &mut state, msg).await,
@@ -1032,11 +1033,11 @@ where
"Handling incoming request"
)?;
}
Err(incoming::Error::Fatal(f)) => return Err(f.into()),
Err(incoming::Error::NonFatal(err)) => {
Err(error) => {
let jfyi = error.split().map_err(incoming::Error::from)?;
tracing::debug!(
target: LOG_TARGET,
?err,
error = ?jfyi,
"Decoding incoming request failed"
);
continue
@@ -17,8 +17,6 @@
//! Error handling related code and Error/Result definitions.
use thiserror::Error;
use polkadot_node_network_protocol::request_response::incoming;
use polkadot_node_primitives::UncheckedSignedFullStatement;
use polkadot_node_subsystem_util::runtime;
@@ -28,80 +26,38 @@ use crate::LOG_TARGET;
/// General result.
pub type Result<T> = std::result::Result<T, Error>;
/// Result with only fatal errors.
pub type FatalResult<T> = std::result::Result<T, Fatal>;
/// Errors for statement distribution.
#[derive(Debug, Error, derive_more::From)]
#[error(transparent)]
use fatality::Nested;
#[allow(missing_docs)]
#[fatality::fatality(splitable)]
pub enum Error {
/// All fatal errors.
Fatal(Fatal),
/// All nonfatal/potentially recoverable errors.
NonFatal(NonFatal),
}
impl From<runtime::Error> for Error {
fn from(o: runtime::Error) -> Self {
match o {
runtime::Error::Fatal(f) => Self::Fatal(Fatal::Runtime(f)),
runtime::Error::NonFatal(f) => Self::NonFatal(NonFatal::Runtime(f)),
}
}
}
impl From<incoming::Error> for Error {
fn from(o: incoming::Error) -> Self {
match o {
incoming::Error::Fatal(f) => Self::Fatal(Fatal::IncomingRequest(f)),
incoming::Error::NonFatal(f) => Self::NonFatal(NonFatal::IncomingRequest(f)),
}
}
}
/// Fatal runtime errors.
#[derive(Debug, Error)]
pub enum Fatal {
/// Receiving subsystem message from overseer failed.
#[fatal]
#[error("Receiving message from overseer failed")]
SubsystemReceive(#[source] SubsystemError),
SubsystemReceive(#[from] SubsystemError),
/// Errors coming from runtime::Runtime.
#[error("Error while accessing runtime information")]
Runtime(#[from] runtime::Fatal),
/// Errors coming from receiving incoming requests.
#[fatal(forward)]
#[error("Retrieving next incoming request failed")]
IncomingRequest(#[from] incoming::Fatal),
}
IncomingRequest(#[from] incoming::Error),
#[fatal(forward)]
#[error("Error while accessing runtime information")]
Runtime(#[from] runtime::Error),
/// Errors for fetching of runtime information.
#[derive(Debug, Error)]
pub enum NonFatal {
/// Signature was invalid on received statement.
#[error("CollationSeconded contained statement with invalid signature")]
InvalidStatementSignature(UncheckedSignedFullStatement),
/// Errors coming from runtime::Runtime.
#[error("Error while accessing runtime information")]
Runtime(#[from] runtime::NonFatal),
/// Errors coming from receiving incoming requests.
#[error("Retrieving next incoming request failed")]
IncomingRequest(#[from] incoming::NonFatal),
}
/// 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) -> FatalResult<()> {
match result {
Err(Error::Fatal(f)) => Err(f),
Err(Error::NonFatal(error)) => {
tracing::warn!(target: LOG_TARGET, error = ?error, ctx);
pub fn log_error(result: Result<()>, ctx: &'static str) -> std::result::Result<(), FatalError> {
match result.into_nested()? {
Ok(()) => Ok(()),
Err(jfyi) => {
tracing::warn!(target: LOG_TARGET, error = ?jfyi, ctx);
Ok(())
},
Ok(()) => Ok(()),
}
}
@@ -17,7 +17,8 @@
//! The Collator Protocol allows collators and validators talk to each other.
//! This subsystem implements both sides of the collator protocol.
#![deny(missing_docs, unused_crate_dependencies)]
#![deny(missing_docs)]
#![deny(unused_crate_dependencies)]
#![recursion_limit = "256"]
use std::time::Duration;
@@ -39,7 +40,6 @@ use polkadot_subsystem::{
};
mod error;
use error::{FatalResult, Result};
mod collator_side;
mod validator_side;
@@ -98,7 +98,7 @@ impl CollatorProtocolSubsystem {
Self { protocol_side }
}
async fn run<Context>(self, ctx: Context) -> FatalResult<()>
async fn run<Context>(self, ctx: Context) -> std::result::Result<(), error::FatalError>
where
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
Context: SubsystemContext<Message = CollatorProtocolMessage>,
@@ -54,9 +54,9 @@ use polkadot_subsystem::{
overseer, FromOverseer, OverseerSignal, PerLeafSpan, SubsystemContext, SubsystemSender,
};
use crate::error::FatalResult;
use crate::error::Result;
use super::{modify_reputation, Result, LOG_TARGET};
use super::{modify_reputation, LOG_TARGET};
#[cfg(test)]
mod tests;
@@ -1132,7 +1132,7 @@ pub(crate) async fn run<Context>(
keystore: SyncCryptoStorePtr,
eviction_policy: crate::CollatorEvictionPolicy,
metrics: Metrics,
) -> FatalResult<()>
) -> std::result::Result<(), crate::error::FatalError>
where
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
Context: SubsystemContext<Message = CollatorProtocolMessage>,