Skip to content

Commit 08f3abf

Browse files
authored
Merge pull request #1520 from rust-lang/senekor/uonoqosnovxm
Improve error message for bad front matter
2 parents 009d4c7 + 17a6819 commit 08f3abf

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
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() {

Diff for: src/posts.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::blogs::Manifest;
2+
use eyre::Context;
23
use front_matter::FrontMatter;
34
use regex::Regex;
45
use serde::Serialize;
@@ -56,7 +57,8 @@ impl Post {
5657
..
5758
},
5859
contents,
59-
) = front_matter::parse(&contents)?;
60+
) = front_matter::parse(&contents)
61+
.with_context(|| format!("failed to parse {filename}"))?;
6062

6163
let options = comrak::Options {
6264
render: comrak::RenderOptions::builder().unsafe_(true).build(),

0 commit comments

Comments
 (0)