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

Make decl_error! errors usable #4449

Merged
merged 3 commits into from
Dec 19, 2019
Merged

Conversation

bkchr
Copy link
Member

@bkchr bkchr commented Dec 18, 2019

This pr implements support for returning errors of different pallets in
a pallet. These errors need to be declared with decl_error!.

The pr changes the following:

  • Each dispatchable function now returns a DispatchResult which is an
    alias for Result<(), DispatchError>.
  • DispatchError is an enum that has 4 variants:
    • Other: For storing string error messages
    • CannotLookup: Variant that is returned when something returns a
      sp_runtime::LookupError
    • BadOrigin: Variant that is returned for any kind of bad origin
    • Module: The error of a specific module. Contains the index,
      error and the message. The index is the index of the module in
      construct_runtime!. error is the index of the error in the error
      enum declared by decl_error!. message is the message to the error
      variant (this will not be encoded).
  • construct_runtime! now creates a new struct ModuleToIndex. This
    struct implements the trait ModuleToIndex.
  • frame_system::Trait has a new associated type: ModuleToIndex that
    expects the ModuleToIndex generated by construct_runtime!.
  • All error strings returned in any module are being converted now to DispatchError.
  • BadOrigin is the default error returned by any type that implements EnsureOrigin.

Fixes: #4409 #4040

@jacogr this changes the encoding of the DispatchError. Do you want to have a bump of the metadata to detect this?

This pr implements support for returning errors of different pallets in
a pallet. These errors need to be declared with `decl_error!`.

The pr changes the following:

- Each dispatchable function now returns a `DispatchResult` which is an
alias for `Result<(), DispatchError>`.
- `DispatchError` is an enum that has 4 variants:
  - `Other`: For storing string error messages
  - `CannotLookup`: Variant that is returned when something returns a
  `sp_runtime::LookupError`
  - `BadOrigin`: Variant that is returned for any kind of bad origin
  - `Module`: The error of a specific module. Contains the `index`,
  `error` and the `message`. The index is the index of the module in
  `construct_runtime!`. `error` is the index of the error in the error
  enum declared by `decl_error!`. `message` is the message to the error
  variant (this will not be encoded).
- `construct_runtime!` now creates a new struct `ModuleToIndex`. This
struct implements the trait `ModuleToIndex`.
- `frame_system::Trait` has a new associated type: `ModuleToIndex` that
expects the `ModuleToIndex` generated by `construct_runtime!`.
- All error strings returned in any module are being converted now to `DispatchError`.
- `BadOrigin` is the default error returned by any type that implements `EnsureOrigin`.
Copy link
Contributor

@shawntabrizi shawntabrizi left a comment

Choose a reason for hiding this comment

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

The API looks good :)

Thank you Basti!

if reasons.intersects(WithdrawReason::Reserve | WithdrawReason::Transfer)
&& Self::vesting_balance(who) > new_balance
{
return Err("vesting balance too high to send value");
Err("vesting balance too high to send value")?
Copy link
Member

@niklasad1 niklasad1 Dec 19, 2019

Choose a reason for hiding this comment

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

personally I prefer return Err("fooo".into()) because it is more explicit than Err("foo")? but it's fine either way.

@gavofyork gavofyork merged commit fef0e75 into master Dec 19, 2019
@gavofyork gavofyork deleted the bkchr-decl-error-improvements branch December 19, 2019 13:01
sorpaas pushed a commit that referenced this pull request Nov 20, 2020
* Make `decl_error!` errors usable

This pr implements support for returning errors of different pallets in
a pallet. These errors need to be declared with `decl_error!`.

The pr changes the following:

- Each dispatchable function now returns a `DispatchResult` which is an
alias for `Result<(), DispatchError>`.
- `DispatchError` is an enum that has 4 variants:
  - `Other`: For storing string error messages
  - `CannotLookup`: Variant that is returned when something returns a
  `sp_runtime::LookupError`
  - `BadOrigin`: Variant that is returned for any kind of bad origin
  - `Module`: The error of a specific module. Contains the `index`,
  `error` and the `message`. The index is the index of the module in
  `construct_runtime!`. `error` is the index of the error in the error
  enum declared by `decl_error!`. `message` is the message to the error
  variant (this will not be encoded).
- `construct_runtime!` now creates a new struct `ModuleToIndex`. This
struct implements the trait `ModuleToIndex`.
- `frame_system::Trait` has a new associated type: `ModuleToIndex` that
expects the `ModuleToIndex` generated by `construct_runtime!`.
- All error strings returned in any module are being converted now to `DispatchError`.
- `BadOrigin` is the default error returned by any type that implements `EnsureOrigin`.

* Fix frame system benchmarks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce general BadOrigin runtime error
5 participants