Use the generated DispatchError instead of the hardcoded Substrate one#394
Use the generated DispatchError instead of the hardcoded Substrate one#394
Conversation
…nto sp_version::RuntimeVersion
…ittle nicer to work with
src/error.rs
Outdated
| GenericError::Invalid(e) => GenericError::Invalid(e), | ||
| GenericError::InvalidMetadata(e) => GenericError::InvalidMetadata(e), | ||
| GenericError::Metadata(e) => GenericError::Metadata(e), | ||
| GenericError::Runtime(e) => GenericError::Runtime(f(e)), |
There was a problem hiding this comment.
Maybe a comment here to point the reader to the salient spot?
| /// Filters events by type. | ||
| pub fn filter_event<E: Event>(&mut self) { | ||
| self.event = Some((E::PALLET, E::EVENT)); | ||
| pub fn filter_event<Ev: Event>(&mut self) { |
There was a problem hiding this comment.
I for one very much appreciate these renames of the generics. :)
codegen/src/api/errors.rs
Outdated
| }) | ||
| .collect::<Result<Vec<(_, _)>, _>>()?; | ||
|
|
||
| let errors = pallet_errors |
There was a problem hiding this comment.
Maybe we can unify these two chains into a single? Like, why collect the pallet_errors first and then build the errors Vec?
There was a problem hiding this comment.
It might be a bit tricky; we end up with Result<_> Items just prior to that collect, so the collecting I think is just there to give a chance to bail if we encounter any errors! I'll see whether I can do it in a clean way!
There was a problem hiding this comment.
I settled on standard for loops; it felt tidier:
let mut pallet_errors = vec![];
for pallet in &metadata.pallets {
let error = match &pallet.error {
Some(err) => err,
None => continue
};
let type_def_variant = get_type_def_variant(error.ty.id())?;
for var in type_def_variant.variants().iter() {
pallet_errors.push(ErrorMetadata {
pallet_index: pallet.index,
error_index: var.index(),
pallet: pallet.name.clone(),
error: var.name().clone(),
variant: var.clone(),
});
}
}
codegen/src/api/errors.rs
Outdated
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct ErrorMetadata { |
There was a problem hiding this comment.
Missing docs here. Also, what do you think about moving this to the top?
codegen/src/api/errors.rs
Outdated
| pub error_index: u8, | ||
| pub pallet: String, | ||
| pub error: String, | ||
| variant: scale_info::Variant<scale_info::form::PortableForm>, |
There was a problem hiding this comment.
dq: what is the reason for storing the variant for?
There was a problem hiding this comment.
Yup, that's it! I'm not sure why I did/left this now. I've made it be just the doc string, which simplifies things a bit!
codegen/src/api/errors.rs
Outdated
|
|
||
| impl ErrorMetadata { | ||
| pub fn description(&self) -> String { | ||
| self.variant.docs().join("\n") |
There was a problem hiding this comment.
So if the node is compiled to strip the docs, this will be empty? Worth detecting and add some kind of default, "No docs available in the metadata"?
There was a problem hiding this comment.
In the empty case docs will just be "", which seems good enough (that can be detected and handled down the line in whichever way things prefer)
codegen/src/api/errors.rs
Outdated
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum InvalidMetadataError { |
There was a problem hiding this comment.
Ah; doesn't need to be pub :)
examples/custom_type_derives.rs
Outdated
| #[subxt::subxt( | ||
| runtime_metadata_path = "examples/polkadot_metadata.scale", | ||
| generated_type_derives = "Clone, Debug" | ||
| generated_type_derives = "Clone" |
There was a problem hiding this comment.
A comment here and on runtime_metadata_path would be good.
There was a problem hiding this comment.
Looking further, I think this whole example could use some attention. I am not sure what it actually does. :/
(Not a concern for this PR ofc)
There was a problem hiding this comment.
I gave the exampel a wee tidy; it's purpose appears solely to be showing off the generated_type_derives usage, so I made that a little clearer (and I didn't see a need for the strange way of calling clone offhand).
paritytech#394) * WIP DispatchError generic param * main crate now compiling with new E generic param for DispatchError * Remove E param from RpcClient since it doesn't really need it * Point to generated DispatchError in codegen so no need for additional param there * More error hunting; cargo check --all-targets passes now * Use our own RuntimeVersion struct (for now) to avoid error decoding into sp_version::RuntimeVersion * cargo fmt * fix docs and expose private documented thing that I think should be pub * move error info to compile time so that we can make DispatchError a little nicer to work with * cargo fmt * clippy * Rework error handling to remove <E> param in most cases * fix Error doc ambiguity (hopefully) * doc tweaks * docs: remove dismbiguation thing that isn't needed now * One more Error<E> that can be a BasicError * rewrite pallet errors thing into normal loops to tidy * tidy errors codegen a little * tidy examples/custom_type_derives.rs a little * cargo fmt * silcnce clippy in example
* Add error information back into metadata to roll back removal in #394 * Go back to obtaining runtime error info * re-do codegen too to check that it's all gravy * Convert DispatchError module errors into a module variant to make them easier to work with * Fix broken doc link
The intention of this PR is to use the version of
DispatchErrorgenerated from the metadata, rather than using a baked in version which may not be compatible with the node being targeted.A rough summary of the changes:
Errortype becomesError<E>(actually,GenericError<RuntimeError<E>>under the hood) whereEis the generatedDispatchError.BasicError(actuallyGenericError<Infallible>under the hood) also exists for the cases where we don't intend to return a Runtime error (ie most cases).Eparam was added where necessary to accomodate this new generic parameter in the error type (although usingBasicErrornegates the need for it in many cases).DispatchErrorto our error type. This logic has moved to compile-time, so that we can obtain more details from the generatedDispatchErrormore ergonomicallyDebughas been added as a default trait for generated types to impl, mainly so that the generatedDispatchErrorand its sub types can be debug printed inside ourErrortype.