Make decl_error! errors usable (#4449)

* 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
This commit is contained in:
Bastian Köcher
2019-12-19 14:01:52 +01:00
committed by Gavin Wood
parent 0aab5c659e
commit 8e393aa5a8
69 changed files with 868 additions and 611 deletions
+7 -9
View File
@@ -240,13 +240,14 @@ mod tests {
mod system {
use super::*;
pub trait Trait {
pub trait Trait: 'static {
const ASSOCIATED_CONST: u64 = 500;
type Origin: Into<Result<RawOrigin<Self::AccountId>, Self::Origin>>
+ From<RawOrigin<Self::AccountId>>;
type AccountId: From<u32> + Encode;
type BlockNumber: From<u32> + Encode;
type SomeValue: Get<u32>;
type ModuleToIndex: crate::traits::ModuleToIndex;
}
decl_module! {
@@ -286,10 +287,8 @@ mod tests {
mod event_module {
use crate::dispatch::DispatchResult;
pub trait Trait {
type Origin;
pub trait Trait: super::system::Trait {
type Balance;
type BlockNumber;
}
decl_event!(
@@ -302,14 +301,14 @@ mod tests {
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error;
type Error = Error<T>;
fn aux_0(_origin) -> DispatchResult<Error> { unreachable!() }
fn aux_0(_origin) -> DispatchResult { unreachable!() }
}
}
crate::decl_error! {
pub enum Error {
pub enum Error for Module<T: Trait> {
/// Some user input error
UserInputError,
/// Something bad happened
@@ -372,9 +371,7 @@ mod tests {
}
impl event_module::Trait for TestRuntime {
type Origin = Origin;
type Balance = u32;
type BlockNumber = u32;
}
impl event_module2::Trait for TestRuntime {
@@ -392,6 +389,7 @@ mod tests {
type AccountId = u32;
type BlockNumber = u32;
type SomeValue = SystemValue;
type ModuleToIndex = ();
}
impl_runtime_metadata!(