Skip to content

Commit 2471324

Browse files
committed
chore: Improve filesystem error messages on WASM
1 parent 906c790 commit 2471324

7 files changed

Lines changed: 34 additions & 12 deletions

File tree

bindings/javascript/__test__/wasm.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,18 @@ test("unsupported network operation", (t) => {
8989
"Loading remote stylesheets is not supported on WASM: http://127.0.0.1:5000/external.css",
9090
);
9191
});
92+
93+
test("unsupported filesystem operation", (t) => {
94+
const error = t.throws(
95+
() => {
96+
inline(
97+
"<html><head><link href='tests/external.css' rel='stylesheet' type='text/css'></head><body></body></html>",
98+
);
99+
},
100+
{ any: true },
101+
);
102+
t.is(
103+
error,
104+
"Loading local files is not supported on WASM: tests/external.css",
105+
);
106+
});

bindings/javascript/src/errors.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ impl From<InlineError> for napi::Error {
3737
#[cfg(target_arch = "wasm32")]
3838
impl From<InlineError> for JsValue {
3939
fn from(error: InlineError) -> Self {
40-
if let css_inline::InlineError::IO(error) = &error.0 {
41-
panic!("{}", error);
42-
}
4340
match &error.0 {
4441
css_inline::InlineError::ParseError(e) => JsValue::from_str(e),
42+
css_inline::InlineError::IO(io_error)
43+
if io_error.kind() == std::io::ErrorKind::Unsupported =>
44+
{
45+
JsValue::from_str(io_error.to_string().as_str())
46+
}
4547
css_inline::InlineError::Network {
4648
error: network_error,
4749
location,

bindings/javascript/wasm/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,15 @@ function __wbg_get_imports() {
274274
const ret = typeof v === "boolean" ? v ? 1 : 0 : 2;
275275
return ret;
276276
};
277+
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
278+
const ret = getStringFromWasm0(arg0, arg1);
279+
return addHeapObject(ret);
280+
};
277281
imports.wbg.__wbindgen_is_object = function(arg0) {
278282
const val = getObject(arg0);
279283
const ret = typeof val === "object" && val !== null;
280284
return ret;
281285
};
282-
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
283-
const ret = getStringFromWasm0(arg0, arg1);
284-
return addHeapObject(ret);
285-
};
286286
imports.wbg.__wbindgen_object_clone_ref = function(arg0) {
287287
const ret = getObject(arg0);
288288
return addHeapObject(ret);

bindings/javascript/wasm/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/javascript/wasm/index.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,15 @@ function __wbg_get_imports() {
246246
const ret = typeof v === "boolean" ? v ? 1 : 0 : 2;
247247
return ret;
248248
};
249+
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
250+
const ret = getStringFromWasm0(arg0, arg1);
251+
return addHeapObject(ret);
252+
};
249253
imports.wbg.__wbindgen_is_object = function(arg0) {
250254
const val = getObject(arg0);
251255
const ret = typeof val === "object" && val !== null;
252256
return ret;
253257
};
254-
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
255-
const ret = getStringFromWasm0(arg0, arg1);
256-
return addHeapObject(ret);
257-
};
258258
imports.wbg.__wbindgen_object_clone_ref = function(arg0) {
259259
const ret = getObject(arg0);
260260
return addHeapObject(ret);
492 Bytes
Binary file not shown.

css-inline/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ fn load_external(location: &str) -> Result<String> {
388388
ErrorKind::NotFound => InlineError::MissingStyleSheet {
389389
path: location.to_string(),
390390
},
391+
#[cfg(target_family = "wasm")]
392+
ErrorKind::Unsupported => InlineError::IO(std::io::Error::new(
393+
ErrorKind::Unsupported,
394+
format!("Loading local files is not supported on WASM: {location}"),
395+
)),
391396
_ => InlineError::IO(error),
392397
})
393398
}

0 commit comments

Comments
 (0)