Attribute Macro frame_support::pallet_macros::error

source ·
#[error]
Expand description

Allows defining an error enum that will be returned from the dispatchable when an error occurs.

The information for this error type is then stored in runtime metadata.

Item must be defined as so:

#[frame_support::pallet(dev_mode)]
mod pallet {
	#[pallet::pallet]
	pub struct Pallet<T>(_);

	#[pallet::error]
	pub enum Error<T> {
		/// SomeFieldLessVariant doc
		SomeFieldLessVariant,
		/// SomeVariantWithOneField doc
		SomeVariantWithOneField(u32),
	}
}

I.e. a regular enum named Error, with generic T and fieldless or multiple-field variants.

Any field type in the enum variants must implement scale_info::TypeInfo in order to be properly used in the metadata, and its encoded size should be as small as possible, preferably 1 byte in size in order to reduce storage size. The error enum itself has an absolute maximum encoded size specified by frame_support::MAX_MODULE_ERROR_ENCODED_SIZE.

(1 byte can still be 256 different errors. The more specific the error, the easier it is to diagnose problems and give a better experience to the user. Don’t skimp on having lots of individual error conditions.)

Field types in enum variants must also implement frame_support::PalletError, otherwise the pallet will fail to compile. Rust primitive types have already implemented the frame_support::PalletError trait along with some commonly used stdlib types such as Option and core::marker::PhantomData, and hence in most use cases, a manual implementation is not necessary and is discouraged.

The generic T must not bound anything and a where clause is not allowed. That said, bounds and/or a where clause should not needed for any use-case.

§Macro expansion

The macro implements the Debug trait and functions as_u8 using variant position, and as_str using variant doc.

The macro also implements From<Error<T>> for &'static str and From<Error<T>> for DispatchError.

§Note on deprecation of Errors


Documentation for this macro can be found at frame_support::pallet_macros::error.