Skip to content

Commit d91f324

Browse files
committed
chore: Separate InlineError::MissingStyleSheet error variant to improve debugging experience
Ref: #124
1 parent 17ff73c commit d91f324

7 files changed

Lines changed: 67 additions & 3 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+
### Added
6+
7+
- Separate `InlineError::MissingStyleSheet` error variant to improve debugging experience. [#124](https://github.com/Stranger6667/css-inline/issues/124)
8+
59
## [0.7.6] - 2022-01-08
610

711
### Fixed

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+
### Added
6+
7+
- Include missing stylesheet path to `InlineError` message. [#124](https://github.com/Stranger6667/css-inline/issues/124)
8+
59
## [0.7.8] - 2022-01-08
610

711
### Fixed

bindings/python/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ impl From<InlineErrorWrapper> for PyErr {
4242
rust_inline::InlineError::ParseError(message) => {
4343
InlineError::new_err(message.to_string())
4444
}
45+
rust_inline::InlineError::MissingStyleSheet { .. } => {
46+
InlineError::new_err(error.0.to_string())
47+
}
4548
}
4649
}
4750
}

bindings/python/tests-py/test_inlining.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ def test_inline_many_wrong_type():
6161
css_inline.inline_many([1])
6262

6363

64+
def test_missing_stylesheet():
65+
with pytest.raises(
66+
css_inline.InlineError, match="Missing stylesheet file: tests/missing.css"
67+
):
68+
css_inline.inline(
69+
"""<html>
70+
<head>
71+
<link href="tests/missing.css" rel="stylesheet" type="text/css">
72+
</head>
73+
<body>
74+
<h1>Big Text</h1>
75+
</body>
76+
</html>"""
77+
)
78+
79+
6480
def test_invalid_base_url():
6581
with pytest.raises(ValueError):
6682
css_inline.CSSInliner(base_url="foo")

css-inline/src/error.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ use std::{
1111
/// Inlining error
1212
#[derive(Debug)]
1313
pub enum InlineError {
14+
/// Missing stylesheet file.
15+
MissingStyleSheet {
16+
/// Path to the missing file.
17+
path: String,
18+
},
1419
/// Input-output error. May happen during writing the resulting HTML.
1520
IO(io::Error),
1621
/// Network-related problem. E.g. resource is not available.
@@ -38,6 +43,9 @@ impl Display for InlineError {
3843
Self::IO(error) => error.fmt(f),
3944
Self::Network(error) => error.fmt(f),
4045
Self::ParseError(error) => f.write_str(error),
46+
Self::MissingStyleSheet { path } => {
47+
f.write_fmt(format_args!("Missing stylesheet file: {}", path))
48+
}
4149
}
4250
}
4351
}

css-inline/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ mod parser;
115115
use ahash::AHashMap;
116116
pub use error::InlineError;
117117
use smallvec::{smallvec, SmallVec};
118-
use std::{borrow::Cow, collections::hash_map::Entry, fs, io::Write};
118+
use std::{
119+
borrow::Cow,
120+
collections::hash_map::Entry,
121+
fs,
122+
io::{ErrorKind, Write},
123+
};
119124
pub use url::{ParseError, Url};
120125

121126
/// Replace double quotes in property values.
@@ -404,7 +409,12 @@ fn load_external(location: &str) -> Result<String> {
404409
let response = request.send()?;
405410
Ok(response.text()?)
406411
} else {
407-
fs::read_to_string(location).map_err(InlineError::IO)
412+
fs::read_to_string(location).map_err(|error| match error.kind() {
413+
ErrorKind::NotFound => InlineError::MissingStyleSheet {
414+
path: location.to_string(),
415+
},
416+
_ => InlineError::IO(error),
417+
})
408418
}
409419
}
410420

css-inline/tests/test_inlining.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[macro_use]
22
mod utils;
3-
use css_inline::{inline, CSSInliner, InlineOptions, Url};
3+
use css_inline::{inline, CSSInliner, InlineError, InlineOptions, Url};
44

55
#[test]
66
fn no_existing_style() {
@@ -309,6 +309,25 @@ h2 { color: red; }
309309
))
310310
}
311311

312+
#[test]
313+
fn missing_stylesheet() {
314+
let html = r#"
315+
<html>
316+
<head>
317+
<link href="tests/missing.css" rel="stylesheet" type="text/css">
318+
</head>
319+
<body>
320+
<h1>Big Text</h1>
321+
</body>
322+
</html>"#;
323+
match inline(html).expect_err("Should be an error") {
324+
InlineError::MissingStyleSheet { path } => {
325+
assert_eq!(path, String::from("tests/missing.css"))
326+
}
327+
_ => panic!("Invalid error type"),
328+
}
329+
}
330+
312331
#[test]
313332
fn remote_file_stylesheet_disable() {
314333
let html = r#"

0 commit comments

Comments
 (0)