Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Refactor module & runtime error handing #2880

Closed
wants to merge 49 commits into from

Conversation

xlc
Copy link
Contributor

@xlc xlc commented Jun 17, 2019

Fixes #2246

This PR should:

  • introduce module error type which declared by new macro decl_error.
  • update construct_runtime macro to generate a runtime Error
  • keep module source code compatibility. i.e. existing module does not need to be updated
  • encode runtime error as a two byte value
  • ensure backward compatibility (needs help from @bkchr ) (I don't think this is needed anymore)

Next stage (won't be in this PR) will :

  • metadata for module errors
  • introduce decl_error to each module and replace all the string error
  • anything else?

cc @jacogr this introduces a new type DispatchError, without additional metadata, SDK won't be able to know the the meaning of the error code, but we can kind of solve it by duplicate the definition of the Error enum like other types.

srml/balances/src/lib.rs Outdated Show resolved Hide resolved
@xlc
Copy link
Contributor Author

xlc commented Jun 18, 2019

@bkchr can you take a look when you have time? Just want to make sure I am on the right track.

Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the general direction looks good :)

core/client/src/block_builder/block_builder.rs Outdated Show resolved Hide resolved
core/sr-primitives/src/lib.rs Outdated Show resolved Hide resolved
srml/executive/src/lib.rs Outdated Show resolved Hide resolved
srml/system/src/lib.rs Outdated Show resolved Hide resolved
@bkchr
Copy link
Member

bkchr commented Jun 19, 2019

@tomusdrw you maybe also want to take a first peek :)

Copy link
Contributor

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work! Looks really impressive and mostly good for me.

core/client/src/block_builder/block_builder.rs Outdated Show resolved Hide resolved
core/sr-primitives/src/lib.rs Show resolved Hide resolved
node-template/runtime/src/template.rs Outdated Show resolved Hide resolved
srml/democracy/src/lib.rs Outdated Show resolved Hide resolved
srml/executive/src/lib.rs Show resolved Hide resolved
srml/executive/src/lib.rs Outdated Show resolved Hide resolved
srml/sudo/src/lib.rs Outdated Show resolved Hide resolved
xlc and others added 3 commits June 21, 2019 22:01
Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
@xlc
Copy link
Contributor Author

xlc commented Jul 12, 2019

I cannot get it compile without modify every modules

@bkchr
Copy link
Member

bkchr commented Jul 12, 2019

What would be the modifications? And in general I don't see the problem? Introducing a new feature some times requires to touch every module.

@xlc
Copy link
Contributor Author

xlc commented Jul 12, 2019

Every module will need Error type in Trait similar to Event. It is fine I guess but will make this PR very large.
I would like to have this merged and then do incremental improvements over change all the modules in one go.

@bkchr
Copy link
Member

bkchr commented Jul 12, 2019

Why does it need Error in the trait? Can you explain this please?

And do you also support nesting errors? If yes, how is this solved? Theoretically you could have an infinite nesting, how do I find the root cause?

@xlc
Copy link
Contributor Author

xlc commented Jul 12, 2019

It is complicated...
In the impl_outer_error macro of my attempt, it has impl From<$module::Error> for Error. This means all the $module::Error needs to be different type (which many of them are &'static str in this PR). So I have to insert decl_error! { pub enum Error { } } if type Error is not included in decl_module. This introduces a new issue with ensure_signed which returns Result<(), system::Error>. One solution is add Error: From<system::Error> + From<Error> to handle the conversion. But it means every module Trait needs change, which is trivial but still a lot of changes.

Nesting error is not really supported.
The runtime Error type is like

pub enum Error {
  system(system::Error),
  balances(balances::Error),
  ...
}

and module Error type is like

pub enum Error {
  Other(&'static str),
  SomeError,
  AnotherError
  ...
}

One way to handle nested error is convert the underlying error to the module error type, by add variants to error enum. Or if the error is from another module, just add From<another_module::Error>bounds to the module Error type and it should be good.

@xlc xlc force-pushed the improve-error-response branch 2 times, most recently from 8a01ca2 to 7587ce3 Compare July 18, 2019 03:12
@gavofyork
Copy link
Member

i'm pretty happy to get this in pre-kusama; the &static str is a bit annoying, but i agree with @xlc that it's otherwise a bit too big to manage as a single PR as it'll get stuck in resolve-hell.

it would be handy to have this pre-based onto #3102

@gavofyork gavofyork added A0-please_review Pull request needs code review. and removed A4-gotissues labels Jul 20, 2019
@gavofyork gavofyork requested a review from bkchr July 20, 2019 01:23
@gavofyork gavofyork added this to the 2.0-k milestone Jul 20, 2019
@gavofyork
Copy link
Member

in any case will need resolving if it is to go in soon

@bkchr
Copy link
Member

bkchr commented Jul 22, 2019

I'm currently looking into this. (JFYI)

@xlc
Copy link
Contributor Author

xlc commented Jul 22, 2019

I am going to do some refactor to make this work with sr_primitives::traits::DispatchError that introduced in #3102,

@bkchr
Copy link
Member

bkchr commented Jul 23, 2019

Can you postpone this? I started to write some code already for this pr.

@xlc
Copy link
Contributor Author

xlc commented Jul 23, 2019

Sure. I just started it and got distracted. Will wait for your one instead.

@gavofyork gavofyork added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A0-please_review Pull request needs code review. labels Aug 8, 2019
@xlc
Copy link
Contributor Author

xlc commented Aug 12, 2019

@bkchr any updates?

@bkchr
Copy link
Member

bkchr commented Aug 13, 2019

@bkchr any updates?

Yeah, after a lot of Kusama related task, I finally have time to work on this. I merged master and now try some things.

@bkchr
Copy link
Member

bkchr commented Aug 18, 2019

Superseded by: #3433

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A3-in_progress Pull request is in progress. No review needed at this stage.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Propagate extrinsic errors over JSON-RPC for locally submitted transactions
6 participants