Skip to main content

wasmrs_wasmtime/
errors.rs

1use wasi_common::StringArrayError;
2
3/// This crate's Error type
4#[derive(thiserror::Error, Debug)]
5pub enum Error {
6  /// WASMTime initialization failed
7  #[error("Initialization failed: {0}")]
8  Initialization(anyhow::Error),
9
10  /// WASMTime Linker initialization failed
11  #[error("Linker initialization failed: {0}")]
12  Linker(anyhow::Error),
13
14  /// Setting up linked functions failed.
15  #[error("Could not create WebAssembly function: {0}")]
16  Func(anyhow::Error),
17
18  /// WASMTime module instantiation failed
19  #[error("Could not instantiate new WASM Module: {0}")]
20  Module(anyhow::Error),
21
22  /// WASMTime module instantiation failed
23  #[error("Could not find module {0} in module cache")]
24  NotFound(String),
25
26  /// Error originating from [wasi_common]
27  #[error(transparent)]
28  WasiError(#[from] wasi_common::Error),
29
30  /// Error originating from [wasi_common]
31  #[error(transparent)]
32  WasiStringArray(#[from] StringArrayError),
33
34  /// Error originating from [std::io::Error]
35  #[error(transparent)]
36  IO(#[from] std::io::Error),
37
38  /// Thrown if the guest's send function is not exported.
39  #[error("Guest init function not exported by wasm module.")]
40  GuestInit,
41
42  /// Thrown if the guest's send function is not exported.
43  #[error("Guest send function not exported by wasm module.")]
44  GuestSend,
45
46  /// Thrown if the host has a problem reading the guest's memory.
47  #[error("Could not read guest memory")]
48  GuestMemory,
49
50  /// Thrown if the builder wasn't provide a module to instantiate with.
51  #[error("Must provide a module to the builder")]
52  NoModule,
53
54  /// Thrown if the builder was provided too many module options.
55  #[error("Must provide either module bytes with ID to cache or a cached ID, not both")]
56  AmbiguousModule,
57}
58
59impl From<Error> for wasmrs::Error {
60  fn from(e: Error) -> Self {
61    let code = match e {
62      Error::GuestMemory => wasmrs::ErrorCode::Canceled,
63      Error::Initialization(_) => wasmrs::ErrorCode::ConnectionError,
64      Error::Func(_) => wasmrs::ErrorCode::ConnectionError,
65      Error::Linker(_) => wasmrs::ErrorCode::ConnectionError,
66      Error::Module(_) => wasmrs::ErrorCode::ConnectionError,
67      Error::WasiError(_) => wasmrs::ErrorCode::ConnectionError,
68      _ => wasmrs::ErrorCode::ApplicationError,
69    };
70    wasmrs::Error::RSocket(code.into())
71  }
72}