1use thiserror::Error;
37
38#[derive(Error, Debug)]
44pub enum NeoError {
45 #[error("Illegal argument: {0}")]
47 IllegalArgument(String),
48
49 #[error("Deserialization error: {0}")]
51 Deserialization(String),
52
53 #[error("Illegal state: {0}")]
55 IllegalState(String),
56
57 #[error("Index out of bounds: {0}")]
59 IndexOutOfBounds(String),
60
61 #[error("Invalid configuration: {0}")]
63 InvalidConfiguration(String),
64
65 #[error("Runtime error: {0}")]
67 Runtime(String),
68
69 #[error("Invalid data: {0}")]
71 InvalidData(String),
72
73 #[error("Unsupported operation: {0}")]
75 UnsupportedOperation(String),
76
77 #[error("Transaction error: {0}")]
79 Transaction(String),
80
81 #[error("Invalid script: {0}")]
83 InvalidScript(String),
84
85 #[error("Invalid format")]
87 InvalidFormat,
88
89 #[error("neo-rs not initialized")]
91 NeoNotInitialized,
92
93 #[error("Contract error: {0}")]
95 ContractError(#[from] ContractError),
96
97 #[error("Wallet error: {0}")]
99 WalletError(#[from] WalletError),
100
101 #[error("Sign error: {0}")]
103 SignError(#[from] SignError),
104
105 #[error("Transaction error: {0}")]
107 TransactionError(#[from] TransactionError),
108
109 #[error("Unexpected returned type")]
111 UnexpectedReturnType,
112
113 #[error("Invalid private key")]
115 InvalidPrivateKey,
116
117 #[error("Invalid public key")]
119 InvalidPublicKey,
120
121 #[error("Invalid address")]
123 InvalidAddress,
124
125 #[error("Invalid signature")]
127 InvalidSignature,
128
129 #[error("Invalid encoding {0}")]
131 InvalidEncoding(String),
132
133 #[error("Invalid op code")]
135 InvalidOpCode,
136
137 #[error("Numeric overflow")]
139 NumericOverflow,
140
141 #[error("Wif error {0}")]
143 WifError(String),
144
145 #[error("Provider error: {0}")]
147 ProviderError(#[from] ProviderError),
148
149 #[error("Codec error: {0}")]
151 CodecError(#[from] CodecError),
152
153 #[error("Type error: {0}")]
155 TypeError(#[from] TypeError),
156
157 #[error("Protocol error: {0}")]
159 ProtocolError(#[from] ProtocolError),
160
161 #[error("JSON RPC error: {0}")]
163 JsonRpcError(String),
164
165 #[error("IO error: {0}")]
167 IoError(#[from] std::io::Error),
168
169 #[error("Serialization error: {0}")]
171 SerializationError(String),
172}
173
174impl Into<TransactionError> for NeoError {
175 fn into(self) -> TransactionError {
176 TransactionError::TransactionConfiguration(self.to_string())
177 }
178}
179
180impl From<serde_json::Error> for NeoError {
181 fn from(err: serde_json::Error) -> Self {
182 NeoError::SerializationError(err.to_string())
183 }
184}
185
186impl From<String> for NeoError {
187 fn from(err: String) -> Self {
188 NeoError::IllegalState(err)
189 }
190}
191
192impl From<&str> for NeoError {
193 fn from(err: &str) -> Self {
194 NeoError::IllegalState(err.to_string())
195 }
196}
197
198use crate::builder::{BuilderError, TransactionError};
199use crate::codec::CodecError;
200use crate::crypto::{CryptoError, Nep2Error, SignError};
201use crate::neo_clients::ProviderError;
202use crate::neo_contract::ContractError;
203use crate::neo_protocol::ProtocolError;
204use crate::neo_wallets::WalletError;
205use crate::TypeError;
206
207impl From<BuilderError> for NeoError {
208 fn from(err: BuilderError) -> Self {
209 match err {
210 BuilderError::InvalidScript(msg) => NeoError::InvalidScript(msg),
211 BuilderError::InvalidOperation =>
212 NeoError::UnsupportedOperation("Invalid operation".to_string()),
213 BuilderError::InvalidArgument =>
214 NeoError::IllegalArgument("Invalid argument".to_string()),
215 BuilderError::InvalidState => NeoError::IllegalState("Invalid state".to_string()),
216 BuilderError::InvalidInvocation =>
217 NeoError::IllegalState("Invalid invocation".to_string()),
218 BuilderError::StackOverflow => NeoError::Runtime("Stack overflow".to_string()),
219 BuilderError::OutOfGas => NeoError::Runtime("Out of gas".to_string()),
220 BuilderError::OutOfMemory => NeoError::Runtime("Out of memory".to_string()),
221 BuilderError::OutOfCycles => NeoError::Runtime("Out of cycles".to_string()),
222 BuilderError::UnknownError => NeoError::Runtime("Unknown error".to_string()),
223 BuilderError::UnsupportedOperation(msg) => NeoError::UnsupportedOperation(msg),
224 BuilderError::SignerConfiguration(msg) =>
225 NeoError::IllegalState(format!("Signer configuration error: {}", msg)),
226 BuilderError::TransactionConfiguration(msg) => NeoError::Transaction(msg),
227 BuilderError::InvalidConfiguration(msg) => NeoError::InvalidConfiguration(msg),
228 BuilderError::TooManySigners(msg) =>
229 NeoError::IllegalState(format!("Too many signers: {}", msg)),
230 BuilderError::IllegalState(msg) => NeoError::IllegalState(msg),
231 BuilderError::IllegalArgument(msg) => NeoError::IllegalArgument(msg),
232 BuilderError::CodecError(err) => NeoError::CodecError(err),
233 BuilderError::CryptoError(err) => NeoError::from(err),
234 BuilderError::ProviderError(err) => NeoError::ProviderError(err),
235 BuilderError::TransactionError(err) => NeoError::TransactionError(*err),
236 }
237 }
238}
239
240impl From<CryptoError> for NeoError {
241 fn from(err: CryptoError) -> Self {
242 match err {
243 CryptoError::InvalidPassphrase(msg) =>
244 NeoError::IllegalArgument(format!("Invalid passphrase: {}", msg)),
245 CryptoError::InvalidFormat(msg) => NeoError::InvalidFormat,
246 CryptoError::HeaderOutOfRange(byte) =>
247 NeoError::IllegalArgument(format!("Header byte out of range: {}", byte)),
248 CryptoError::RecoverFailed =>
249 NeoError::IllegalState("Could not recover public key from signature".to_string()),
250 CryptoError::InvalidPublicKey => NeoError::InvalidPublicKey,
251 CryptoError::InvalidPrivateKey => NeoError::InvalidPrivateKey,
252 CryptoError::P256Error(err) => NeoError::IllegalState(format!("P256 error: {}", err)),
253 CryptoError::SigningError => NeoError::SignError(SignError::RecoverFailed),
254 CryptoError::SignatureVerificationError => NeoError::InvalidSignature,
255 CryptoError::FromHexError(err) =>
256 NeoError::InvalidEncoding(format!("Hex error: {}", err)),
257 CryptoError::DecryptionError(msg) =>
258 NeoError::IllegalState(format!("Decryption error: {}", msg)),
259 CryptoError::KeyError(msg) => NeoError::IllegalState(format!("Key error: {}", msg)),
260 }
261 }
262}
263
264impl From<Nep2Error> for NeoError {
265 fn from(err: Nep2Error) -> Self {
266 match err {
267 Nep2Error::InvalidPassphrase(msg) =>
268 NeoError::IllegalArgument(format!("Invalid NEP-2 passphrase: {}", msg)),
269 Nep2Error::InvalidFormat(msg) =>
270 NeoError::InvalidEncoding(format!("Invalid NEP-2 format: {}", msg)),
271 }
272 }
273}
274
275impl From<reqwest::Error> for NeoError {
277 fn from(err: reqwest::Error) -> Self {
278 NeoError::IoError(std::io::Error::new(
279 std::io::ErrorKind::Other,
280 format!("HTTP error: {}", err),
281 ))
282 }
283}
284
285impl From<hex::FromHexError> for NeoError {
287 fn from(err: hex::FromHexError) -> Self {
288 NeoError::InvalidEncoding(format!("Hex error: {}", err))
289 }
290}
291
292impl From<std::num::ParseIntError> for NeoError {
294 fn from(err: std::num::ParseIntError) -> Self {
295 NeoError::IllegalArgument(format!("Integer parsing error: {}", err))
296 }
297}
298
299impl From<std::str::Utf8Error> for NeoError {
301 fn from(err: std::str::Utf8Error) -> Self {
302 NeoError::InvalidEncoding(format!("UTF-8 error: {}", err))
303 }
304}