|
1 | 1 | use serde::{Deserialize, Serialize};
|
2 | 2 |
|
| 3 | +/// Create a link to this section of the Execution Errors documentation. |
| 4 | +pub fn doc_ref(section: &str) -> String { |
| 5 | + format!( |
| 6 | + "http://internetcomputer.org/docs/current/references/execution-errors#{}", |
| 7 | + section |
| 8 | + ) |
| 9 | +} |
| 10 | + |
3 | 11 | pub enum ErrorHelp {
|
4 | 12 | UserError {
|
5 | 13 | suggestion: String,
|
@@ -55,8 +63,12 @@ pub enum WasmValidationError {
|
55 | 63 | InvalidImportSection(String),
|
56 | 64 | /// Module contains an invalid export section
|
57 | 65 | InvalidExportSection(String),
|
58 |
| - /// Module contains an invalid export section caused by a user error. |
59 |
| - UserInvalidExportSection(String), |
| 66 | + /// Same function name is exported multiple times (with different types). |
| 67 | + DuplicateExport { name: String }, |
| 68 | + /// There are too many exports defined in the module. |
| 69 | + TooManyExports { defined: usize, allowed: usize }, |
| 70 | + /// The total length of exported function names is too large. |
| 71 | + ExportedNamesTooLong { total_length: usize, allowed: usize }, |
60 | 72 | /// Module contains an invalid data section
|
61 | 73 | InvalidDataSection(String),
|
62 | 74 | /// Module contains an invalid custom section
|
@@ -107,8 +119,32 @@ impl std::fmt::Display for WasmValidationError {
|
107 | 119 | Self::InvalidExportSection(err) => {
|
108 | 120 | write!(f, "Wasm module has an invalid export section. {err}")
|
109 | 121 | }
|
110 |
| - Self::UserInvalidExportSection(err) => { |
111 |
| - write!(f, "Wasm module has an invalid export section. {}", err) |
| 122 | + Self::DuplicateExport { name } => { |
| 123 | + write!( |
| 124 | + f, |
| 125 | + "Duplicate function '{name}' exported multiple times \ |
| 126 | + with different call types: update, query, or composite_query." |
| 127 | + ) |
| 128 | + } |
| 129 | + Self::TooManyExports { defined, allowed } => { |
| 130 | + write!( |
| 131 | + f, |
| 132 | + "The number of exported functions called \ |
| 133 | + `canister_update <name>`, `canister_query <name>`, or \ |
| 134 | + `canister_composite_query <name>` is {defined} which exceeds {allowed}." |
| 135 | + ) |
| 136 | + } |
| 137 | + Self::ExportedNamesTooLong { |
| 138 | + total_length, |
| 139 | + allowed, |
| 140 | + } => { |
| 141 | + write!( |
| 142 | + f, |
| 143 | + "The sum of `<name>` lengths in exported \ |
| 144 | + functions called `canister_update <name>`, `canister_query <name>`, \ |
| 145 | + or `canister_composite_query <name>` is {total_length} which exceeds \ |
| 146 | + the allowed limit of {allowed}." |
| 147 | + ) |
112 | 148 | }
|
113 | 149 | Self::InvalidDataSection(err) => {
|
114 | 150 | write!(f, "Wasm module has an invalid data section. {err}")
|
@@ -185,15 +221,54 @@ impl AsErrorHelp for WasmValidationError {
|
185 | 221 | | WasmValidationError::InvalidGlobalSection(_)
|
186 | 222 | | WasmValidationError::UnsupportedWasmInstruction { .. }
|
187 | 223 | | WasmValidationError::TooManyCustomSections { .. } => ErrorHelp::ToolchainError,
|
188 |
| - WasmValidationError::UserInvalidExportSection(_) |
189 |
| - | WasmValidationError::TooManyFunctions { .. } |
190 |
| - | WasmValidationError::TooManyGlobals { .. } |
191 |
| - | WasmValidationError::FunctionComplexityTooHigh { .. } |
192 |
| - | WasmValidationError::FunctionTooLarge { .. } |
193 |
| - | WasmValidationError::CodeSectionTooLarge { .. } |
194 |
| - | WasmValidationError::ModuleTooLarge { .. } => ErrorHelp::UserError { |
195 |
| - suggestion: "".to_string(), |
196 |
| - doc_link: "".to_string(), |
| 224 | + WasmValidationError::DuplicateExport { name } => ErrorHelp::UserError { |
| 225 | + suggestion: format!( |
| 226 | + "Try defining different versions of the function for each \ |
| 227 | + call type, e.g. `{name}_update`, `{name}_query`, etc." |
| 228 | + ), |
| 229 | + doc_link: doc_ref("wasm-module-duplicate-exports"), |
| 230 | + }, |
| 231 | + WasmValidationError::TooManyExports { .. } => ErrorHelp::UserError { |
| 232 | + suggestion: "Try combining multiple endpoints into a single endpoint.".to_string(), |
| 233 | + doc_link: doc_ref("wasm-module-exports-too-many-methods"), |
| 234 | + }, |
| 235 | + WasmValidationError::ExportedNamesTooLong { .. } => ErrorHelp::UserError { |
| 236 | + suggestion: "Try using shorter method names.".to_string(), |
| 237 | + doc_link: doc_ref("wasm-module-sum-of-exported-name-lengths-too-large"), |
| 238 | + }, |
| 239 | + WasmValidationError::TooManyFunctions { .. } => ErrorHelp::UserError { |
| 240 | + suggestion: "Try spliting this canister into multiple canisters.".to_string(), |
| 241 | + doc_link: doc_ref("wasm-module-too-many-functions"), |
| 242 | + }, |
| 243 | + WasmValidationError::TooManyGlobals { .. } => ErrorHelp::UserError { |
| 244 | + suggestion: "Try collecting multiple globals into a single \ |
| 245 | + structured which can be stored on the heap." |
| 246 | + .to_string(), |
| 247 | + doc_link: doc_ref("wasm-module-too-many-globals"), |
| 248 | + }, |
| 249 | + WasmValidationError::FunctionComplexityTooHigh { .. } => ErrorHelp::UserError { |
| 250 | + suggestion: "Try breaking large functions up into multiple \ |
| 251 | + smaller functions." |
| 252 | + .to_string(), |
| 253 | + doc_link: doc_ref("wasm-module-function-complexity-too-high"), |
| 254 | + }, |
| 255 | + WasmValidationError::FunctionTooLarge { .. } => ErrorHelp::UserError { |
| 256 | + suggestion: "Try breaking large functions up into multiple \ |
| 257 | + smaller functions." |
| 258 | + .to_string(), |
| 259 | + doc_link: doc_ref("wasm-module-function-too-large"), |
| 260 | + }, |
| 261 | + WasmValidationError::CodeSectionTooLarge { .. } => ErrorHelp::UserError { |
| 262 | + suggestion: "Try shrinking the module code section using tools like \ |
| 263 | + `ic-wasm` or splitting the logic across multiple canisters." |
| 264 | + .to_string(), |
| 265 | + doc_link: doc_ref("wasm-module-code-section-too-large"), |
| 266 | + }, |
| 267 | + WasmValidationError::ModuleTooLarge { .. } => ErrorHelp::UserError { |
| 268 | + suggestion: "Try shrinking the module using tools like \ |
| 269 | + `ic-wasm` or splitting the logic across multiple canisters." |
| 270 | + .to_string(), |
| 271 | + doc_link: doc_ref("wasm-module-too-large"), |
197 | 272 | },
|
198 | 273 | }
|
199 | 274 | }
|
|
0 commit comments