Skip to main content

foundry_evm_core/backend/
error.rs

1use alloy_primitives::Address;
2pub use foundry_fork_db::{DatabaseError, DatabaseResult};
3use revm::context_interface::result::EVMError;
4use std::convert::Infallible;
5
6pub type BackendResult<T> = Result<T, BackendError>;
7
8/// Errors that can happen when working with [`revm::Database`]
9#[derive(Debug, thiserror::Error)]
10#[expect(missing_docs)]
11pub enum BackendError {
12    #[error("{0}")]
13    Message(String),
14    #[error("cheatcodes are not enabled for {0}; see `vm.allowCheatcodes(address)`")]
15    NoCheats(Address),
16    #[error(transparent)]
17    Database(#[from] DatabaseError),
18    #[error("failed to fetch account info for {0}")]
19    MissingAccount(Address),
20    #[error(
21        "CREATE2 Deployer (0x4e59b44847b379578588920ca78fbf26c0b4956c) not present on this chain.\n\
22         For a production environment, you can deploy it using the pre-signed transaction from \
23         https://github.com/Arachnid/deterministic-deployment-proxy.\n\
24         For a test environment, you can use `etch` to place the required bytecode at that address."
25    )]
26    MissingCreate2Deployer,
27}
28
29impl BackendError {
30    /// Create a new error with a message
31    pub fn msg(msg: impl Into<String>) -> Self {
32        Self::Message(msg.into())
33    }
34
35    /// Create a new error with a message
36    pub fn display(msg: impl std::fmt::Display) -> Self {
37        Self::Message(msg.to_string())
38    }
39}
40
41impl From<tokio::task::JoinError> for BackendError {
42    fn from(value: tokio::task::JoinError) -> Self {
43        Self::display(value)
44    }
45}
46
47impl From<Infallible> for BackendError {
48    fn from(value: Infallible) -> Self {
49        match value {}
50    }
51}
52
53// Note: this is mostly necessary to use some revm internals that return an [EVMError]
54impl<T: Into<Self>> From<EVMError<T>> for BackendError {
55    fn from(err: EVMError<T>) -> Self {
56        match err {
57            EVMError::Database(err) => err.into(),
58            EVMError::Custom(err) => Self::msg(err),
59            EVMError::Header(err) => Self::msg(err.to_string()),
60            EVMError::Transaction(err) => Self::msg(err.to_string()),
61        }
62    }
63}