1use std::num::TryFromIntError;
19
20use serde::{Deserialize, Serialize};
21use tari_max_size::MaxSizeVecError;
22use tari_utilities::ByteArrayError;
23use thiserror::Error;
24
25#[derive(Debug, Clone, Error, PartialEq, Eq, Serialize, Deserialize)]
26pub enum ScriptError {
27 #[error("The script failed with an explicit Return")]
28 Return,
29 #[error("The stack cannot exceed MAX_STACK_SIZE items")]
30 StackOverflow,
31 #[error("The script completed execution with a stack size other than one")]
32 NonUnitLengthStack,
33 #[error("Tried to pop an element off an empty stack")]
34 StackUnderflow,
35 #[error("An operand was applied to incompatible types")]
36 IncompatibleTypes,
37 #[error("A script opcode resulted in a value that exceeded the maximum or minimum value")]
38 ValueExceedsBounds,
39 #[error("The script encountered an invalid opcode")]
40 InvalidOpcode,
41 #[error("The script is missing closing opcodes (Else or EndIf)")]
42 MissingOpcode,
43 #[error("The script contained an invalid signature")]
44 InvalidSignature,
45 #[error("The serialised stack contained invalid input")]
46 InvalidInput,
47 #[error("The script contained invalid data")]
48 InvalidData,
49 #[error("A verification opcode failed, aborting the script immediately")]
50 VerifyFailed,
51 #[error("as_hash requires a Digest function that returns at least 32 bytes")]
52 InvalidDigest,
53 #[error("A compare opcode failed, aborting the script immediately with reason: `{0}`")]
54 CompareFailed(String),
55 #[error("Max sized vector error: {0}")]
56 MaxSizeVecError(#[from] MaxSizeVecError),
57}
58
59impl ScriptError {
60 pub fn to_std_io_error(self) -> std::io::Error {
61 std::io::Error::other(self.to_string())
62 }
63}
64
65impl From<TryFromIntError> for ScriptError {
66 fn from(_err: TryFromIntError) -> ScriptError {
67 ScriptError::ValueExceedsBounds
68 }
69}
70
71impl From<ByteArrayError> for ScriptError {
72 fn from(_err: ByteArrayError) -> ScriptError {
73 ScriptError::InvalidData
74 }
75}