1use std::fmt;
5use std::string::String;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum NeoError {
13 InvalidOperation,
15 InvalidArgument,
17 InvalidType,
19 OutOfBounds,
21 DivisionByZero,
23 Overflow,
25 Underflow,
27 NullReference,
29 InvalidState,
31 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 pub fn new(message: &str) -> Self {
63 NeoError::Custom(message.to_string())
64 }
65
66 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 pub fn message(&self) -> &str {
87 match self {
88 NeoError::Custom(msg) => msg,
89 _ => self.as_str(),
90 }
91 }
92
93 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
112pub type NeoResult<T> = Result<T, NeoError>;