Skip to main content

ave_core/evaluation/compiler/
error.rs

1use crate::model::common::contract::ContractError;
2use thiserror::Error;
3
4#[derive(Debug, Error, Clone)]
5pub enum CompilerError {
6    #[error("base64 decode failed: {details}")]
7    Base64DecodeFailed { details: String },
8
9    #[error("directory creation failed [{path}]: {details}")]
10    DirectoryCreationFailed { path: String, details: String },
11
12    #[error("file write failed [{path}]: {details}")]
13    FileWriteFailed { path: String, details: String },
14
15    #[error("file read failed [{path}]: {details}")]
16    FileReadFailed { path: String, details: String },
17
18    #[error("cargo build failed: {details}")]
19    CargoBuildFailed { details: String },
20
21    #[error("compilation failed")]
22    CompilationFailed,
23
24    #[error("missing helper: {name}")]
25    MissingHelper { name: &'static str },
26
27    #[error("wasm precompile failed: {details}")]
28    WasmPrecompileFailed { details: String },
29
30    #[error("wasm deserialization failed: {details}")]
31    WasmDeserializationFailed { details: String },
32
33    #[error("invalid module: {kind}")]
34    InvalidModule { kind: InvalidModuleKind },
35
36    #[error("fuel limit error: {details}")]
37    FuelLimitError { details: String },
38
39    #[error("instantiation failed: {details}")]
40    InstantiationFailed { details: String },
41
42    #[error("entry point not found: {function}")]
43    EntryPointNotFound { function: &'static str },
44
45    #[error("contract execution failed: {details}")]
46    ContractExecutionFailed { details: String },
47
48    #[error("serialization error [{context}]: {details}")]
49    SerializationError {
50        context: &'static str,
51        details: String,
52    },
53
54    #[error("invalid contract output: {details}")]
55    InvalidContractOutput { details: String },
56
57    #[error("memory allocation failed: {details}")]
58    MemoryAllocationFailed { details: String },
59
60    #[error("contract check failed: {error}")]
61    ContractCheckFailed { error: String },
62}
63
64#[derive(Debug, Clone)]
65pub enum InvalidModuleKind {
66    UnknownImportFunction { name: String },
67    NonFunctionImport { import_type: String },
68    MissingImports { missing: Vec<String> },
69}
70
71impl std::fmt::Display for InvalidModuleKind {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        match self {
74            Self::UnknownImportFunction { name } => {
75                write!(
76                    f,
77                    "module has function '{}' that is not contemplated in the SDK",
78                    name
79                )
80            }
81            Self::NonFunctionImport { import_type } => {
82                write!(
83                    f,
84                    "module has a '{}' import that is not a function",
85                    import_type
86                )
87            }
88            Self::MissingImports { missing } => {
89                write!(
90                    f,
91                    "module is missing SDK imports: {}",
92                    missing.join(", ")
93                )
94            }
95        }
96    }
97}
98
99impl From<ContractError> for CompilerError {
100    fn from(error: ContractError) -> Self {
101        match error {
102            ContractError::MemoryAllocationFailed { details } => {
103                Self::MemoryAllocationFailed { details }
104            }
105            ContractError::InvalidPointer { pointer } => {
106                Self::MemoryAllocationFailed {
107                    details: format!("invalid pointer: {}", pointer),
108                }
109            }
110            ContractError::WriteOutOfBounds { offset, size } => {
111                Self::MemoryAllocationFailed {
112                    details: format!(
113                        "write out of bounds: offset {} >= size {}",
114                        offset, size
115                    ),
116                }
117            }
118            ContractError::AllocationTooLarge { size, max } => {
119                Self::MemoryAllocationFailed {
120                    details: format!(
121                        "allocation size {} exceeds maximum of {} bytes",
122                        size, max
123                    ),
124                }
125            }
126            ContractError::TotalMemoryExceeded { total, max } => {
127                Self::MemoryAllocationFailed {
128                    details: format!(
129                        "total memory {} exceeds maximum of {} bytes",
130                        total, max
131                    ),
132                }
133            }
134            ContractError::AllocationOverflow => Self::MemoryAllocationFailed {
135                details: "memory allocation would overflow".to_string(),
136            },
137            ContractError::LinkerError { function, details } => {
138                Self::InstantiationFailed {
139                    details: format!(
140                        "linker error [{}]: {}",
141                        function, details
142                    ),
143                }
144            }
145        }
146    }
147}