sp_runtime/legacy/
byte_sized_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//! Runtime types that existed prior to BlockBuilder API version 6.
19
20use crate::{ArithmeticError, TokenError};
21use codec::{Decode, Encode};
22use scale_info::TypeInfo;
23#[cfg(feature = "serde")]
24use serde::{Deserialize, Serialize};
25
26/// [`ModuleError`] type definition before BlockBuilder API version 6.
27#[derive(Eq, Clone, Copy, Encode, Decode, Debug, TypeInfo)]
28#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
29pub struct ModuleError {
30	/// Module index, matching the metadata module index.
31	pub index: u8,
32	/// Module specific error value.
33	pub error: u8,
34	/// Optional error message.
35	#[codec(skip)]
36	#[cfg_attr(feature = "serde", serde(skip_deserializing))]
37	pub message: Option<&'static str>,
38}
39
40impl PartialEq for ModuleError {
41	fn eq(&self, other: &Self) -> bool {
42		(self.index == other.index) && (self.error == other.error)
43	}
44}
45
46/// [`DispatchError`] type definition before BlockBuilder API version 6.
47#[derive(Eq, Clone, Copy, Encode, Decode, Debug, TypeInfo, PartialEq)]
48#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
49pub enum DispatchError {
50	/// Some error occurred.
51	Other(
52		#[codec(skip)]
53		#[cfg_attr(feature = "serde", serde(skip_deserializing))]
54		&'static str,
55	),
56	/// Failed to lookup some data.
57	CannotLookup,
58	/// A bad origin.
59	BadOrigin,
60	/// A custom error in a module.
61	Module(ModuleError),
62	/// At least one consumer is remaining so the account cannot be destroyed.
63	ConsumerRemaining,
64	/// There are no providers so the account cannot be created.
65	NoProviders,
66	/// There are too many consumers so the account cannot be created.
67	TooManyConsumers,
68	/// An error to do with tokens.
69	Token(TokenError),
70	/// An arithmetic error.
71	Arithmetic(ArithmeticError),
72}
73
74/// [`DispatchOutcome`] type definition before BlockBuilder API version 6.
75pub type DispatchOutcome = Result<(), DispatchError>;
76
77/// [`ApplyExtrinsicResult`] type definition before BlockBuilder API version 6.
78pub type ApplyExtrinsicResult =
79	Result<DispatchOutcome, crate::transaction_validity::TransactionValidityError>;
80
81/// Convert the legacy `ApplyExtrinsicResult` type to the latest version.
82pub fn convert_to_latest(old: ApplyExtrinsicResult) -> crate::ApplyExtrinsicResult {
83	old.map(|outcome| {
84		outcome.map_err(|e| match e {
85			DispatchError::Other(s) => crate::DispatchError::Other(s),
86			DispatchError::CannotLookup => crate::DispatchError::CannotLookup,
87			DispatchError::BadOrigin => crate::DispatchError::BadOrigin,
88			DispatchError::Module(err) => crate::DispatchError::Module(crate::ModuleError {
89				index: err.index,
90				error: [err.error, 0, 0, 0],
91				message: err.message,
92			}),
93			DispatchError::ConsumerRemaining => crate::DispatchError::ConsumerRemaining,
94			DispatchError::NoProviders => crate::DispatchError::NoProviders,
95			DispatchError::TooManyConsumers => crate::DispatchError::TooManyConsumers,
96			DispatchError::Token(err) => crate::DispatchError::Token(err),
97			DispatchError::Arithmetic(err) => crate::DispatchError::Arithmetic(err),
98		})
99	})
100}