use crate::arithmetic::ArithmeticsError;
const MAX_USER_ERROR: u16 = 32767;
const USER_ERROR_TOO_HIGH: u16 = 32768;
const UNWRAP_ERROR: u16 = u16::MAX;
const CODE_ADDITION_OVERFLOW: u16 = 7;
const CODE_SUBTRACTION_OVERFLOW: u16 = 8;
const CODE_NON_PAYABLE: u16 = 9;
const CODE_TRANSFER_TO_CONTRACT: u16 = 10;
const CODE_REENTRANT_CALL: u16 = 11;
#[derive(Clone, Debug, PartialEq)]
pub enum OdraError {
ExecutionError(ExecutionError),
VmError(VmError)
}
impl From<ArithmeticsError> for ExecutionError {
fn from(error: ArithmeticsError) -> Self {
match error {
ArithmeticsError::AdditionOverflow => {
Self::internal(CODE_ADDITION_OVERFLOW, "Addition overflow")
}
ArithmeticsError::SubtractingOverflow => {
Self::internal(CODE_SUBTRACTION_OVERFLOW, "Subtracting overflow")
}
}
}
}
impl From<ArithmeticsError> for OdraError {
fn from(error: ArithmeticsError) -> Self {
Into::<ExecutionError>::into(error).into()
}
}
impl From<Box<dyn std::any::Any + Send>> for OdraError {
fn from(_: Box<dyn std::any::Any + Send>) -> Self {
OdraError::VmError(VmError::Panic)
}
}
#[derive(Clone, Debug)]
pub struct ExecutionError(u16, String);
impl ExecutionError {
pub fn new(code: u16, msg: &str) -> Self {
if code > MAX_USER_ERROR {
Self(
USER_ERROR_TOO_HIGH,
String::from("User error too high. The code should be in range 0..32767.")
)
} else {
Self(code, String::from(msg))
}
}
pub fn unwrap_error() -> Self {
Self::new(UNWRAP_ERROR, "Unwrap error")
}
pub fn code(&self) -> u16 {
self.0
}
pub fn non_payable() -> Self {
Self::internal(CODE_NON_PAYABLE, "Method does not accept deposit")
}
pub fn can_not_transfer_to_contract() -> Self {
Self::internal(
CODE_TRANSFER_TO_CONTRACT,
"Can't transfer tokens to contract."
)
}
pub fn reentrant_call() -> Self {
Self::internal(CODE_REENTRANT_CALL, "Reentrant call.")
}
fn internal(code: u16, msg: &str) -> Self {
ExecutionError(code + USER_ERROR_TOO_HIGH, String::from(msg))
}
}
impl PartialEq for ExecutionError {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl From<ExecutionError> for OdraError {
fn from(error: ExecutionError) -> Self {
Self::ExecutionError(error)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum VmError {
Serialization,
Deserialization,
BalanceExceeded,
NoSuchMethod(String),
InvalidContractAddress,
InvalidContext,
MissingArg,
Other(String),
Panic
}
pub enum CollectionError {
IndexOutOfBounds
}
impl From<CollectionError> for ExecutionError {
fn from(error: CollectionError) -> Self {
match error {
CollectionError::IndexOutOfBounds => Self::internal(9, "Index out of bounds")
}
}
}
impl From<CollectionError> for OdraError {
fn from(error: CollectionError) -> Self {
Into::<ExecutionError>::into(error).into()
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum AddressError {
ZeroAddress,
AddressCreationError
}
impl From<AddressError> for ExecutionError {
fn from(error: AddressError) -> Self {
match error {
AddressError::ZeroAddress => Self::internal(10, "Zero address"),
AddressError::AddressCreationError => Self::internal(11, "Address creation error")
}
}
}
impl From<AddressError> for OdraError {
fn from(error: AddressError) -> Self {
Into::<ExecutionError>::into(error).into()
}
}