Skip to content

Commit 33240f7

Browse files
committed
chore: Display stylesheet location in network-related errors
1 parent c08920f commit 33240f7

17 files changed

Lines changed: 184 additions & 43 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Changed
6+
7+
- Display stylesheet location in network-related errors.
8+
59
### Performance
610

711
- Optimize serialization of attributes and text nodes.

bindings/c/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Changed
6+
7+
- Display stylesheet location in network-related errors.
8+
59
## [0.11.3] - 2023-12-14
610

711
- Initial public release

bindings/c/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub unsafe extern "C" fn css_inline_to(
9090
if let Err(e) = options.inline_to(html, &mut buffer) {
9191
return match e {
9292
InlineError::IO(_) => CssResult::IoError,
93-
InlineError::Network(_) => CssResult::RemoteStylesheetNotAvailable,
93+
InlineError::Network { .. } => CssResult::RemoteStylesheetNotAvailable,
9494
InlineError::ParseError(_) => CssResult::InternalSelectorParseError,
9595
InlineError::MissingStyleSheet { .. } => CssResult::MissingStylesheet,
9696
};

bindings/javascript/__test__/index.spec.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,32 @@ test("valid baseURL", (t) => {
3232
});
3333

3434
test("invalid baseURL", (t) => {
35-
const error = t.throws(
36-
() => {
37-
inline(
38-
"<html><head><style>h1 { color:red; }</style></head><body><h1>Test</h1></body></html>",
39-
{ baseUrl: "invalid" },
40-
);
41-
},
42-
undefined,
43-
"relative URL without a base",
44-
);
35+
const error = t.throws(() => {
36+
inline(
37+
"<html><head><style>h1 { color:red; }</style></head><body><h1>Test</h1></body></html>",
38+
{ baseUrl: "foo" },
39+
);
40+
});
4541
t.is(error.code, "InvalidArg");
42+
t.is(error.message, "relative URL without a base: foo");
43+
});
44+
45+
test("invalid href", (t) => {
46+
const error = t.throws(() => {
47+
inline(
48+
"<html><head><link href='http:' rel='stylesheet' type='text/css'></head><body></body></html>",
49+
);
50+
});
51+
t.is(error.code, "GenericFailure");
52+
t.is(error.message, "Invalid base URL: http:");
53+
});
54+
55+
test("invalid style", (t) => {
56+
const error = t.throws(() => {
57+
inline(
58+
"<html><head><style>h1, h2 { color:red; }</style></head><body><h1 style='@wrong { color: --- }'>Hello world!</h1></body></html>",
59+
);
60+
});
61+
t.is(error.code, "GenericFailure");
62+
t.is(error.message, "Invalid @ rule: wrong");
4663
});

bindings/javascript/__test__/wasm.spec.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,34 @@ test("invalid baseURL", (t) => {
4343
() => {
4444
inline(
4545
"<html><head><style>h1 { color:red; }</style></head><body><h1>Test</h1></body></html>",
46-
{ baseUrl: "invalid" },
46+
{ baseUrl: "foo" },
4747
);
4848
},
4949
{ any: true },
5050
);
51-
t.is(error, "relative URL without a base");
51+
t.is(error, "relative URL without a base: foo");
52+
});
53+
54+
test("invalid href", (t) => {
55+
const error = t.throws(
56+
() => {
57+
inline(
58+
"<html><head><link href='http:' rel='stylesheet' type='text/css'></head><body></body></html>",
59+
);
60+
},
61+
{ any: true },
62+
);
63+
t.is(error, "Invalid base URL: http:");
64+
});
65+
66+
test("invalid style", (t) => {
67+
const error = t.throws(
68+
() => {
69+
inline(
70+
"<html><head><style>h1, h2 { color:red; }</style></head><body><h1 style='@wrong { color: --- }'>Hello world!</h1></body></html>",
71+
);
72+
},
73+
{ any: true },
74+
);
75+
t.is(error, "Invalid @ rule: wrong");
5276
});

bindings/javascript/src/errors.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@ use napi::bindgen_prelude::Status;
33
#[cfg(target_arch = "wasm32")]
44
use wasm_bindgen::prelude::JsValue;
55

6-
pub(crate) struct UrlError(pub(crate) css_inline::ParseError);
6+
pub(crate) struct UrlError {
7+
pub(crate) error: css_inline::ParseError,
8+
pub(crate) url: String,
9+
}
710

811
#[cfg(not(target_arch = "wasm32"))]
912
impl From<UrlError> for napi::Error {
10-
fn from(err: UrlError) -> Self {
11-
napi::Error::new(Status::InvalidArg, err.0.to_string())
13+
fn from(error: UrlError) -> Self {
14+
napi::Error::new(
15+
Status::InvalidArg,
16+
format!("{}: {}", error.error, error.url),
17+
)
1218
}
1319
}
1420

1521
#[cfg(target_arch = "wasm32")]
1622
impl From<UrlError> for JsValue {
1723
fn from(error: UrlError) -> Self {
18-
JsValue::from_str(error.0.to_string().as_str())
24+
JsValue::from_str(format!("{}: {}", error.error, error.url).as_str())
1925
}
2026
}
2127

bindings/javascript/src/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use wasm_bindgen::prelude::JsValue;
77

88
fn parse_url(url: Option<String>) -> std::result::Result<Option<css_inline::Url>, JsError> {
99
Ok(if let Some(url) = url {
10-
Some(css_inline::Url::parse(url.as_str()).map_err(UrlError)?)
10+
Some(css_inline::Url::parse(url.as_str()).map_err(|error| UrlError { error, url })?)
1111
} else {
1212
None
1313
})
545 Bytes
Binary file not shown.

bindings/python/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Changed
6+
7+
- Display stylesheet location in network-related errors.
8+
59
### Performance
610

711
- Optimize serialization of attributes and text nodes.

bindings/python/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl From<InlineErrorWrapper> for PyErr {
4040
fn from(error: InlineErrorWrapper) -> Self {
4141
match error.0 {
4242
rust_inline::InlineError::IO(error) => InlineError::new_err(error.to_string()),
43-
rust_inline::InlineError::Network(error) => InlineError::new_err(error.to_string()),
43+
rust_inline::InlineError::Network { .. } => InlineError::new_err(error.0.to_string()),
4444
rust_inline::InlineError::ParseError(message) => {
4545
InlineError::new_err(message.to_string())
4646
}
@@ -51,17 +51,20 @@ impl From<InlineErrorWrapper> for PyErr {
5151
}
5252
}
5353

54-
struct UrlError(url::ParseError);
54+
struct UrlError {
55+
error: rust_inline::ParseError,
56+
url: String,
57+
}
5558

5659
impl From<UrlError> for PyErr {
5760
fn from(error: UrlError) -> Self {
58-
exceptions::PyValueError::new_err(error.0.to_string())
61+
exceptions::PyValueError::new_err(format!("{}: {}", error.error, error.url))
5962
}
6063
}
6164

62-
fn parse_url(url: Option<String>) -> PyResult<Option<url::Url>> {
65+
fn parse_url(url: Option<String>) -> PyResult<Option<rust_inline::Url>> {
6366
Ok(if let Some(url) = url {
64-
Some(url::Url::parse(url.as_str()).map_err(UrlError)?)
67+
Some(rust_inline::Url::parse(url.as_str()).map_err(|error| UrlError { error, url })?)
6568
} else {
6669
None
6770
})

0 commit comments

Comments
 (0)