Skip to content

Commit

Permalink
chore: better identify errors when loading core modules or components
Browse files Browse the repository at this point in the history
  • Loading branch information
ereslibre committed Oct 3, 2023
1 parent ba715d9 commit f8e35d5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.85"
tokio = "1.28"
toml = "0.7.0"
wasmparser = "0.113.2"
wws-config = { path = "./crates/config" }
wws-runtimes = { path = "./crates/runtimes" }
wws-data-kv = { path = "./crates/data-kv" }
Expand Down
1 change: 1 addition & 0 deletions crates/worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
toml = { workspace = true }
wasmparser = { workspace = true }
wasmtime = { workspace = true }
wasmtime-wasi = { workspace = true }
wasmtime-wasi-nn = { workspace = true }
Expand Down
16 changes: 14 additions & 2 deletions crates/worker/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ pub type Result<T> = std::result::Result<T, WorkerError>;

#[derive(Debug)]
pub enum WorkerError {
BadWasmModuleOrComponent,
BadWasmCoreModule {
error: String,
},
BadWasmComponent {
error: String,
},
BadWasmCoreModuleOrComponent,
CannotLoadConfig,
CannotParseConfig {
path: PathBuf,
Expand Down Expand Up @@ -37,7 +43,13 @@ impl From<wws_runtimes::errors::RuntimeError> for WorkerError {
impl std::fmt::Display for WorkerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WorkerError::BadWasmModuleOrComponent => write!(f, "Bad Wasm module or component"),
WorkerError::BadWasmCoreModule { error } => {
write!(f, "Bad Wasm core module: {}", error)
}
WorkerError::BadWasmComponent { error } => write!(f, "Bad Wasm component: {}", error),
WorkerError::BadWasmCoreModuleOrComponent => {
write!(f, "Bad Wasm core module or component")
}
WorkerError::CannotLoadConfig => write!(f, "Could not load configuration"),
WorkerError::CannotParseConfig { path, error } => write!(
f,
Expand Down
23 changes: 18 additions & 5 deletions crates/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,25 @@ impl Worker {
})?;
let runtime = init_runtime(project_root, path, project_config)?;
let bytes = runtime.module_bytes()?;
let module_or_component = if let Ok(module) = Module::from_binary(&engine, &bytes) {
Ok(ModuleOrComponent::Module(module))
} else if let Ok(component) = Component::from_binary(&engine, &bytes) {
Ok(ModuleOrComponent::Component(component))

let module_or_component = if wasmparser::Parser::is_core_wasm(&bytes) {
Ok(ModuleOrComponent::Module(
Module::from_binary(&engine, &bytes).map_err(|err| {
errors::WorkerError::BadWasmCoreModule {
error: format!("{:?}", err),
}
})?,
))
} else if wasmparser::Parser::is_component(&bytes) {
Ok(ModuleOrComponent::Component(
Component::from_binary(&engine, &bytes).map_err(|err| {
errors::WorkerError::BadWasmComponent {
error: format!("{:?}", err),
}
})?,
))
} else {
Err(errors::WorkerError::BadWasmModuleOrComponent)
Err(errors::WorkerError::BadWasmCoreModuleOrComponent)
}?;

// Prepare the environment if required
Expand Down

0 comments on commit f8e35d5

Please sign in to comment.