Is there any way to tell the construct_runtime!
macro to build/export a top-level Error
enum type in the same way it provides the top-level Call
and Event
types?
As far as I can tell the only way to decode an Error retrieved from the runtime is to use the Metadata (which is a little cumbersome), since the inner ModuleError erases the error message - presumably to save storage.
It would be nice if it was possible to do something like this:
match dispatch_error {
DispatchError::ModuleError(module_error) => RuntimeError::from(module_error),
_ => // do something else
}
A concrete use case for this would be a client that wants to submit and watch an extrinsic, and be able to show the user a useful error message.
Pseudo-code:
// Imagine we have a top-level runtime::Error type.
fn submit_and_watch(ex: Extrinsic) -> Result<Vec<runtime::Event>, runtime::Error> {
let ex_hash = author.submitExtrinsic(ex);
let ex_events = get_extrinsic_events(ex_hash);
if let Some(ex_failed) = ex_events.find(|e| matches!(e, System::ExtrinsicFailed { .. })) {
// Can't convert to string because `dispatch_error` erases
// the error message.
// Retrieving the error message currently requires (i think?)
// parsing the metadata.
// If we had a top-level Error type with associated conversion
// traits, we could do it like this:
Err(runtime::Error::from(ex_failed.dispatch_error))
} else {
Ok(ex_events)
}
}