Skip to content

Commit 4558e01

Browse files
authored
Report adapter type errors with a dedicated type (#2321)
Instead of using `bail!` with a string, report adapter type errors using a dedicated `AdapterModuleDidNotExport` type, so that users can downcast errors to it, rather than checking the error message string.
1 parent 14db2f3 commit 4558e01

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

crates/wit-component/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub use encoding::{ComponentEncoder, LibraryInfo, encode};
2121
pub use linking::Linker;
2222
pub use printing::*;
2323
pub use targets::*;
24+
pub use validation::AdapterModuleDidNotExport;
2425
pub use wit_parser::decoding::{DecodedWasm, decode, decode_reader};
2526

2627
pub mod metadata;

crates/wit-component/src/validation.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::encoding::{Instance, Item, LibraryInfo, MainOrAdapter, ModuleImportMa
22
use crate::{ComponentEncoder, StringEncoding};
33
use anyhow::{Context, Result, anyhow, bail};
44
use indexmap::{IndexMap, IndexSet, map::Entry};
5+
use std::fmt;
56
use std::hash::{Hash, Hasher};
67
use std::mem;
78
use wasm_encoder::ExportKind;
@@ -2523,14 +2524,31 @@ pub fn validate_adapter_module(
25232524
for (name, required_ty) in required_by_import {
25242525
let actual = match ret.exports.raw_exports.get(name) {
25252526
Some(ty) => ty,
2526-
None => bail!("adapter module did not export `{name}`"),
2527+
None => return Err(AdapterModuleDidNotExport(name.clone()).into()),
25272528
};
25282529
validate_func_sig(name, required_ty, &actual)?;
25292530
}
25302531

25312532
Ok(ret)
25322533
}
25332534

2535+
/// An error that can be returned from adapting a core Wasm module into a
2536+
/// component using an adapter module.
2537+
///
2538+
/// If the core Wasm module contained an import that it requires to be
2539+
/// satisfied by the adapter, and the adapter does not contain an export
2540+
/// with the same name, an instance of this error is returned.
2541+
#[derive(Debug, Clone)]
2542+
pub struct AdapterModuleDidNotExport(String);
2543+
2544+
impl fmt::Display for AdapterModuleDidNotExport {
2545+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2546+
write!(f, "adapter module did not export `{}`", self.0)
2547+
}
2548+
}
2549+
2550+
impl std::error::Error for AdapterModuleDidNotExport {}
2551+
25342552
fn resource_test_for_interface<'a>(
25352553
resolve: &'a Resolve,
25362554
id: InterfaceId,

0 commit comments

Comments
 (0)