Skip to content

Commit e06eb1c

Browse files
committed
chore: Improved error messages
Ref: #27
1 parent d5ce717 commit e06eb1c

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

src/error.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//! Errors that may happen during inlining.
2-
use cssparser::ParseError;
3-
use std::io;
2+
use cssparser::{BasicParseErrorKind, ParseError, ParseErrorKind};
3+
use std::fmt::{Display, Formatter};
4+
use std::{fmt, io};
45

56
/// Inlining error
67
#[derive(Debug)]
78
pub enum InlineError {
89
/// Input-output error. May happen during writing the resulting HTML.
910
IO(io::Error),
1011
/// Syntax errors or unsupported selectors.
11-
ParseError,
12+
ParseError(String),
1213
}
1314

1415
impl From<io::Error> for InlineError {
@@ -17,8 +18,29 @@ impl From<io::Error> for InlineError {
1718
}
1819
}
1920

21+
impl Display for InlineError {
22+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
23+
match self {
24+
InlineError::IO(error) => write!(f, "{}", error),
25+
InlineError::ParseError(error) => write!(f, "{}", error),
26+
}
27+
}
28+
}
29+
2030
impl From<(ParseError<'_, ()>, &str)> for InlineError {
21-
fn from(_: (ParseError<'_, ()>, &str)) -> Self {
22-
InlineError::ParseError
31+
fn from(error: (ParseError<'_, ()>, &str)) -> Self {
32+
let message = match error.0.kind {
33+
ParseErrorKind::Basic(kind) => match kind {
34+
BasicParseErrorKind::UnexpectedToken(token) => {
35+
format!("Unexpected token: {:?}", token)
36+
}
37+
BasicParseErrorKind::EndOfInput => "End of input".to_string(),
38+
BasicParseErrorKind::AtRuleInvalid(value) => format!("Invalid @ rule: {}", value),
39+
BasicParseErrorKind::AtRuleBodyInvalid => "Invalid @ rule body".to_string(),
40+
BasicParseErrorKind::QualifiedRuleInvalid => "Invalid qualified rule".to_string(),
41+
},
42+
ParseErrorKind::Custom(_) => "Unknown error".to_string(),
43+
};
44+
InlineError::ParseError(message)
2345
}
2446
}

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn inline(html: &str) -> Result<String, InlineError> {
127127
let document = parse_html().one(html);
128128
for style_tag in document
129129
.select("style")
130-
.map_err(|_| error::InlineError::ParseError)?
130+
.map_err(|_| error::InlineError::ParseError("Unknown error".to_string()))?
131131
{
132132
if let Some(first_child) = style_tag.as_node().first_child() {
133133
if let Some(css_cell) = first_child.as_text() {
@@ -136,8 +136,9 @@ pub fn inline(html: &str) -> Result<String, InlineError> {
136136
let mut parser = parse::CSSParser::new(&mut parse_input);
137137
for parsed in parser.parse() {
138138
if let Ok((selector, declarations)) = parsed {
139-
let rule = Rule::new(&selector, declarations)
140-
.map_err(|_| error::InlineError::ParseError)?;
139+
let rule = Rule::new(&selector, declarations).map_err(|_| {
140+
error::InlineError::ParseError("Unknown error".to_string())
141+
})?;
141142
let matching_elements = document
142143
.inclusive_descendants()
143144
.filter_map(|node| node.into_element_ref())

tests/test_inlining.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,14 @@ fn media_query_ignore() {
8989
expected = "<h1>Hello world!</h1>"
9090
)
9191
}
92+
93+
#[test]
94+
fn invalid_rule() {
95+
let html = html!(
96+
"h1 {background-color: blue;}",
97+
r#"<h1 style="@wrong { color: ---}">Hello world!</h1>"#
98+
);
99+
let result = inline(&html);
100+
assert!(result.is_err());
101+
assert_eq!(result.unwrap_err().to_string(), "Invalid @ rule: wrong")
102+
}

0 commit comments

Comments
 (0)