frame_support/traits/
error.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Traits for describing and constraining pallet error types.
19use codec::{Compact, Decode, Encode};
20use core::marker::PhantomData;
21
22/// Trait indicating that the implementing type is going to be included as a field in a variant of
23/// the `#[pallet::error]` enum type.
24///
25/// ## Notes
26///
27/// The pallet error enum has a maximum encoded size as defined by
28/// [`frame_support::MAX_MODULE_ERROR_ENCODED_SIZE`]. If the pallet error type exceeds this size
29/// limit, a static assertion during compilation will fail. The compilation error will be in the
30/// format of `error[E0080]: evaluation of constant value failed` due to the usage of
31/// const assertions.
32pub trait PalletError: Encode + Decode {
33	/// The maximum encoded size for the implementing type.
34	///
35	/// This will be used to check whether the pallet error type is less than or equal to
36	/// [`frame_support::MAX_MODULE_ERROR_ENCODED_SIZE`], and if it is, a compilation error will be
37	/// thrown.
38	const MAX_ENCODED_SIZE: usize;
39}
40
41macro_rules! impl_for_types {
42	(size: $size:expr, $($typ:ty),+) => {
43		$(
44			impl PalletError for $typ {
45				const MAX_ENCODED_SIZE: usize = $size;
46			}
47		)+
48	};
49}
50
51impl_for_types!(size: 0, (), crate::Never);
52impl_for_types!(size: 1, u8, i8, bool);
53impl_for_types!(size: 2, u16, i16, Compact<u8>);
54impl_for_types!(size: 4, u32, i32, Compact<u16>);
55impl_for_types!(size: 5, Compact<u32>);
56impl_for_types!(size: 8, u64, i64);
57impl_for_types!(size: 9, Compact<u64>);
58// Contains a u64 for secs and u32 for nanos, hence 12 bytes
59impl_for_types!(size: 12, core::time::Duration);
60impl_for_types!(size: 16, u128, i128);
61impl_for_types!(size: 17, Compact<u128>);
62
63impl<T> PalletError for PhantomData<T> {
64	const MAX_ENCODED_SIZE: usize = 0;
65}
66
67impl<T: PalletError> PalletError for core::ops::Range<T> {
68	const MAX_ENCODED_SIZE: usize = T::MAX_ENCODED_SIZE.saturating_mul(2);
69}
70
71impl<T: PalletError, const N: usize> PalletError for [T; N] {
72	const MAX_ENCODED_SIZE: usize = T::MAX_ENCODED_SIZE.saturating_mul(N);
73}
74
75impl<T: PalletError> PalletError for Option<T> {
76	const MAX_ENCODED_SIZE: usize = T::MAX_ENCODED_SIZE.saturating_add(1);
77}
78
79impl<T: PalletError, E: PalletError> PalletError for Result<T, E> {
80	const MAX_ENCODED_SIZE: usize = if T::MAX_ENCODED_SIZE > E::MAX_ENCODED_SIZE {
81		T::MAX_ENCODED_SIZE
82	} else {
83		E::MAX_ENCODED_SIZE
84	}
85	.saturating_add(1);
86}
87
88#[impl_trait_for_tuples::impl_for_tuples(1, 18)]
89impl PalletError for Tuple {
90	const MAX_ENCODED_SIZE: usize = {
91		let mut size = 0_usize;
92		for_tuples!( #(size = size.saturating_add(Tuple::MAX_ENCODED_SIZE);)* );
93		size
94	};
95}