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}
50
51impl From<Vec<u8>> for DataConversionError {
52    fn from(value: Vec<u8>) -> Self {
53        Self::IncorrectLength(value.len())
54    }
55}
56
57impl From<TryFromSliceError> for DataConversionError {
58    fn from(_: TryFromSliceError) -> Self {
59        Self::IncorrectLength(0)
60    }
61}
62
63#[derive(thiserror::Error, Debug)]
64pub enum ExecutionError {
65    #[error("Data conversion error: {0}")]
66    DataConversionError(#[from] DataConversionError),
67    #[error("Execution failure: {0:?}")]
68    TransactionFailure(Box<ExecutionFailure>),
69    #[error("EOF while parsing a value at line 1 column 0")]
70    EofWhileParsingValue,
71    #[error("Executing transaction failed")]
72    TransactionExecutionFailed(Box<TxExecutionError>),
73    #[error("Execution pending or unknown")]
74    ExecutionPendingOrUnknown,
75}
76
77impl From<ExecutionFailure> for ExecutionError {
78    fn from(value: ExecutionFailure) -> Self {
79        Self::TransactionFailure(Box::new(value))
80    }
81}
82
83impl From<TxExecutionError> for ExecutionError {
84    fn from(value: TxExecutionError) -> Self {
85        Self::TransactionExecutionFailed(Box::new(value))
86    }
87}
88
89#[derive(thiserror::Error, Debug)]
90pub enum SecretKeyError {
91    #[error("Invalid secret key: {0}")]
92    InvalidSecp256k1SecretKey(secp256k1::Error),
93    #[error("Invalid conversion: {0}")]
94    InvalidConversion(#[from] DataConversionError),
95}
96
97impl From<secp256k1::Error> for SecretKeyError {
98    fn from(value: secp256k1::Error) -> Self {
99        Self::InvalidSecp256k1SecretKey(value)
100    }
101}
102
103impl From<Vec<u8>> for SecretKeyError {
104    fn from(value: Vec<u8>) -> Self {
105        Self::InvalidConversion(value.into())
106    }
107}
108
109impl From<TryFromSliceError> for SecretKeyError {
110    fn from(error: TryFromSliceError) -> Self {
111        Self::InvalidConversion(error.into())
112    }
113}
114
115#[derive(thiserror::Error, Debug)]
116pub enum SignatureErrors {
117    #[error("Invalid signature data: {0}")]
118    InvalidSignatureData(secp256k1::Error),
119}
120
121impl From<secp256k1::Error> for SignatureErrors {
122    fn from(value: secp256k1::Error) -> Self {
123        Self::InvalidSignatureData(value)
124    }
125}