use crate::metadata::Metadata;
use codec::Decode;
use core::fmt::Debug;
use scale_info::TypeDef;
use std::borrow::Cow;
pub use crate::metadata::{
InvalidMetadataError,
MetadataError,
};
pub use scale_value::scale::{
DecodeError,
EncodeError,
};
#[derive(Debug, thiserror::Error)]
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("Invalid Metadata: {0}")]
InvalidMetadata(#[from] InvalidMetadataError),
#[error("Metadata: {0}")]
Metadata(#[from] MetadataError),
#[error("Runtime error: {0:?}")]
Runtime(DispatchError),
#[error("Error decoding into dynamic value: {0}")]
DecodeValue(#[from] DecodeError),
#[error("Error encoding from dynamic value: {0}")]
EncodeValue(#[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("Other error: {0}")]
Other(String),
}
impl From<&str> for Error {
fn from(error: &str) -> Self {
Error::Other(error.into())
}
}
impl From<String> for Error {
fn from(error: String) -> Self {
Error::Other(error)
}
}
impl From<DispatchError> for Error {
fn from(error: DispatchError) -> Self {
Error::Runtime(error)
}
}
#[derive(Debug, thiserror::Error)]
pub enum RpcError {
#[error("RPC error: {0}")]
ClientError(Box<dyn std::error::Error + Send + Sync + 'static>),
#[error("RPC error: subscription dropped.")]
SubscriptionDropped,
}
#[derive(Debug, thiserror::Error)]
pub enum DispatchError {
#[error("Module error: {0}")]
Module(ModuleError),
#[error("Undecoded dispatch error: {0:?}")]
Other(Vec<u8>),
}
impl DispatchError {
pub fn decode_from<'a>(bytes: impl Into<Cow<'a, [u8]>>, metadata: &Metadata) -> Self {
let bytes = bytes.into();
let dispatch_error_ty_id = match metadata.dispatch_error_ty() {
Some(id) => id,
None => {
tracing::warn!(
"Can't decode error: sp_runtime::DispatchError was not found in Metadata"
);
return DispatchError::Other(bytes.into_owned())
}
};
let dispatch_error_ty = match metadata.types().resolve(dispatch_error_ty_id) {
Some(ty) => ty,
None => {
tracing::warn!("Can't decode error: sp_runtime::DispatchError type ID doesn't resolve to a known type");
return DispatchError::Other(bytes.into_owned())
}
};
let variant = match dispatch_error_ty.type_def() {
TypeDef::Variant(var) => var,
_ => {
tracing::warn!(
"Can't decode error: sp_runtime::DispatchError type is not a Variant"
);
return DispatchError::Other(bytes.into_owned())
}
};
let module_variant_idx = variant
.variants()
.iter()
.find(|v| v.name() == "Module")
.map(|v| v.index());
let module_variant_idx = match module_variant_idx {
Some(idx) => idx,
None => {
tracing::warn!("Can't decode error: sp_runtime::DispatchError does not have a 'Module' variant");
return DispatchError::Other(bytes.into_owned())
}
};
if bytes[0] != module_variant_idx {
return DispatchError::Other(bytes.into_owned())
}
let bytes = &bytes[1..];
#[derive(Decode)]
struct LegacyModuleError {
index: u8,
error: u8,
}
#[derive(Decode)]
struct CurrentModuleError {
index: u8,
error: [u8; 4],
}
let err = match CurrentModuleError::decode(&mut &*bytes) {
Ok(e) => e,
Err(_) => {
let old_e = match LegacyModuleError::decode(&mut &*bytes) {
Ok(err) => err,
Err(_) => {
tracing::warn!("Can't decode error: sp_runtime::DispatchError does not match known formats");
return DispatchError::Other(bytes.to_vec())
}
};
CurrentModuleError {
index: old_e.index,
error: [old_e.error, 0, 0, 0],
}
}
};
let error_details = match metadata.error(err.index, err.error[0]) {
Ok(details) => details,
Err(_) => {
tracing::warn!("Can't decode error: sp_runtime::DispatchError::Module details do not match known information");
return DispatchError::Other(bytes.to_vec())
}
};
DispatchError::Module(ModuleError {
pallet: error_details.pallet().to_string(),
error: error_details.error().to_string(),
description: error_details.docs().to_vec(),
error_data: ModuleErrorData {
pallet_index: err.index,
error: err.error,
},
})
}
}
#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
pub enum BlockError {
#[error(
"Could not find a block with hash {0} (perhaps it was on a non-finalized fork?)"
)]
BlockHashNotFound(String),
}
impl BlockError {
pub fn block_hash_not_found(hash: impl AsRef<[u8]>) -> BlockError {
let hash = format!("0x{}", hex::encode(hash));
BlockError::BlockHashNotFound(hash)
}
}
#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
pub enum TransactionError {
#[error("The finality subscription expired")]
FinalitySubscriptionTimeout,
#[error("The block containing the transaction can no longer be found (perhaps it was on a non-finalized fork?)")]
BlockHashNotFound,
}
#[derive(Clone, Debug, thiserror::Error)]
#[error("{pallet}: {error}\n\n{}", .description.join("\n"))]
pub struct ModuleError {
pub pallet: String,
pub error: String,
pub description: Vec<String>,
pub error_data: ModuleErrorData,
}
#[derive(Clone, Debug, thiserror::Error)]
#[error("Pallet index {pallet_index}: raw error: {error:?}")]
pub struct ModuleErrorData {
pub pallet_index: u8,
pub error: [u8; 4],
}
impl ModuleErrorData {
pub fn error_index(&self) -> u8 {
self.error[0]
}
}
#[derive(Clone, Debug, thiserror::Error)]
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 lookup requires type {0} to exist in the metadata, but it was not found"
)]
TypeNotFound(u32),
#[error(
"Storage entry in metadata does not have the correct number of hashers to fields"
)]
WrongNumberOfHashers {
hashers: usize,
fields: usize,
},
}