use crate::{ArithmeticError, TokenError};
use codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Eq, Clone, Copy, Encode, Decode, Debug, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ModuleError {
pub index: u8,
pub error: u8,
#[codec(skip)]
#[cfg_attr(feature = "serde", serde(skip_deserializing))]
pub message: Option<&'static str>,
}
impl PartialEq for ModuleError {
fn eq(&self, other: &Self) -> bool {
(self.index == other.index) && (self.error == other.error)
}
}
#[derive(Eq, Clone, Copy, Encode, Decode, Debug, TypeInfo, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DispatchError {
Other(
#[codec(skip)]
#[cfg_attr(feature = "serde", serde(skip_deserializing))]
&'static str,
),
CannotLookup,
BadOrigin,
Module(ModuleError),
ConsumerRemaining,
NoProviders,
TooManyConsumers,
Token(TokenError),
Arithmetic(ArithmeticError),
}
pub type DispatchOutcome = Result<(), DispatchError>;
pub type ApplyExtrinsicResult =
Result<DispatchOutcome, crate::transaction_validity::TransactionValidityError>;
pub fn convert_to_latest(old: ApplyExtrinsicResult) -> crate::ApplyExtrinsicResult {
old.map(|outcome| {
outcome.map_err(|e| match e {
DispatchError::Other(s) => crate::DispatchError::Other(s),
DispatchError::CannotLookup => crate::DispatchError::CannotLookup,
DispatchError::BadOrigin => crate::DispatchError::BadOrigin,
DispatchError::Module(err) => crate::DispatchError::Module(crate::ModuleError {
index: err.index,
error: [err.error, 0, 0, 0],
message: err.message,
}),
DispatchError::ConsumerRemaining => crate::DispatchError::ConsumerRemaining,
DispatchError::NoProviders => crate::DispatchError::NoProviders,
DispatchError::TooManyConsumers => crate::DispatchError::TooManyConsumers,
DispatchError::Token(err) => crate::DispatchError::Token(err),
DispatchError::Arithmetic(err) => crate::DispatchError::Arithmetic(err),
})
})
}