Macro frame_support::decl_error
source · macro_rules! decl_error { ( $(#[$attr:meta])* pub enum $error:ident for $module:ident< $generic:ident: $trait:path $(, $inst_generic:ident: $instance:path)? > $( where $( $where_ty:ty: $where_bound:path ),* $(,)? )? { $( $( #[doc = $doc_attr:tt] )* $name:ident ),* $(,)? } ) => { ... }; }
Expand description
Declare an error type for a runtime module.
decl_error!
supports only variants that do not hold any data. The dispatchable
functions return DispatchResult
. The error type
implements From<ErrorType> for DispatchResult
to make the error type usable as error
in the dispatchable functions.
It is required that the error type is registered in decl_module!
to make the error
exported in the metadata.
Usage
decl_error! {
/// Errors that can occur in my module.
pub enum MyError for Module<T: Config> {
/// Hey this is an error message that indicates bla.
MyCoolErrorMessage,
/// You are just not cool enough for my module!
YouAreNotCoolEnough,
}
}
// You need to register the error type in `decl_module!` as well to make the error
// exported in the metadata.
decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {
type Error = MyError<T>;
#[weight = 0]
fn do_something(origin) -> frame_support::dispatch::DispatchResult {
Err(MyError::<T>::YouAreNotCoolEnough.into())
}
}
}
For instantiable modules you also need to give the instance generic type and bound to the error declaration.