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:
Tadeo Hepperle
2023-05-02 17:33:09 +02:00
committed by GitHub
parent a3b3d262ed
commit 265f16fdec
6 changed files with 1572 additions and 6 deletions
+11 -1
View File
@@ -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.