1use crate::{ExternType, Pages};
3use std::io;
4use thiserror::Error;
5
6#[derive(Error, Debug)]
9pub enum SerializeError {
10 #[error(transparent)]
12 Io(#[from] io::Error),
13 #[error("{0}")]
15 Generic(String),
16}
17
18#[derive(Error, Debug)]
21pub enum DeserializeError {
22 #[error(transparent)]
24 Io(#[from] io::Error),
25 #[error("{0}")]
27 Generic(String),
28 #[error("incompatible binary: {0}")]
30 Incompatible(String),
31 #[error("corrupted binary: {0}")]
33 CorruptedBinary(String),
34 #[error(transparent)]
37 Compiler(#[from] CompileError),
38 #[error("invalid input bytes: expected {expected} bytes, got {got}")]
40 InvalidByteLength {
41 expected: usize,
43 got: usize,
45 },
46}
47
48#[derive(Error, Debug, Clone, PartialEq, Eq, Hash)]
50#[non_exhaustive]
51pub enum MemoryError {
52 #[error("Error when allocating memory: {0}")]
54 Region(String),
55 #[error("The memory could not grow: current size {} pages, requested increase: {} pages", current.0, attempted_delta.0)]
58 CouldNotGrow {
59 current: Pages,
61 attempted_delta: Pages,
63 },
64 #[error("The memory is invalid because {}", reason)]
66 InvalidMemory {
67 reason: String,
69 },
70 #[error("The minimum requested ({} pages) memory is greater than the maximum allowed memory ({} pages)", min_requested.0, max_allowed.0)]
72 MinimumMemoryTooLarge {
73 min_requested: Pages,
75 max_allowed: Pages,
77 },
78 #[error("The maximum requested memory ({} pages) is greater than the maximum allowed memory ({} pages)", max_requested.0, max_allowed.0)]
80 MaximumMemoryTooLarge {
81 max_requested: Pages,
83 max_allowed: Pages,
85 },
86 #[error("The memory is not shared")]
88 MemoryNotShared,
89 #[error("tried to call an unsupported memory operation: {message}")]
92 UnsupportedOperation {
93 message: String,
95 },
96 #[error("The memory does not support atomic operations")]
98 AtomicsNotSupported,
99 #[error("A user-defined error occurred: {0}")]
101 Generic(String),
102}
103
104#[derive(Error, Debug, Clone)]
109pub enum ImportError {
110 #[error("incompatible import type. Expected {0:?} but received {1:?}")]
113 IncompatibleType(ExternType, ExternType),
114
115 #[error("unknown import. Expected {0:?}")]
118 UnknownImport(ExternType),
119
120 #[error("memory error. {0}")]
122 MemoryError(String),
123}
124
125#[derive(Error, Debug)]
128pub enum PreInstantiationError {
129 #[error("module compiled with CPU feature that is missing from host")]
132 CpuFeature(String),
133}
134
135use crate::lib::std::string::String;
136
137#[derive(Debug)]
149#[cfg_attr(feature = "std", derive(Error))]
150pub enum CompileError {
151 #[cfg_attr(feature = "std", error("WebAssembly translation error: {0}"))]
153 Wasm(WasmError),
154
155 #[cfg_attr(feature = "std", error("Compilation error: {0}"))]
157 Codegen(String),
158
159 #[cfg_attr(feature = "std", error("Validation error: {0}"))]
161 Validate(String),
162
163 #[cfg_attr(feature = "std", error("Feature {0} is not yet supported"))]
165 UnsupportedFeature(String),
166
167 #[cfg_attr(
170 feature = "std",
171 error("The target {0} is not yet supported (see https://docs.wasmer.io/runtime/features)")
172 )]
173 UnsupportedTarget(String),
174
175 #[cfg_attr(feature = "std", error("Insufficient resources: {0}"))]
177 Resource(String),
178
179 #[cfg_attr(feature = "std", error("Middleware error: {0}"))]
181 MiddlewareError(String),
182}
183
184impl From<WasmError> for CompileError {
185 fn from(original: WasmError) -> Self {
186 Self::Wasm(original)
187 }
188}
189
190#[derive(Debug)]
192#[cfg_attr(feature = "std", derive(Error))]
193#[cfg_attr(feature = "std", error("Error in middleware {name}: {message}"))]
194pub struct MiddlewareError {
195 pub name: String,
197 pub message: String,
199}
200
201impl MiddlewareError {
202 pub fn new<A: Into<String>, B: Into<String>>(name: A, message: B) -> Self {
204 Self {
205 name: name.into(),
206 message: message.into(),
207 }
208 }
209}
210
211#[derive(Debug)]
216#[cfg_attr(feature = "std", derive(Error))]
217pub enum WasmError {
218 #[cfg_attr(
223 feature = "std",
224 error("Invalid input WebAssembly code at offset {offset}: {message}")
225 )]
226 InvalidWebAssembly {
227 message: String,
229 offset: usize,
231 },
232
233 #[cfg_attr(feature = "std", error("Unsupported feature: {0}"))]
237 Unsupported(String),
238
239 #[cfg_attr(feature = "std", error("Implementation limit exceeded"))]
241 ImplLimitExceeded,
242
243 #[cfg_attr(feature = "std", error("{0}"))]
245 Middleware(MiddlewareError),
246
247 #[cfg_attr(feature = "std", error("{0}"))]
249 Generic(String),
250}
251
252impl From<MiddlewareError> for WasmError {
253 fn from(original: MiddlewareError) -> Self {
254 Self::Middleware(original)
255 }
256}
257
258#[derive(Debug)]
261#[cfg_attr(feature = "std", derive(Error))]
262pub enum ParseCpuFeatureError {
263 #[cfg_attr(feature = "std", error("CpuFeature {0} not recognized"))]
265 Missing(String),
266}
267
268pub type WasmResult<T> = Result<T, WasmError>;
270
271#[cfg(test)]
272mod tests {
273 use super::*;
274
275 #[test]
276 fn middleware_error_can_be_created() {
277 let msg = String::from("Something went wrong");
278 let error = MiddlewareError::new("manipulator3000", msg);
279 assert_eq!(error.name, "manipulator3000");
280 assert_eq!(error.message, "Something went wrong");
281 }
282
283 #[test]
284 fn middleware_error_be_converted_to_wasm_error() {
285 let error = WasmError::from(MiddlewareError::new("manipulator3000", "foo"));
286 match error {
287 WasmError::Middleware(MiddlewareError { name, message }) => {
288 assert_eq!(name, "manipulator3000");
289 assert_eq!(message, "foo");
290 }
291 err => panic!("Unexpected error: {err:?}"),
292 }
293 }
294}