unc_vm_engine/error.rs
1//! The WebAssembly possible errors
2use crate::trap::RuntimeError;
3use std::io;
4use thiserror::Error;
5use unc_vm_compiler::CompileError;
6use unc_vm_types::ExternType;
7
8/// The Deserialize error can occur when loading a
9/// compiled Module from a binary.
10#[derive(Error, Debug)]
11pub enum DeserializeError {
12 /// An IO error
13 #[error(transparent)]
14 Io(#[from] io::Error),
15 /// A generic deserialization error
16 #[error("{0}")]
17 Generic(String),
18 /// Incompatible serialized binary
19 #[error("incompatible binary: {0}")]
20 Incompatible(String),
21 /// The provided binary is corrupted
22 #[error("corrupted binary: {0}")]
23 CorruptedBinary(String),
24 /// The binary was valid, but we got an error when
25 /// trying to allocate the required resources.
26 #[error(transparent)]
27 Compiler(CompileError),
28}
29
30/// An ImportError.
31///
32/// Note: this error is not standard to WebAssembly, but it's
33/// useful to determine the import issue on the API side.
34#[derive(Error, Debug)]
35pub enum ImportError {
36 /// Incompatible Import Type.
37 /// This error occurs when the import types mismatch.
38 #[error("incompatible import type. Expected {0:?} but received {1:?}")]
39 IncompatibleType(ExternType, ExternType),
40
41 /// Unknown Import.
42 /// This error occurs when an import was expected but not provided.
43 #[error("unknown import. Expected {0:?}")]
44 UnknownImport(ExternType),
45}
46
47/// The WebAssembly.LinkError object indicates an error during
48/// module instantiation (besides traps from the start function).
49///
50/// This is based on the [link error][link-error] API.
51///
52/// [link-error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/LinkError
53#[derive(Error, Debug)]
54#[error("Link error: {0}")]
55pub enum LinkError {
56 /// An error occurred when checking the import types.
57 #[error("Error while importing {0:?}.{1:?}: {2}")]
58 Import(String, String, Box<ImportError>),
59
60 /// A trap ocurred during linking.
61 #[error("RuntimeError occurred during linking: {0}")]
62 Trap(#[source] RuntimeError),
63
64 /// Insufficient resources available for linking.
65 #[error("Insufficient resources: {0}")]
66 Resource(String),
67}
68
69/// An error while instantiating a module.
70///
71/// This is not a common WebAssembly error, however
72/// we need to differentiate from a `LinkError` (an error
73/// that happens while linking, on instantiation) and a
74/// Trap that occurs when calling the WebAssembly module
75/// start function.
76#[derive(Error, Debug)]
77pub enum InstantiationError {
78 /// A linking ocurred during instantiation.
79 #[error(transparent)]
80 Link(LinkError),
81
82 /// The module was compiled with a CPU feature that is not available on
83 /// the current host.
84 #[error("module compiled with CPU feature that is missing from host")]
85 CpuFeature(String),
86
87 /// A runtime error occured while invoking the start function
88 #[error(transparent)]
89 Start(RuntimeError),
90}