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