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 ed25519 secret key: {0}")]
96    InvalidEd25519SecretKey(ed25519_dalek::SignatureError),
97    #[error("Invalid conversion: {0}")]
98    InvalidConversion(#[from] DataConversionError),
99}
100
101impl From<ed25519_dalek::SignatureError> for SecretKeyError {
102    fn from(value: ed25519_dalek::SignatureError) -> Self {
103        Self::InvalidEd25519SecretKey(value)
104    }
105}
106
107impl From<secp256k1::Error> for SecretKeyError {
108    fn from(value: secp256k1::Error) -> Self {
109        Self::InvalidSecp256k1SecretKey(value)
110    }
111}
112
113impl From<Vec<u8>> for SecretKeyError {
114    fn from(value: Vec<u8>) -> Self {
115        Self::InvalidConversion(value.into())
116    }
117}
118
119impl From<TryFromSliceError> for SecretKeyError {
120    fn from(error: TryFromSliceError) -> Self {
121        Self::InvalidConversion(error.into())
122    }
123}
124
125#[derive(thiserror::Error, Debug)]
126pub enum SignatureErrors {
127    #[error("Invalid signature data: {0}")]
128    InvalidSignatureData(secp256k1::Error),
129}
130
131impl From<secp256k1::Error> for SignatureErrors {
132    fn from(value: secp256k1::Error) -> Self {
133        Self::InvalidSignatureData(value)
134    }
135}