mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
codegen for root level error (#930)
* code gen for root error * cargo fmt * polkadot.rs regenerated * use pallet name and decode with metadata * remove pallet by name fn * test that we can decode a ModuleError via as_root_error * nits --------- Co-authored-by: James Wilson <james@jsdw.me>
This commit is contained in:
@@ -10,6 +10,9 @@ use core::fmt::Debug;
|
||||
use scale_decode::visitor::DecodeAsTypeResult;
|
||||
use std::borrow::Cow;
|
||||
|
||||
use super::Error;
|
||||
use crate::error::RootError;
|
||||
|
||||
/// An error dispatching a transaction.
|
||||
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
@@ -133,12 +136,13 @@ impl PartialEq for ModuleError {
|
||||
self.raw == other.raw
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for ModuleError {}
|
||||
|
||||
impl std::fmt::Display for ModuleError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let Ok(details) = self.details() else {
|
||||
return f.write_str("Unknown pallet error (pallet and error details cannot be retrieved)")
|
||||
return f.write_str("Unknown pallet error (pallet and error details cannot be retrieved)");
|
||||
};
|
||||
|
||||
let pallet = details.pallet();
|
||||
@@ -159,6 +163,12 @@ impl ModuleError {
|
||||
pub fn raw(&self) -> RawModuleError {
|
||||
self.raw
|
||||
}
|
||||
|
||||
/// Attempts to decode the ModuleError into a value implementing the trait `RootError`
|
||||
/// where the actual type of value is the generated top level enum `Error`.
|
||||
pub fn as_root_error<E: RootError>(&self) -> Result<E, Error> {
|
||||
E::root_error(&self.raw.error, self.details()?.pallet(), &self.metadata)
|
||||
}
|
||||
}
|
||||
|
||||
/// The error details about a module error that has occurred.
|
||||
|
||||
+13
-1
@@ -14,7 +14,7 @@ pub use dispatch_error::{
|
||||
};
|
||||
|
||||
// Re-expose the errors we use from other crates here:
|
||||
pub use crate::metadata::{InvalidMetadataError, MetadataError};
|
||||
pub use crate::metadata::{InvalidMetadataError, Metadata, MetadataError};
|
||||
pub use scale_decode::Error as DecodeError;
|
||||
pub use scale_encode::Error as EncodeError;
|
||||
|
||||
@@ -162,3 +162,15 @@ pub enum StorageAddressError {
|
||||
fields: usize,
|
||||
},
|
||||
}
|
||||
|
||||
/// This trait is implemented on the statically generated root ModuleError type
|
||||
#[doc(hidden)]
|
||||
pub trait RootError: Sized {
|
||||
/// Given details of the pallet error we want to decode
|
||||
fn root_error(
|
||||
// typically a [u8; 4] encodes the error of a pallet
|
||||
pallet_bytes: &[u8],
|
||||
pallet_name: &str,
|
||||
metadata: &Metadata,
|
||||
) -> Result<Self, Error>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user