Add arithmetic dispatch errors. (#8726)

* Add arithmetic dispatch errors.

* Replace custom overflow errors.

* Replace custom underflow and division by zero errors.

* Replace overflow/underflow in token error.

* Add token and arithmetic errors in dispatch error equality test.

* Trigger CI.
This commit is contained in:
Shaun Wang
2021-05-10 20:14:02 +12:00
committed by GitHub
parent 655ebc95fb
commit 2a38b23062
12 changed files with 88 additions and 60 deletions
+45 -6
View File
@@ -468,6 +468,8 @@ pub enum DispatchError {
NoProviders,
/// An error to do with tokens.
Token(TokenError),
/// An arithmetic error.
Arithmetic(ArithmeticError),
}
/// Result of a `Dispatchable` which contains the `DispatchResult` and additional information about
@@ -542,10 +544,6 @@ pub enum TokenError {
UnknownAsset,
/// Funds exist but are frozen.
Frozen,
/// An underflow would occur.
Underflow,
/// An overflow would occur.
Overflow,
}
impl From<TokenError> for &'static str {
@@ -557,8 +555,6 @@ impl From<TokenError> for &'static str {
TokenError::CannotCreate => "Account cannot be created",
TokenError::UnknownAsset => "The asset in question is unknown",
TokenError::Frozen => "Funds exist but are frozen",
TokenError::Underflow => "An underflow would occur",
TokenError::Overflow => "An overflow would occur",
}
}
}
@@ -569,6 +565,34 @@ impl From<TokenError> for DispatchError {
}
}
/// Arithmetic errors.
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum ArithmeticError {
/// Underflow.
Underflow,
/// Overflow.
Overflow,
/// Division by zero.
DivisionByZero,
}
impl From<ArithmeticError> for &'static str {
fn from(e: ArithmeticError) -> &'static str {
match e {
ArithmeticError::Underflow => "An underflow would occur",
ArithmeticError::Overflow => "An overflow would occur",
ArithmeticError::DivisionByZero => "Division by zero",
}
}
}
impl From<ArithmeticError> for DispatchError {
fn from(e: ArithmeticError) -> DispatchError {
Self::Arithmetic(e)
}
}
impl From<&'static str> for DispatchError {
fn from(err: &'static str) -> DispatchError {
Self::Other(err)
@@ -585,6 +609,7 @@ impl From<DispatchError> for &'static str {
DispatchError::ConsumerRemaining => "Consumer remaining",
DispatchError::NoProviders => "No providers",
DispatchError::Token(e) => e.into(),
DispatchError::Arithmetic(e) => e.into(),
}
}
}
@@ -616,6 +641,10 @@ impl traits::Printable for DispatchError {
Self::Token(e) => {
"Token error: ".print();
<&'static str>::from(*e).print();
},
Self::Arithmetic(e) => {
"Arithmetic error: ".print();
<&'static str>::from(*e).print();
}
}
}
@@ -643,6 +672,7 @@ impl PartialEq for DispatchError {
(Token(l), Token(r)) => l == r,
(Other(l), Other(r)) => l == r,
(Arithmetic(l), Arithmetic(r)) => l == r,
(
Module { index: index_l, error: error_l, .. },
@@ -903,6 +933,15 @@ mod tests {
Module { index: 2, error: 1, message: None },
ConsumerRemaining,
NoProviders,
Token(TokenError::NoFunds),
Token(TokenError::WouldDie),
Token(TokenError::BelowMinimum),
Token(TokenError::CannotCreate),
Token(TokenError::UnknownAsset),
Token(TokenError::Frozen),
Arithmetic(ArithmeticError::Overflow),
Arithmetic(ArithmeticError::Underflow),
Arithmetic(ArithmeticError::DivisionByZero),
];
for (i, variant) in variants.iter().enumerate() {
for (j, other_variant) in variants.iter().enumerate() {