near_api_types/
errors.rs

1use std::array::TryFromSliceError;
2
3use near_openapi_types::TxExecutionError;
4
5use crate::transaction::result::ExecutionFailure;
6
7#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
8pub enum DecimalNumberParsingError {
9    #[error("Invalid number: {0}")]
10    InvalidNumber(String),
11    #[error("Too long whole part: {0}")]
12    LongWhole(String),
13    #[error("Too long fractional part: {0}")]
14    LongFractional(String),
15}
16
17#[derive(thiserror::Error, Debug)]
18pub enum KeyTypeError {
19    #[error("Invalid key format. Expected: [ed25519, secp256k1] but got: {0}")]
20    InvalidKeyFormat(String),
21    #[error("Invalid key type byte index: {0}")]
22    InvalidKeyTypeByteIndex(u8),
23}
24
25#[derive(thiserror::Error, Debug)]
26pub enum ParseKeyTypeError {
27    #[error("Unknown key type: {0}")]
28    UnknownKeyType(String),
29}
30
31#[derive(thiserror::Error, Debug)]
32pub enum DataConversionError {
33    #[error("Base64 decoding error: {0}")]
34    Base64DecodingError(#[from] base64::DecodeError),
35    #[error("Base58 decoding error: {0}")]
36    Base58DecodingError(#[from] bs58::decode::Error),
37    #[error("Borsh deserialization error: {0}")]
38    BorshDeserializationError(#[from] borsh::io::Error),
39    #[error("JSON deserialization error: {0}")]
40    JsonDeserializationError(#[from] serde_json::Error),
41    #[error("Parse int error: {0}")]
42    ParseIntError(#[from] std::num::ParseIntError),
43    #[error("Incorrect length: {0}")]
44    IncorrectLength(usize),
45    #[error("Invalid public key: {0}")]
46    InvalidKeyFormat(#[from] KeyTypeError),
47    #[error("Delegate action is not supported")]
48    DelegateActionNotSupported,
49    #[error("Invalid global contract identifier")]
50    InvalidGlobalContractIdentifier,
51}
52
53impl From<Vec<u8>> for DataConversionError {
54    fn from(value: Vec<u8>) -> Self {
55        Self::IncorrectLength(value.len())
56    }
57}
58
59impl From<TryFromSliceError> for DataConversionError {
60    fn from(_: TryFromSliceError) -> Self {
61        Self::IncorrectLength(0)
62    }
63}
64
65#[derive(thiserror::Error, Debug)]
66pub enum ExecutionError {
67    #[error("Data conversion error: {0}")]
68    DataConversionError(#[from] DataConversionError),
69    #[error("Execution failure: {0:?}")]
70    TransactionFailure(Box<ExecutionFailure>),
71    #[error("EOF while parsing a value at line 1 column 0")]
72    EofWhileParsingValue,
73    #[error("Executing transaction failed")]
74    TransactionExecutionFailed(Box<TxExecutionError>),
75    #[error("Execution pending or unknown")]
76    ExecutionPendingOrUnknown,
77}
78
79impl From<ExecutionFailure> for ExecutionError {
80    fn from(value: ExecutionFailure) -> Self {
81        Self::TransactionFailure(Box::new(value))
82    }
83}
84
85impl From<TxExecutionError> for ExecutionError {
86    fn from(value: TxExecutionError) -> Self {
87        Self::TransactionExecutionFailed(Box::new(value))
88    }
89}
90
91#[derive(thiserror::Error, Debug)]
92pub enum SecretKeyError {
93    #[error("Invalid secret key: {0}")]
94    InvalidSecp256k1SecretKey(secp256k1::Error),
95    #[error("Invalid conversion: {0}")]
96    InvalidConversion(#[from] DataConversionError),
97}
98
99impl From<secp256k1::Error> for SecretKeyError {
100    fn from(value: secp256k1::Error) -> Self {
101        Self::InvalidSecp256k1SecretKey(value)
102    }
103}
104
105impl From<Vec<u8>> for SecretKeyError {
106    fn from(value: Vec<u8>) -> Self {
107        Self::InvalidConversion(value.into())
108    }
109}
110
111impl From<TryFromSliceError> for SecretKeyError {
112    fn from(error: TryFromSliceError) -> Self {
113        Self::InvalidConversion(error.into())
114    }
115}
116
117#[derive(thiserror::Error, Debug)]
118pub enum SignatureErrors {
119    #[error("Invalid signature data: {0}")]
120    InvalidSignatureData(secp256k1::Error),
121}
122
123impl From<secp256k1::Error> for SignatureErrors {
124    fn from(value: secp256k1::Error) -> Self {
125        Self::InvalidSignatureData(value)
126    }
127}