foundry_evm_core/backend/
error.rs

1use alloy_primitives::Address;
2pub use foundry_fork_db::{DatabaseError, DatabaseResult};
3use revm::primitives::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    #[error("{0}")]
28    Other(String),
29}
30
31impl BackendError {
32    /// Create a new error with a message
33    pub fn msg(msg: impl Into<String>) -> Self {
34        Self::Message(msg.into())
35    }
36
37    /// Create a new error with a message
38    pub fn display(msg: impl std::fmt::Display) -> Self {
39        Self::Message(msg.to_string())
40    }
41}
42
43impl From<tokio::task::JoinError> for BackendError {
44    fn from(value: tokio::task::JoinError) -> Self {
45        Self::display(value)
46    }
47}
48
49impl From<Infallible> for BackendError {
50    fn from(value: Infallible) -> Self {
51        match value {}
52    }
53}
54
55// Note: this is mostly necessary to use some revm internals that return an [EVMError]
56impl<T: Into<Self>> From<EVMError<T>> for BackendError {
57    fn from(err: EVMError<T>) -> Self {
58        match err {
59            EVMError::Database(err) => err.into(),
60            EVMError::Custom(err) => Self::msg(err),
61            EVMError::Header(err) => Self::msg(err.to_string()),
62            EVMError::Precompile(err) => Self::msg(err),
63            EVMError::Transaction(err) => Self::msg(err.to_string()),
64        }
65    }
66}