-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Export a top-level Error enum from construct_runtime!
#12733
Comments
As already on SE, I don't get where you want to use this api? Where would this kind of api live and do? |
It's the same use case as polkaJs and subxt - but both are general-purpose client libraries. If I want to write one specifically for my runtime, what I have suggested above might be helpful. |
Subxt already has an issue for exactly what you want: paritytech/subxt#313 |
So... great! Clearly I'm not the only one who thinks this would be useful. The issue has been open in subxt for over a year, though, so apparently not getting much love. Moreover, like I said, For anyone writing a client for their own chain, we shouldn't need to generate it from the type metadata: we have all the type info we need in the source code, and we have the rust compiler to ensure its correctness. The only thing we're missing is a slightly more useful runtime I don't really understand - is there some specific reason you think it's a bad idea? |
So you want to use this error type in your node? Or where exactly? Or only in your runtime? |
My use case is outside the runtime (but with access to the runtime crate: so I can use the concrete types of my runtime). I want to create a type-safe wrapper around the jsonrpc interface to submit extrinsics. And I want to be able to return a useful error when a call fails. I can submit an extrinsic with an encoded /// Reason why a pallet call failed.
#[derive(Eq, Clone, Copy, Encode, Decode, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct ModuleError {
/// Module index, matching the metadata module index.
pub index: u8,
/// Module specific error value.
// NB. MAX_MODULE_ERROR_ENCODED_SIZE is 4 for some reason
pub error: [u8; MAX_MODULE_ERROR_ENCODED_SIZE],
/// Optional error message.
#[codec(skip)]
#[cfg_attr(feature = "std", serde(skip_deserializing))]
pub message: Option<&'static str>,
} But note the message is not encoded. So in order to get any useful information about the error (ie. the originating I feel like this would obviously be quite useful, in the same way that being able to refer to calls and events can be useful. I don't understand why errors do not follow the same approach as used for calls and events - was this a conscious decision? Is there a better alternative for what I'm trying to do? |
Yes it was. Otherwise we would have required to add I also get what you are trying to do, but this goes a little bit against the design decisions of Substrate. Substrate is build in a way that the node and runtime are separate. They are only talking to each other via defined interfaces/apis. When you would start using the If you are watching for the runtime events any way, getting the error and index isn't hard. What you could do is to add support for your requested feature to |
Hey, thanks for the reply! I guess this makes sense but then similar arguments seem to apply to pallet calls, events and origins. And we could have the Error type for convenience but still use TLDR I can see why it was done the way it was, but I still think a top-level RuntimeError enum would be useful in some cases, for similar reasons that top-level RuntimeEvent/Call/Origins are useful. I don't see why Errors are an exception. The suggested compromise with |
You can open an issue here: https://github.com/paritytech/frame-metadata |
Thanks, I found this - paritytech/frame-metadata#43 Looks like it might fit our needs. |
Is there an existing issue?
Experiencing problems? Have you tried our Stack Exchange first?
Description of bug
Not technically a bug, more of feature request, but then there is no other category of issue, and I opened a stackexchange issue but got little traction beyond "why would you need this?".
Given that the
construct_runtime
macro exports top-levelCall
andEvent
enums that group the pallet-level equivalents, it seems reasonable to expect that there would also be a top-levelError
enum to allow easy encoding and decoding of runtime errors.The specific use-case I have in mind is an api that allows execution of a
runtime::Call
with response type ofResult<runtime::Event, runtime::Error>
, whereruntime::Error
contains a useful error message (or at least a structured enum that can be matched on).Encoding the Call: easy. Decoding the Events: easy. Decoding the Error.. not so easy.
Right now the only option (as far as I can tell) to get the details of the Error involves parsing the metadata to pull out the error details, which is not the most intuitive or ergonomic. If there is a better alternative, please let me know!
I appreciate this might be non-trivial to add, but it would be great if it could be considered.
Steps to reproduce
No response
The text was updated successfully, but these errors were encountered: