Skip to main content

neo_types/
error.rs

1use std::fmt;
2use std::string::String;
3
4/// Neo N3 Error type
5///
6/// Represents all possible error conditions that can occur during
7/// Neo N3 smart contract execution.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum NeoError {
10    /// Operation is not valid in the current context
11    InvalidOperation,
12    /// Argument has an invalid value or type
13    InvalidArgument,
14    /// Type mismatch encountered during execution
15    InvalidType,
16    /// Index or offset is out of valid bounds
17    OutOfBounds,
18    /// Attempted division by zero
19    DivisionByZero,
20    /// Arithmetic overflow occurred
21    Overflow,
22    /// Arithmetic underflow occurred
23    Underflow,
24    /// Dereferenced a null or invalid reference
25    NullReference,
26    /// Internal state is invalid or corrupted
27    InvalidState,
28    /// Application-specific error with custom message
29    Custom(String),
30}
31
32impl fmt::Display for NeoError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            NeoError::InvalidOperation => write!(f, "Invalid operation: the requested operation cannot be performed in the current context"),
36            NeoError::InvalidArgument => write!(f, "Invalid argument: one or more arguments have invalid values or types"),
37            NeoError::InvalidType => write!(f, "Invalid type: type mismatch encountered during execution"),
38            NeoError::OutOfBounds => write!(f, "Out of bounds: index or offset exceeds valid range"),
39            NeoError::DivisionByZero => write!(f, "Division by zero: cannot divide by zero"),
40            NeoError::Overflow => write!(f, "Overflow: arithmetic operation resulted in overflow"),
41            NeoError::Underflow => write!(f, "Underflow: arithmetic operation resulted in underflow"),
42            NeoError::NullReference => write!(f, "Null reference: attempted to access a null or invalid reference"),
43            NeoError::InvalidState => write!(f, "Invalid state: internal state is invalid or corrupted"),
44            NeoError::Custom(msg) => write!(f, "{}", msg),
45        }
46    }
47}
48
49impl NeoError {
50    /// Creates a new custom error with the given message.
51    ///
52    /// # Examples
53    ///
54    /// ```
55    /// use neo_types::NeoError;
56    ///
57    /// let err = NeoError::new("Custom error message");
58    /// ```
59    pub fn new(message: &str) -> Self {
60        NeoError::Custom(message.to_string())
61    }
62
63    /// Returns the error message if this is a custom error, otherwise returns a generic description.
64    pub fn message(&self) -> &str {
65        match self {
66            NeoError::Custom(msg) => msg,
67            _ => self.as_str(),
68        }
69    }
70
71    /// Returns a static string description of the error variant.
72    pub fn as_str(&self) -> &'static str {
73        match self {
74            NeoError::InvalidOperation => "InvalidOperation",
75            NeoError::InvalidArgument => "InvalidArgument",
76            NeoError::InvalidType => "InvalidType",
77            NeoError::OutOfBounds => "OutOfBounds",
78            NeoError::DivisionByZero => "DivisionByZero",
79            NeoError::Overflow => "Overflow",
80            NeoError::Underflow => "Underflow",
81            NeoError::NullReference => "NullReference",
82            NeoError::InvalidState => "InvalidState",
83            NeoError::Custom(_) => "Custom",
84        }
85    }
86}
87
88/// Neo N3 Result type
89pub type NeoResult<T> = Result<T, NeoError>;