Skip to content

replace deprecated failure library with anyhow and thiserror #1456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 52 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ r2d2_postgres = "0.18"
url = { version = "2.1.1", features = ["serde"] }
badge = { path = "crates/badge" }
docsrs-metadata = { path = "crates/metadata" }
backtrace = "0.3"
failure = { version = "0.1.3", features = ["backtrace"] }
anyhow = { version = "1.0.42", features = ["backtrace"]}
backtrace = "0.3.61"
failure = "0.1.8"
thiserror = "1.0.26"
comrak = { version = "0.10.1", default-features = false }
toml = "0.5"
schemamama = "0.3"
Expand Down
37 changes: 20 additions & 17 deletions src/bin/cratesfyi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use std::fmt::Write;
use std::path::PathBuf;
use std::sync::Arc;

use anyhow::{anyhow, Context as _, Error, Result};
use docs_rs::db::{self, add_path_into_database, Pool, PoolClient};
use docs_rs::repositories::RepositoryStatsUpdater;
use docs_rs::utils::{remove_crate_priority, set_crate_priority};
use docs_rs::{
BuildQueue, Config, Context, DocBuilder, Index, Metrics, PackageKind, RustwideBuilder, Server,
Storage,
};
use failure::{err_msg, Error, ResultExt};
use once_cell::sync::OnceCell;
use structopt::StructOpt;
use strum::VariantNames;
Expand All @@ -21,12 +21,14 @@ pub fn main() {

if let Err(err) = CommandLine::from_args().handle_args() {
let mut msg = format!("Error: {}", err);
for cause in err.iter_causes() {
for cause in err.chain() {
write!(msg, "\n\nCaused by:\n {}", cause).unwrap();
}
eprintln!("{}", msg);
if !err.backtrace().is_empty() {
eprintln!("\nStack backtrace:\n{}", err.backtrace());

let backtrace = err.backtrace().to_string();
if !backtrace.is_empty() {
eprintln!("\nStack backtrace:\n{}", backtrace);
}
std::process::exit(1);
}
Expand Down Expand Up @@ -108,7 +110,7 @@ enum CommandLine {
}

impl CommandLine {
pub fn handle_args(self) -> Result<(), Error> {
pub fn handle_args(self) -> Result<()> {
let ctx = BinContext::new();

match self {
Expand Down Expand Up @@ -166,7 +168,7 @@ enum QueueSubcommand {
}

impl QueueSubcommand {
pub fn handle_args(self, ctx: BinContext) -> Result<(), Error> {
pub fn handle_args(self, ctx: BinContext) -> Result<()> {
match self {
Self::Add {
crate_name,
Expand Down Expand Up @@ -205,7 +207,7 @@ enum PrioritySubcommand {
}

impl PrioritySubcommand {
pub fn handle_args(self, ctx: BinContext) -> Result<(), Error> {
pub fn handle_args(self, ctx: BinContext) -> Result<()> {
match self {
Self::Set { pattern, priority } => {
set_crate_priority(&mut *ctx.conn()?, &pattern, priority)
Expand Down Expand Up @@ -239,7 +241,7 @@ struct Build {
}

impl Build {
pub fn handle_args(self, ctx: BinContext) -> Result<(), Error> {
pub fn handle_args(self, ctx: BinContext) -> Result<()> {
self.subcommand.handle_args(ctx, self.skip_if_exists)
}
}
Expand Down Expand Up @@ -286,10 +288,10 @@ enum BuildSubcommand {
}

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

let rustwide_builder = || -> Result<RustwideBuilder, Error> {
let rustwide_builder = || -> Result<RustwideBuilder> {
let mut builder = RustwideBuilder::init(&ctx)?;
builder.set_skip_build_if_exists(skip_if_exists);
Ok(builder)
Expand Down Expand Up @@ -317,9 +319,10 @@ impl BuildSubcommand {
let registry_url = ctx.config()?.registry_url.clone();
builder
.build_package(
&crate_name.ok_or_else(|| err_msg("must specify name if not local"))?,
&crate_name
.with_context(|| anyhow!("must specify name if not local"))?,
&crate_version
.ok_or_else(|| err_msg("must specify version if not local"))?,
.with_context(|| anyhow!("must specify version if not local"))?,
registry_url
.as_ref()
.map(|s| PackageKind::Registry(s.as_str()))
Expand Down Expand Up @@ -412,7 +415,7 @@ enum DatabaseSubcommand {
}

impl DatabaseSubcommand {
pub fn handle_args(self, ctx: BinContext) -> Result<(), Error> {
pub fn handle_args(self, ctx: BinContext) -> Result<()> {
match self {
Self::Migrate { version } => {
db::migrate(version, &mut *ctx.conn()?)
Expand Down Expand Up @@ -482,7 +485,7 @@ enum BlacklistSubcommand {
}

impl BlacklistSubcommand {
fn handle_args(self, ctx: BinContext) -> Result<(), Error> {
fn handle_args(self, ctx: BinContext) -> Result<()> {
let mut conn = &mut *ctx.conn()?;
match self {
Self::List => {
Expand Down Expand Up @@ -545,14 +548,14 @@ impl BinContext {
}
}

fn conn(&self) -> Result<PoolClient, Error> {
fn conn(&self) -> Result<PoolClient> {
Ok(self.pool()?.get()?)
}
}

macro_rules! lazy {
( $(fn $name:ident($self:ident) -> $type:ty = $init:expr);+ $(;)? ) => {
$(fn $name(&$self) -> Result<Arc<$type>, Error> {
$(fn $name(&$self) -> Result<Arc<$type>> {
Ok($self
.$name
.get_or_try_init::<_, Error>(|| Ok(Arc::new($init)))?
Expand Down Expand Up @@ -591,7 +594,7 @@ impl Context for BinContext {
};
}

fn pool(&self) -> Result<Pool, Error> {
fn pool(&self) -> Result<Pool> {
Ok(self
.pool
.get_or_try_init::<_, Error>(|| Ok(Pool::new(&*self.config()?, self.metrics()?)?))?
Expand Down
4 changes: 2 additions & 2 deletions src/build_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ mod tests {
let assert_next_and_fail = |name| -> Result<()> {
queue.process_next_crate(|krate| {
assert_eq!(name, krate.name);
failure::bail!("simulate a failure");
anyhow::bail!("simulate a failure");
})?;
Ok(())
};
Expand Down Expand Up @@ -278,7 +278,7 @@ mod tests {
assert_eq!(queue.failed_count()?, 0);
queue.process_next_crate(|krate| {
assert_eq!("foo", krate.name);
failure::bail!("this failed");
anyhow::bail!("this failed");
})?;
}
assert_eq!(queue.failed_count()?, 1);
Expand Down
Loading