Skip to content

Commit 89059b8

Browse files
committed
Implement error handling for wasm
1 parent f4cd618 commit 89059b8

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use wasm_bindgen::prelude::*;
3131
pub fn transform(config_val: JsValue) -> Result<JsValue, JsValue> {
3232
let config: Config = from_value(config_val).map_err(JsValue::from)?;
3333
let code = unsafe { std::str::from_utf8_unchecked(&config.code) };
34-
let res = compile(code, &config).unwrap();
34+
let res = compile(code, &config)?;
3535
let serializer = Serializer::new().serialize_maps_as_objects(true);
3636
res.serialize(&serializer).map_err(JsValue::from)
3737
}
@@ -195,6 +195,7 @@ impl<'i> From<parcel_sourcemap::SourceMapError> for CompileError<'i> {
195195
}
196196
}
197197

198+
#[cfg(not(target_arch = "wasm32"))]
198199
impl<'i> From<CompileError<'i>> for napi::Error {
199200
fn from(e: CompileError) -> napi::Error {
200201
match e {
@@ -204,6 +205,16 @@ impl<'i> From<CompileError<'i>> for napi::Error {
204205
}
205206
}
206207

208+
#[cfg(target_arch = "wasm32")]
209+
impl<'i> From<CompileError<'i>> for wasm_bindgen::JsValue {
210+
fn from(e: CompileError) -> wasm_bindgen::JsValue {
211+
match e {
212+
CompileError::SourceMapError(e) => e.into(),
213+
_ => js_sys::Error::new(&e.reason()).into()
214+
}
215+
}
216+
}
217+
207218
#[cfg(test)]
208219
mod tests {
209220
use super::*;

0 commit comments

Comments
 (0)