use std::num::TryFromIntError;
use serde::{Deserialize, Serialize};
use tari_max_size::MaxSizeVecError;
use tari_utilities::ByteArrayError;
use thiserror::Error;
#[derive(Debug, Clone, Error, PartialEq, Eq, Serialize, Deserialize)]
pub enum ScriptError {
#[error("The script failed with an explicit Return")]
Return,
#[error("The stack cannot exceed MAX_STACK_SIZE items")]
StackOverflow,
#[error("The script completed execution with a stack size other than one")]
NonUnitLengthStack,
#[error("Tried to pop an element off an empty stack")]
StackUnderflow,
#[error("An operand was applied to incompatible types")]
IncompatibleTypes,
#[error("A script opcode resulted in a value that exceeded the maximum or minimum value")]
ValueExceedsBounds,
#[error("The script encountered an invalid opcode")]
InvalidOpcode,
#[error("The script is missing closing opcodes (Else or EndIf)")]
MissingOpcode,
#[error("The script contained an invalid signature")]
InvalidSignature,
#[error("The serialised stack contained invalid input")]
InvalidInput,
#[error("The script contained invalid data")]
InvalidData,
#[error("A verification opcode failed, aborting the script immediately")]
VerifyFailed,
#[error("as_hash requires a Digest function that returns at least 32 bytes")]
InvalidDigest,
#[error("A compare opcode failed, aborting the script immediately with reason: `{0}`")]
CompareFailed(String),
#[error("Max sized vector error: {0}")]
MaxSizeVecError(#[from] MaxSizeVecError),
}
impl ScriptError {
pub fn to_std_io_error(self) -> std::io::Error {
std::io::Error::other(self.to_string())
}
}
impl From<TryFromIntError> for ScriptError {
fn from(_err: TryFromIntError) -> ScriptError {
ScriptError::ValueExceedsBounds
}
}
impl From<ByteArrayError> for ScriptError {
fn from(_err: ByteArrayError) -> ScriptError {
ScriptError::InvalidData
}
}