neo3/neo_wallets/
error.rs

1use crate::{builder::BuilderError, crypto::CryptoError, neo_wallets::WalletError, TypeError};
2use hex::FromHexError;
3use thiserror::Error;
4
5/// Represents errors that can occur within the signing process.
6///
7/// This enum encapsulates a variety of errors that may arise when performing
8/// operations related to signing transactions, messages, or performing cryptographic
9/// operations within a blockchain context. It includes errors related to invalid input
10/// data (like passphrases or addresses), failures in underlying cryptographic operations,
11/// and errors from external libraries used for tasks such as hex encoding/decoding or
12/// type conversion.
13///
14/// Variants of this enum are designed to be converted from errors thrown by these
15/// operations, allowing for a unified error handling interface throughout the signing
16/// process.
17///
18/// # Variants
19///
20/// - `InvalidPassphrase`: Occurs when a passphrase does not meet validation criteria.
21/// - `InvalidAddress`: Triggered by malformed blockchain addresses.
22/// - `BuilderError`: Wraps errors from the construction of cryptographic objects.
23/// - `WalletError`: Encompasses errors from wallet operations.
24/// - `FromHexError`: Pertains to failures in hexadecimal to binary data conversion.
25/// - `CryptoError`: Covers general cryptographic failures.
26/// - `RustcFromHexError`: Specific to hex decoding issues via `rustc_serialize`.
27/// - `TypeError`: Indicates failures in type conversion or coercion.
28///
29/// # Examples
30///
31/// Handling a `SignerError` in a function that performs signing operations might look
32/// like this:
33///
34/// ```
35/// use neo3::prelude::*;
36///  async fn sign_data(data: &[u8]) -> Result<(), wallets::SignerError> {
37///     // Example function body
38///     Ok(())
39/// }
40///
41/// async fn example_usage() {
42///     let data = b"example data";
43///
44///     match sign_data(data).await {
45///         Ok(_) => println!("Data signed successfully"),
46///         Err(e) => match e {
47///             wallets::SignerError::InvalidPassphrase(_) => println!("Invalid passphrase provided"),
48///             wallets::SignerError::InvalidAddress => println!("Invalid address"),
49///             // Handle other errors accordingly
50///             _ => println!("An error occurred: {:?}", e),
51///         },
52///     }
53/// }
54/// ```
55#[derive(Debug, Error)]
56pub enum SignerError {
57	/// Represents an error when an invalid passphrase is provided.
58	/// This could happen during the decryption of a private key or any operation
59	/// that requires passphrase verification.
60	#[error("Invalid passphrase: {0}")]
61	InvalidPassphrase(String),
62
63	/// Indicates that the provided address is not valid.
64	/// This error might occur if an address does not comply with expected formats or checksums.
65	#[error("Invalid address")]
66	InvalidAddress,
67
68	/// Wraps errors related to building or configuring objects, possibly during
69	/// the setup of cryptographic operations or when constructing complex objects
70	/// that have specific requirements.
71	#[error(transparent)]
72	BuilderError(#[from] BuilderError),
73
74	/// Encapsulates errors that originate from wallet operations.
75	/// This can include issues with creating, loading, or performing transactions with wallets.
76	#[error(transparent)]
77	WalletError(#[from] WalletError),
78
79	/// Covers general cryptographic errors such as failures in hashing, signature generation,
80	/// or encryption/decryption processes.
81	#[error(transparent)]
82	CryptoError(#[from] CryptoError),
83
84	/// An error for hex decoding issues.
85	#[error("Hex decoding error: {0}")]
86	RustcFromHexError(#[from] hex::FromHexError),
87
88	/// Indicates a failure related to type conversion or coercion.
89	/// This variant is useful for signaling issues when trying to convert between incompatible types,
90	/// such as when deserializing data into a specific structure.
91	#[error(transparent)]
92	TypeError(#[from] TypeError),
93}