Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: generic error message for file load #377

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions wrappers/src/fdw/wasm_fdw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use self::bindings::supabase::wrappers::types::FdwError as GuestFdwError;

#[derive(Error, Debug)]
enum WasmFdwError {
#[error("invalid WebAssembly component")]
InvalidWasmComponent,

#[error("guest fdw error: {0}")]
GuestFdw(GuestFdwError),

Expand Down
16 changes: 12 additions & 4 deletions wrappers/src/fdw/wasm_fdw/wasm_fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ fn check_version_requirement(ver_req: &str) -> WasmFdwResult<()> {
Ok(())
}

// compiles a new WebAssembly component from a wasm file
fn load_component_from_file(
engine: &Engine,
file_path: impl AsRef<std::path::Path>,
) -> WasmFdwResult<Component> {
Component::from_file(engine, file_path).map_err(|_| WasmFdwError::InvalidWasmComponent)
}

// Download wasm component package from warg registry or custom url.
// The url protoal can be 'file://', 'warg(s)://' or 'http(s)://'.
fn download_component(
Expand All @@ -44,7 +52,7 @@ fn download_component(
checksum: Option<&str>,
) -> WasmFdwResult<Component> {
if let Some(file_path) = url.strip_prefix("file://") {
return Ok(Component::from_file(engine, file_path)?);
return load_component_from_file(engine, file_path);
}

if url.starts_with("warg://") || url.starts_with("wargs://") {
Expand All @@ -69,7 +77,7 @@ fn download_component(
.block_on(client.download(&pkg_name, &ver))?
.ok_or(format!("{}@{} not found on {}", name, version, url))?;

return Ok(Component::from_file(engine, pkg.path)?);
return load_component_from_file(engine, pkg.path);
}

// otherwise, download from custom url if it is not in local cache
Expand Down Expand Up @@ -107,10 +115,10 @@ fn download_component(
fs::write(&path, bytes)?;
}

Ok(Component::from_file(engine, &path).inspect_err(|_| {
load_component_from_file(engine, &path).inspect_err(|_| {
// remove the cache file if it cannot be loaded as component
let _ = fs::remove_file(&path);
})?)
})
}

#[wrappers_fdw(
Expand Down
Loading