use fatality::Nested;
use futures::channel::{mpsc, oneshot};
use polkadot_node_subsystem::{
messages::{StoreAvailableDataError, ValidationFailed},
SubsystemError,
};
use polkadot_node_subsystem_util::Error as UtilError;
use polkadot_primitives::BackedCandidate;
use crate::LOG_TARGET;
pub type Result<T> = std::result::Result<T, Error>;
pub type FatalResult<T> = std::result::Result<T, FatalError>;
#[allow(missing_docs)]
#[fatality::fatality(splitable)]
pub enum Error {
#[error("Candidate is not found")]
CandidateNotFound,
#[error("Signature is invalid")]
InvalidSignature,
#[error("Failed to send candidates {0:?}")]
Send(Vec<BackedCandidate>),
#[error("FetchPoV failed")]
FetchPoV,
#[fatal]
#[error("Failed to spawn background task")]
FailedToSpawnBackgroundTask,
#[error("ValidateFromChainState channel closed before receipt")]
ValidateFromChainState(#[source] oneshot::Canceled),
#[error("StoreAvailableData channel closed before receipt")]
StoreAvailableDataChannel(#[source] oneshot::Canceled),
#[error("a channel was closed before receipt in try_join!")]
JoinMultiple(#[source] oneshot::Canceled),
#[error("Obtaining erasure chunks failed")]
ObtainErasureChunks(#[from] erasure_coding::Error),
#[error(transparent)]
ValidationFailed(#[from] ValidationFailed),
#[fatal]
#[error(transparent)]
BackgroundValidationMpsc(#[from] mpsc::SendError),
#[error(transparent)]
UtilError(#[from] UtilError),
#[error(transparent)]
SubsystemError(#[from] SubsystemError),
#[fatal]
#[error(transparent)]
OverseerExited(SubsystemError),
#[error("Availability store error")]
StoreAvailableData(#[source] StoreAvailableDataError),
}
pub fn log_error(result: Result<()>) -> std::result::Result<(), FatalError> {
match result.into_nested()? {
Ok(()) => Ok(()),
Err(jfyi) => {
jfyi.log();
Ok(())
},
}
}
impl JfyiError {
pub fn log(self) {
gum::debug!(target: LOG_TARGET, error = ?self);
}
}