Skip to content

Commit 073f85d

Browse files
committed
Switch from error-chain to anyhow.
1 parent 2f1d3ff commit 073f85d

File tree

21 files changed

+100
-165
lines changed

21 files changed

+100
-165
lines changed

Cargo.lock

Lines changed: 7 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ repository = "https://github.com/rust-lang/mdBook"
1616
description = "Creates a book from markdown files"
1717

1818
[dependencies]
19+
anyhow = "1.0.28"
1920
chrono = "0.4"
2021
clap = "2.24"
2122
env_logger = "0.7.1"
22-
error-chain = "0.12"
2323
handlebars = "3.0"
2424
lazy_static = "1.0"
2525
log = "0.4"

examples/nop-preprocessor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ mod nop_lib {
8787
// particular config value
8888
if let Some(nop_cfg) = ctx.config.get_preprocessor(self.name()) {
8989
if nop_cfg.contains_key("blow-up") {
90-
return Err("Boom!!1!".into());
90+
anyhow::bail!("Boom!!1!");
9191
}
9292
}
9393

src/book/book.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ pub fn load_book<P: AsRef<Path>>(src_dir: P, cfg: &BuildConfig) -> Result<Book>
1515

1616
let mut summary_content = String::new();
1717
File::open(summary_md)
18-
.chain_err(|| "Couldn't open SUMMARY.md")?
18+
.with_context(|| "Couldn't open SUMMARY.md")?
1919
.read_to_string(&mut summary_content)?;
2020

21-
let summary = parse_summary(&summary_content).chain_err(|| "Summary parsing failed")?;
21+
let summary = parse_summary(&summary_content).with_context(|| "Summary parsing failed")?;
2222

2323
if cfg.create_missing {
24-
create_missing(&src_dir, &summary).chain_err(|| "Unable to create missing chapters")?;
24+
create_missing(&src_dir, &summary).with_context(|| "Unable to create missing chapters")?;
2525
}
2626

2727
load_book_from_disk(&summary, src_dir)
@@ -257,11 +257,12 @@ fn load_chapter<P: AsRef<Path>>(
257257
};
258258

259259
let mut f = File::open(&location)
260-
.chain_err(|| format!("Chapter file not found, {}", link_location.display()))?;
260+
.with_context(|| format!("Chapter file not found, {}", link_location.display()))?;
261261

262262
let mut content = String::new();
263-
f.read_to_string(&mut content)
264-
.chain_err(|| format!("Unable to read \"{}\" ({})", link.name, location.display()))?;
263+
f.read_to_string(&mut content).with_context(|| {
264+
format!("Unable to read \"{}\" ({})", link.name, location.display())
265+
})?;
265266

266267
let stripped = location
267268
.strip_prefix(&src_dir)

src/book/init.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ impl BookBuilder {
6464
info!("Creating a new book with stub content");
6565

6666
self.create_directory_structure()
67-
.chain_err(|| "Unable to create directory structure")?;
67+
.with_context(|| "Unable to create directory structure")?;
6868

6969
self.create_stub_files()
70-
.chain_err(|| "Unable to create stub files")?;
70+
.with_context(|| "Unable to create stub files")?;
7171

7272
if self.create_gitignore {
7373
self.build_gitignore()
74-
.chain_err(|| "Unable to create .gitignore")?;
74+
.with_context(|| "Unable to create .gitignore")?;
7575
}
7676

7777
if self.copy_theme {
7878
self.copy_across_theme()
79-
.chain_err(|| "Unable to copy across the theme")?;
79+
.with_context(|| "Unable to copy across the theme")?;
8080
}
8181

8282
self.write_book_toml()?;
@@ -97,12 +97,12 @@ impl BookBuilder {
9797
fn write_book_toml(&self) -> Result<()> {
9898
debug!("Writing book.toml");
9999
let book_toml = self.root.join("book.toml");
100-
let cfg = toml::to_vec(&self.config).chain_err(|| "Unable to serialize the config")?;
100+
let cfg = toml::to_vec(&self.config).with_context(|| "Unable to serialize the config")?;
101101

102102
File::create(book_toml)
103-
.chain_err(|| "Couldn't create book.toml")?
103+
.with_context(|| "Couldn't create book.toml")?
104104
.write_all(&cfg)
105-
.chain_err(|| "Unable to write config to book.toml")?;
105+
.with_context(|| "Unable to write config to book.toml")?;
106106
Ok(())
107107
}
108108

@@ -174,13 +174,14 @@ impl BookBuilder {
174174
let summary = src_dir.join("SUMMARY.md");
175175
if !summary.exists() {
176176
trace!("No summary found creating stub summary and chapter_1.md.");
177-
let mut f = File::create(&summary).chain_err(|| "Unable to create SUMMARY.md")?;
177+
let mut f = File::create(&summary).with_context(|| "Unable to create SUMMARY.md")?;
178178
writeln!(f, "# Summary")?;
179179
writeln!(f)?;
180180
writeln!(f, "- [Chapter 1](./chapter_1.md)")?;
181181

182182
let chapter_1 = src_dir.join("chapter_1.md");
183-
let mut f = File::create(&chapter_1).chain_err(|| "Unable to create chapter_1.md")?;
183+
let mut f =
184+
File::create(&chapter_1).with_context(|| "Unable to create chapter_1.md")?;
184185
writeln!(f, "# Chapter 1")?;
185186
} else {
186187
trace!("Existing summary found, no need to create stub files.");

src/book/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl MDBook {
215215

216216
renderer
217217
.render(&render_context)
218-
.chain_err(|| "Rendering failed")
218+
.with_context(|| "Rendering failed")
219219
}
220220

221221
/// You can change the default renderer to another one by using this method.
@@ -282,10 +282,12 @@ impl MDBook {
282282
let output = cmd.output()?;
283283

284284
if !output.status.success() {
285-
bail!(ErrorKind::Subprocess(
286-
"Rustdoc returned an error".to_string(),
287-
output
288-
));
285+
bail!(
286+
"rustdoc returned an error:\n\
287+
\n--- stdout\n{}\n--- stderr\n{}",
288+
String::from_utf8_lossy(&output.stdout),
289+
String::from_utf8_lossy(&output.stderr)
290+
);
289291
}
290292
}
291293
}

src/book/summary.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ impl<'a> SummaryParser<'a> {
236236

237237
let prefix_chapters = self
238238
.parse_affix(true)
239-
.chain_err(|| "There was an error parsing the prefix chapters")?;
239+
.with_context(|| "There was an error parsing the prefix chapters")?;
240240
let numbered_chapters = self
241241
.parse_parts()
242-
.chain_err(|| "There was an error parsing the numbered chapters")?;
242+
.with_context(|| "There was an error parsing the numbered chapters")?;
243243
let suffix_chapters = self
244244
.parse_affix(false)
245-
.chain_err(|| "There was an error parsing the suffix chapters")?;
245+
.with_context(|| "There was an error parsing the suffix chapters")?;
246246

247247
Ok(Summary {
248248
title,
@@ -320,7 +320,7 @@ impl<'a> SummaryParser<'a> {
320320
// Parse the rest of the part.
321321
let numbered_chapters = self
322322
.parse_numbered(&mut root_items, &mut root_number)
323-
.chain_err(|| "There was an error parsing the numbered chapters")?;
323+
.with_context(|| "There was an error parsing the numbered chapters")?;
324324

325325
if let Some(title) = title {
326326
parts.push(SummaryItem::PartTitle(title));
@@ -514,8 +514,12 @@ impl<'a> SummaryParser<'a> {
514514

515515
fn parse_error<D: Display>(&self, msg: D) -> Error {
516516
let (line, col) = self.current_location();
517-
518-
ErrorKind::ParseError(line, col, msg.to_string()).into()
517+
anyhow::anyhow!(
518+
"failed to parse SUMMARY.md line {}, column {}: {}",
519+
line,
520+
col,
521+
msg
522+
)
519523
}
520524

521525
/// Try to parse the title line.
@@ -553,10 +557,9 @@ fn get_last_link(links: &mut [SummaryItem]) -> Result<(usize, &mut Link)> {
553557
.filter_map(|(i, item)| item.maybe_link_mut().map(|l| (i, l)))
554558
.rev()
555559
.next()
556-
.ok_or_else(|| {
557-
"Unable to get last link because the list of SummaryItems doesn't contain any Links"
558-
.into()
559-
})
560+
.ok_or_else(||
561+
anyhow::anyhow!("Unable to get last link because the list of SummaryItems doesn't contain any Links")
562+
)
560563
}
561564

562565
/// Removes the styling from a list of Markdown events and returns just the

src/cmd/clean.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::get_book_dir;
2+
use anyhow::Context;
23
use clap::{App, ArgMatches, SubCommand};
3-
use mdbook::errors::*;
44
use mdbook::MDBook;
55
use std::fs;
66

@@ -31,7 +31,8 @@ pub fn execute(args: &ArgMatches) -> mdbook::errors::Result<()> {
3131
};
3232

3333
if dir_to_remove.exists() {
34-
fs::remove_dir_all(&dir_to_remove).chain_err(|| "Unable to remove the build directory")?;
34+
fs::remove_dir_all(&dir_to_remove)
35+
.with_context(|| "Unable to remove the build directory")?;
3536
}
3637

3738
Ok(())

src/cmd/serve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
7474
let sockaddr: SocketAddr = address
7575
.to_socket_addrs()?
7676
.next()
77-
.ok_or_else(|| format!("no address found for {}", address))?;
77+
.ok_or_else(|| anyhow::anyhow!("no address found for {}", address))?;
7878
let build_dir = book.build_dir_for("html");
7979

8080
// A channel used to broadcast to any websockets to reload when a file changes.

src/config.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl FromStr for Config {
7979

8080
/// Load a `Config` from some string.
8181
fn from_str(src: &str) -> Result<Self> {
82-
toml::from_str(src).chain_err(|| Error::from("Invalid configuration file"))
82+
toml::from_str(src).with_context(|| "Invalid configuration file")
8383
}
8484
}
8585

@@ -88,9 +88,9 @@ impl Config {
8888
pub fn from_disk<P: AsRef<Path>>(config_file: P) -> Result<Config> {
8989
let mut buffer = String::new();
9090
File::open(config_file)
91-
.chain_err(|| "Unable to open the configuration file")?
91+
.with_context(|| "Unable to open the configuration file")?
9292
.read_to_string(&mut buffer)
93-
.chain_err(|| "Couldn't read the file")?;
93+
.with_context(|| "Couldn't read the file")?;
9494

9595
Config::from_str(&buffer)
9696
}
@@ -176,11 +176,14 @@ impl Config {
176176
/// HTML renderer is refactored to be less coupled to `mdbook` internals.
177177
#[doc(hidden)]
178178
pub fn html_config(&self) -> Option<HtmlConfig> {
179-
match self.get_deserialized_opt("output.html") {
179+
match self
180+
.get_deserialized_opt("output.html")
181+
.with_context(|| "Parsing configuration [output.html]")
182+
{
180183
Ok(Some(config)) => Some(config),
181184
Ok(None) => None,
182185
Err(e) => {
183-
utils::log_backtrace(&e.chain_err(|| "Parsing configuration [output.html]"));
186+
utils::log_backtrace(&e);
184187
None
185188
}
186189
}
@@ -208,7 +211,7 @@ impl Config {
208211
value
209212
.clone()
210213
.try_into()
211-
.chain_err(|| "Couldn't deserialize the value")
214+
.with_context(|| "Couldn't deserialize the value")
212215
})
213216
.transpose()
214217
}
@@ -220,8 +223,8 @@ impl Config {
220223
pub fn set<S: Serialize, I: AsRef<str>>(&mut self, index: I, value: S) -> Result<()> {
221224
let index = index.as_ref();
222225

223-
let value =
224-
Value::try_from(value).chain_err(|| "Unable to represent the item as a JSON Value")?;
226+
let value = Value::try_from(value)
227+
.with_context(|| "Unable to represent the item as a JSON Value")?;
225228

226229
if index.starts_with("book.") {
227230
self.book.update_value(&index[5..], value);

0 commit comments

Comments
 (0)