Skip to content

Commit b2eabd6

Browse files
authored
Merge pull request #1456 from syphar/anyhow
replace deprecated failure library with anyhow and thiserror
2 parents 3e2f1c0 + 8af425a commit b2eabd6

40 files changed

+427
-398
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ r2d2_postgres = "0.18"
3535
url = { version = "2.1.1", features = ["serde"] }
3636
badge = { path = "crates/badge" }
3737
docsrs-metadata = { path = "crates/metadata" }
38-
backtrace = "0.3"
39-
failure = { version = "0.1.3", features = ["backtrace"] }
38+
anyhow = { version = "1.0.42", features = ["backtrace"]}
39+
backtrace = "0.3.61"
40+
failure = "0.1.8"
41+
thiserror = "1.0.26"
4042
comrak = { version = "0.10.1", default-features = false }
4143
toml = "0.5"
4244
schemamama = "0.3"

src/bin/cratesfyi.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use std::fmt::Write;
33
use std::path::PathBuf;
44
use std::sync::Arc;
55

6+
use anyhow::{anyhow, Context as _, Error, Result};
67
use docs_rs::db::{self, add_path_into_database, Pool, PoolClient};
78
use docs_rs::repositories::RepositoryStatsUpdater;
89
use docs_rs::utils::{remove_crate_priority, set_crate_priority};
910
use docs_rs::{
1011
BuildQueue, Config, Context, DocBuilder, Index, Metrics, PackageKind, RustwideBuilder, Server,
1112
Storage,
1213
};
13-
use failure::{err_msg, Error, ResultExt};
1414
use once_cell::sync::OnceCell;
1515
use structopt::StructOpt;
1616
use strum::VariantNames;
@@ -21,12 +21,14 @@ pub fn main() {
2121

2222
if let Err(err) = CommandLine::from_args().handle_args() {
2323
let mut msg = format!("Error: {}", err);
24-
for cause in err.iter_causes() {
24+
for cause in err.chain() {
2525
write!(msg, "\n\nCaused by:\n {}", cause).unwrap();
2626
}
2727
eprintln!("{}", msg);
28-
if !err.backtrace().is_empty() {
29-
eprintln!("\nStack backtrace:\n{}", err.backtrace());
28+
29+
let backtrace = err.backtrace().to_string();
30+
if !backtrace.is_empty() {
31+
eprintln!("\nStack backtrace:\n{}", backtrace);
3032
}
3133
std::process::exit(1);
3234
}
@@ -108,7 +110,7 @@ enum CommandLine {
108110
}
109111

110112
impl CommandLine {
111-
pub fn handle_args(self) -> Result<(), Error> {
113+
pub fn handle_args(self) -> Result<()> {
112114
let ctx = BinContext::new();
113115

114116
match self {
@@ -166,7 +168,7 @@ enum QueueSubcommand {
166168
}
167169

168170
impl QueueSubcommand {
169-
pub fn handle_args(self, ctx: BinContext) -> Result<(), Error> {
171+
pub fn handle_args(self, ctx: BinContext) -> Result<()> {
170172
match self {
171173
Self::Add {
172174
crate_name,
@@ -205,7 +207,7 @@ enum PrioritySubcommand {
205207
}
206208

207209
impl PrioritySubcommand {
208-
pub fn handle_args(self, ctx: BinContext) -> Result<(), Error> {
210+
pub fn handle_args(self, ctx: BinContext) -> Result<()> {
209211
match self {
210212
Self::Set { pattern, priority } => {
211213
set_crate_priority(&mut *ctx.conn()?, &pattern, priority)
@@ -239,7 +241,7 @@ struct Build {
239241
}
240242

241243
impl Build {
242-
pub fn handle_args(self, ctx: BinContext) -> Result<(), Error> {
244+
pub fn handle_args(self, ctx: BinContext) -> Result<()> {
243245
self.subcommand.handle_args(ctx, self.skip_if_exists)
244246
}
245247
}
@@ -286,10 +288,10 @@ enum BuildSubcommand {
286288
}
287289

288290
impl BuildSubcommand {
289-
pub fn handle_args(self, ctx: BinContext, skip_if_exists: bool) -> Result<(), Error> {
291+
pub fn handle_args(self, ctx: BinContext, skip_if_exists: bool) -> Result<()> {
290292
let docbuilder = DocBuilder::new(ctx.config()?, ctx.pool()?, ctx.build_queue()?);
291293

292-
let rustwide_builder = || -> Result<RustwideBuilder, Error> {
294+
let rustwide_builder = || -> Result<RustwideBuilder> {
293295
let mut builder = RustwideBuilder::init(&ctx)?;
294296
builder.set_skip_build_if_exists(skip_if_exists);
295297
Ok(builder)
@@ -317,9 +319,10 @@ impl BuildSubcommand {
317319
let registry_url = ctx.config()?.registry_url.clone();
318320
builder
319321
.build_package(
320-
&crate_name.ok_or_else(|| err_msg("must specify name if not local"))?,
322+
&crate_name
323+
.with_context(|| anyhow!("must specify name if not local"))?,
321324
&crate_version
322-
.ok_or_else(|| err_msg("must specify version if not local"))?,
325+
.with_context(|| anyhow!("must specify version if not local"))?,
323326
registry_url
324327
.as_ref()
325328
.map(|s| PackageKind::Registry(s.as_str()))
@@ -412,7 +415,7 @@ enum DatabaseSubcommand {
412415
}
413416

414417
impl DatabaseSubcommand {
415-
pub fn handle_args(self, ctx: BinContext) -> Result<(), Error> {
418+
pub fn handle_args(self, ctx: BinContext) -> Result<()> {
416419
match self {
417420
Self::Migrate { version } => {
418421
db::migrate(version, &mut *ctx.conn()?)
@@ -482,7 +485,7 @@ enum BlacklistSubcommand {
482485
}
483486

484487
impl BlacklistSubcommand {
485-
fn handle_args(self, ctx: BinContext) -> Result<(), Error> {
488+
fn handle_args(self, ctx: BinContext) -> Result<()> {
486489
let mut conn = &mut *ctx.conn()?;
487490
match self {
488491
Self::List => {
@@ -545,14 +548,14 @@ impl BinContext {
545548
}
546549
}
547550

548-
fn conn(&self) -> Result<PoolClient, Error> {
551+
fn conn(&self) -> Result<PoolClient> {
549552
Ok(self.pool()?.get()?)
550553
}
551554
}
552555

553556
macro_rules! lazy {
554557
( $(fn $name:ident($self:ident) -> $type:ty = $init:expr);+ $(;)? ) => {
555-
$(fn $name(&$self) -> Result<Arc<$type>, Error> {
558+
$(fn $name(&$self) -> Result<Arc<$type>> {
556559
Ok($self
557560
.$name
558561
.get_or_try_init::<_, Error>(|| Ok(Arc::new($init)))?
@@ -591,7 +594,7 @@ impl Context for BinContext {
591594
};
592595
}
593596

594-
fn pool(&self) -> Result<Pool, Error> {
597+
fn pool(&self) -> Result<Pool> {
595598
Ok(self
596599
.pool
597600
.get_or_try_init::<_, Error>(|| Ok(Pool::new(&*self.config()?, self.metrics()?)?))?

src/build_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ mod tests {
170170
let assert_next_and_fail = |name| -> Result<()> {
171171
queue.process_next_crate(|krate| {
172172
assert_eq!(name, krate.name);
173-
failure::bail!("simulate a failure");
173+
anyhow::bail!("simulate a failure");
174174
})?;
175175
Ok(())
176176
};
@@ -278,7 +278,7 @@ mod tests {
278278
assert_eq!(queue.failed_count()?, 0);
279279
queue.process_next_crate(|krate| {
280280
assert_eq!("foo", krate.name);
281-
failure::bail!("this failed");
281+
anyhow::bail!("this failed");
282282
})?;
283283
}
284284
assert_eq!(queue.failed_count()?, 1);

0 commit comments

Comments
 (0)