This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
errors.rs
107 lines (96 loc) · 4.28 KB
/
errors.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use blockifier::execution::errors::ContractClassError;
use blockifier::state::errors::StateError;
use blockifier::transaction::errors::{
ParseError, TransactionExecutionError, TransactionPreValidationError,
};
use blockifier::transaction::transaction_types::TransactionType;
use cairo_vm::types::errors::program_errors::ProgramError;
use pyo3::create_exception;
use pyo3::exceptions::PyException;
use pyo3::prelude::*;
use starknet_api::StarknetApiError;
use thiserror::Error;
pub type NativeBlockifierResult<T> = Result<T, NativeBlockifierError>;
/// Defines `NativeBlockifierError` variants, their respective Python types, and implements a
/// conversion to `PyErr`.
macro_rules! native_blockifier_errors {
($(($variant_name:ident, $from_error_type:ty, $py_error_name:ident)),*) => {
#[derive(Debug, Error)]
pub enum NativeBlockifierError {
$(
#[error(transparent)]
$variant_name(#[from] $from_error_type)
),*
}
// Utility method for Python code to know which error types exist.
#[pyfunction]
pub fn py_error_names() -> Vec<String> {
vec![$(String::from(stringify!($py_error_name))),*]
}
// Creates new types that implement `Into<PyException>`.
$(create_exception!(native_blockifier, $py_error_name, PyException);)*
// Call to register all Python exceptions (and name list getter) in the native_blockifier
// module.
pub fn add_py_exceptions(py: Python<'_>, py_module: &PyModule) -> PyResult<()> {
$(py_module.add(stringify!($py_error_name), py.get_type::<$py_error_name>())?;)*
py_module.add_function(wrap_pyfunction!(py_error_names, py)?)?;
Ok(())
}
impl From<NativeBlockifierError> for PyErr {
fn from(error: NativeBlockifierError) -> PyErr {
match error {
$(NativeBlockifierError::$variant_name(error) => $py_error_name::new_err(
// Constructs with the tuple `(error_code, error_message)`.
(
String::from("native_blockifier.") + stringify!($py_error_name),
format!("{:?}", error),
)
)),*
}
}
}
};
}
native_blockifier_errors!(
(NativeBlockifierInputError, NativeBlockifierInputError, PyNativeBlockifierInputError),
(ProgramError, ProgramError, PyProgramError),
(Pyo3Error, PyErr, PyPyo3Error),
(SerdeError, serde_json::Error, PySerdeError),
(StarknetApiError, StarknetApiError, PyStarknetApiError),
(StateError, StateError, PyStateError),
(StorageError, papyrus_storage::StorageError, PyStorageError),
(TransactionExecutionError, TransactionExecutionError, PyTransactionExecutionError),
(TransactionPreValidationError, TransactionPreValidationError, PyTransactionPreValidationError),
(ContractClassError, ContractClassError, PyContractClassError)
);
#[derive(Debug, Error)]
pub enum NativeBlockifierInputError {
#[error("Max steps per tx out of range: {0}")]
MaxStepsPerTxOutOfRange(u32),
#[error("Max validate steps per tx out of range: {0}")]
MaxValidateStepsPerTxOutOfRange(u32),
#[error(transparent)]
InvalidNativeBlockifierInputError(#[from] InvalidNativeBlockifierInputError),
#[error(transparent)]
ParseError(#[from] ParseError),
#[error(transparent)]
ProgramError(#[from] ProgramError),
#[error(transparent)]
StarknetApiError(#[from] StarknetApiError),
#[error("Contract class of version {version} is unsupported.")]
UnsupportedContractClassVersion { version: usize },
#[error("Transaction of type {tx_type:?} is unsupported in version {version}.")]
UnsupportedTransactionVersion { tx_type: TransactionType, version: usize },
}
#[derive(Debug, Error)]
pub enum InvalidNativeBlockifierInputError {
#[error("Invalid Wei gas price: {0}.")]
InvalidGasPriceWei(u128),
#[error("Invalid Fri gas price: {0}.")]
InvalidGasPriceFri(u128),
#[error("Invalid Wei data gas price: {0}.")]
InvalidDataGasPriceWei(u128),
#[error("Invalid Fri data gas price: {0}.")]
InvalidDataGasPriceFri(u128),
}
create_exception!(native_blockifier, UndeclaredClassHashError, PyException);