mod dispatch_error;
use core::fmt::Debug;
pub use dispatch_error::{
ArithmeticError, DispatchError, ModuleError, RawModuleError, TokenError, TransactionalError,
};
pub use crate::metadata::Metadata;
pub use scale_decode::Error as DecodeError;
pub use scale_encode::Error as EncodeError;
pub use subxt_metadata::TryFromError as MetadataTryFromError;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("Io error: {0}")]
Io(#[from] std::io::Error),
#[error("Scale codec error: {0}")]
Codec(#[from] codec::Error),
#[error("Rpc error: {0}")]
Rpc(#[from] RpcError),
#[error("Serde json error: {0}")]
Serialization(#[from] serde_json::error::Error),
#[error("Metadata: {0}")]
Metadata(#[from] MetadataError),
#[error("Metadata: {0}")]
MetadataDecoding(#[from] MetadataTryFromError),
#[error("Runtime error: {0:?}")]
Runtime(#[from] DispatchError),
#[error("Error decoding into dynamic value: {0}")]
Decode(#[from] DecodeError),
#[error("Error encoding from dynamic value: {0}")]
Encode(#[from] EncodeError),
#[error("Transaction error: {0}")]
Transaction(#[from] TransactionError),
#[error("Block error: {0}")]
Block(#[from] BlockError),
#[error("Error encoding storage address: {0}")]
StorageAddress(#[from] StorageAddressError),
#[error("An error occurred but it could not be decoded: {0:?}")]
Unknown(Vec<u8>),
#[error("Other error: {0}")]
Other(String),
}
impl<'a> From<&'a str> for Error {
fn from(error: &'a str) -> Self {
Error::Other(error.into())
}
}
impl From<String> for Error {
fn from(error: String) -> Self {
Error::Other(error)
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum RpcError {
#[error("RPC error: {0}")]
ClientError(Box<dyn std::error::Error + Send + Sync + 'static>),
#[error("RPC error: subscription dropped.")]
SubscriptionDropped,
}
#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
#[non_exhaustive]
pub enum BlockError {
#[error("Could not find a block with hash {0} (perhaps it was on a non-finalized fork?)")]
NotFound(String),
#[error("Extrinsic type ID cannot be resolved with the provided metadata. Make sure this is a valid metadata")]
MissingType,
#[error("Unsupported extrinsic version, only version 4 is supported currently")]
UnsupportedVersion(u8),
#[error("Cannot decode extrinsic: {0}")]
DecodingError(codec::Error),
}
impl BlockError {
pub fn not_found(hash: impl AsRef<[u8]>) -> BlockError {
let hash = format!("0x{}", hex::encode(hash));
BlockError::NotFound(hash)
}
}
#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
#[non_exhaustive]
pub enum TransactionError {
#[error("The finality subscription expired")]
FinalityTimeout,
#[error("The block containing the transaction can no longer be found (perhaps it was on a non-finalized fork?)")]
BlockNotFound,
#[error("The transaction is no longer valid")]
Invalid,
#[error("The transaction was replaced by a transaction with the same (sender, nonce) pair but with higher priority.")]
Usurped,
#[error("The transaction was dropped from the pool because of a limit.")]
Dropped,
}
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum StorageAddressError {
#[error("Storage map type must be a composite type")]
MapTypeMustBeTuple,
#[error("Storage lookup requires {expected} keys but got {actual} keys")]
WrongNumberOfKeys {
actual: usize,
expected: usize,
},
#[error("Storage entry in metadata does not have the correct number of hashers to fields")]
WrongNumberOfHashers {
hashers: usize,
fields: usize,
},
}
#[derive(Clone, Debug, PartialEq, thiserror::Error)]
#[non_exhaustive]
pub enum MetadataError {
#[error("The DispatchError type isn't available")]
DispatchErrorNotFound,
#[error("Type with ID {0} not found")]
TypeNotFound(u32),
#[error("Pallet with index {0} not found")]
PalletIndexNotFound(u8),
#[error("Pallet with name {0} not found")]
PalletNameNotFound(String),
#[error("Variant with index {0} not found")]
VariantIndexNotFound(u8),
#[error("Constant with name {0} not found")]
ConstantNameNotFound(String),
#[error("Call with name {0} not found")]
CallNameNotFound(String),
#[error("Runtime trait with name {0} not found")]
RuntimeTraitNotFound(String),
#[error("Runtime method with name {0} not found")]
RuntimeMethodNotFound(String),
#[error("Call type not found in pallet with index {0}")]
CallTypeNotFoundInPallet(u8),
#[error("Event type not found in pallet with index {0}")]
EventTypeNotFoundInPallet(u8),
#[error("Storage details not found in pallet with name {0}")]
StorageNotFoundInPallet(String),
#[error("Storage entry {0} not found")]
StorageEntryNotFound(String),
#[error("The generated code is not compatible with the node")]
IncompatibleCodegen,
}
#[doc(hidden)]
pub trait RootError: Sized {
fn root_error(
pallet_bytes: &[u8],
pallet_name: &str,
metadata: &Metadata,
) -> Result<Self, Error>;
}