Skip to content

Commit 1946de3

Browse files
committed
Improve error message for bad front matter
1 parent 009d4c7 commit 1946de3

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Diff for: front_matter/src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use eyre::bail;
1+
use eyre::{ContextCompat, bail};
22
use serde::{Deserialize, Serialize};
33
use toml::value::Date;
44

@@ -21,12 +21,12 @@ pub struct FrontMatter {
2121
/// the tuple.
2222
pub fn parse(markdown: &str) -> eyre::Result<(FrontMatter, &str)> {
2323
if !markdown.starts_with("+++\n") {
24-
bail!("markdown file must start with the line `+++`");
24+
bail!("missing start of TOML front matter (+++)");
2525
}
2626
let (front_matter, content) = markdown
2727
.trim_start_matches("+++\n")
2828
.split_once("\n+++\n")
29-
.expect("couldn't find the end of the front matter: `+++`");
29+
.context("missing end of TOML front matter (+++)")?;
3030

3131
Ok((toml::from_str(front_matter)?, content))
3232
}
@@ -63,7 +63,9 @@ mod tests {
6363

6464
for post in posts {
6565
let content = fs::read_to_string(&post).unwrap();
66-
let normalized = normalize(&content).unwrap();
66+
let normalized = normalize(&content).unwrap_or_else(|err| {
67+
panic!("failed to normalize {:?}: {err}", post.file_name().unwrap());
68+
});
6769

6870
if content != normalized {
6971
if env::var("FIX_FRONT_MATTER").is_ok() {

0 commit comments

Comments
 (0)