Skip to main content

neo_types/
error.rs

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