1use alloy_primitives::Address;
2pub use foundry_fork_db::{DatabaseError, DatabaseResult};
3use revm::primitives::EVMError;
4use std::convert::Infallible;
56pub type BackendResult<T> = Result<T, BackendError>;
78/// Errors that can happen when working with [`revm::Database`]
9#[derive(Debug, thiserror::Error)]
10#[expect(missing_docs)]
11pub enum BackendError {
12#[error("{0}")]
13Message(String),
14#[error("cheatcodes are not enabled for {0}; see `vm.allowCheatcodes(address)`")]
15NoCheats(Address),
16#[error(transparent)]
17Database(#[from] DatabaseError),
18#[error("failed to fetch account info for {0}")]
19MissingAccount(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)]
26MissingCreate2Deployer,
27#[error("{0}")]
28Other(String),
29}
3031impl BackendError {
32/// Create a new error with a message
33pub fn msg(msg: impl Into<String>) -> Self {
34Self::Message(msg.into())
35 }
3637/// Create a new error with a message
38pub fn display(msg: impl std::fmt::Display) -> Self {
39Self::Message(msg.to_string())
40 }
41}
4243impl From<tokio::task::JoinError> for BackendError {
44fn from(value: tokio::task::JoinError) -> Self {
45Self::display(value)
46 }
47}
4849impl From<Infallible> for BackendError {
50fn from(value: Infallible) -> Self {
51match value {}
52 }
53}
5455// Note: this is mostly necessary to use some revm internals that return an [EVMError]
56impl<T: Into<Self>> From<EVMError<T>> for BackendError {
57fn from(err: EVMError<T>) -> Self {
58match 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}