Skip to content

Commit 960e3e4

Browse files
use failure instead of error-chain
needed because cargo's CargoError is a re-export of failure's Error, and doesn't work with error-chain
1 parent 8abb2dc commit 960e3e4

File tree

14 files changed

+66
-99
lines changed

14 files changed

+66
-99
lines changed

Cargo.lock

Lines changed: 14 additions & 34 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ r2d2_postgres = "0.12"
2525
url = "1.4"
2626
libc = "0.2"
2727
badge = { version = "0", path = "src/web/badge" }
28-
error-chain = "0.12"
28+
failure = "0.1"
2929
comrak = { version = "0.2.10", default-features = false }
3030
toml = "0.4"
3131
html5ever = "0.22"
@@ -50,6 +50,9 @@ time = "0.1"
5050
git2 = "0.7"
5151
sass-rs = "0.2"
5252

53+
[patch.crates-io]
54+
failure = { git = "https://github.com/rust-lang-nursery/failure.git" }
55+
5356
[[bin]]
5457
name = "cratesfyi"
5558
test = false

src/db/add_package.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use semver;
1818
use postgres::Connection;
1919
use time;
2020
use error::Result;
21-
21+
use failure::err_msg;
2222

2323
/// Adds a package into database.
2424
///
@@ -222,7 +222,7 @@ fn convert_dependencies(pkg: &Package) -> Vec<(String, String)> {
222222

223223
/// Reads readme if there is any read defined in Cargo.toml of a Package
224224
fn get_readme(pkg: &Package) -> Result<Option<String>> {
225-
let readme_path = PathBuf::from(try!(source_path(&pkg).ok_or("File not found")))
225+
let readme_path = PathBuf::from(try!(source_path(&pkg).ok_or_else(|| err_msg("File not found"))))
226226
.join(pkg.manifest().metadata().readme.clone().unwrap_or("README.md".to_owned()));
227227

228228
if !readme_path.exists() {
@@ -240,7 +240,7 @@ fn get_rustdoc(pkg: &Package) -> Result<Option<String>> {
240240
if pkg.manifest().targets()[0].src_path().is_absolute() {
241241
read_rust_doc(pkg.manifest().targets()[0].src_path())
242242
} else {
243-
let mut path = PathBuf::from(try!(source_path(&pkg).ok_or("File not found")));
243+
let mut path = PathBuf::from(try!(source_path(&pkg).ok_or_else(|| err_msg("File not found"))));
244244
path.push(pkg.manifest().targets()[0].src_path());
245245
read_rust_doc(path.as_path())
246246
}
@@ -289,31 +289,31 @@ fn get_release_time_yanked_downloads
289289
let versions = try!(json.as_object()
290290
.and_then(|o| o.get("versions"))
291291
.and_then(|v| v.as_array())
292-
.ok_or("Not a JSON object"));
292+
.ok_or_else(|| err_msg("Not a JSON object")));
293293

294294
let (mut release_time, mut yanked, mut downloads) = (None, None, None);
295295

296296
for version in versions {
297-
let version = try!(version.as_object().ok_or("Not a JSON object"));
297+
let version = try!(version.as_object().ok_or_else(|| err_msg("Not a JSON object")));
298298
let version_num = try!(version.get("num")
299299
.and_then(|v| v.as_string())
300-
.ok_or("Not a JSON object"));
300+
.ok_or_else(|| err_msg("Not a JSON object")));
301301

302302
if &semver::Version::parse(version_num).unwrap() == pkg.manifest().version() {
303303
let release_time_raw = try!(version.get("created_at")
304304
.and_then(|c| c.as_string())
305-
.ok_or("Not a JSON object"));
305+
.ok_or_else(|| err_msg("Not a JSON object")));
306306
release_time = Some(time::strptime(release_time_raw, "%Y-%m-%dT%H:%M:%S")
307307
.unwrap()
308308
.to_timespec());
309309

310310
yanked = Some(try!(version.get("yanked")
311311
.and_then(|c| c.as_boolean())
312-
.ok_or("Not a JSON object")));
312+
.ok_or_else(|| err_msg("Not a JSON object"))));
313313

314314
downloads = Some(try!(version.get("downloads")
315315
.and_then(|c| c.as_i64())
316-
.ok_or("Not a JSON object")) as i32);
316+
.ok_or_else(|| err_msg("Not a JSON object"))) as i32);
317317

318318
break;
319319
}

src/db/file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_serialize::json::{Json, ToJson};
1111
use std::fs::File;
1212
use std::io::Read;
1313
use error::Result;
14-
14+
use failure::err_msg;
1515

1616

1717
fn file_path(prefix: &str, name: &str) -> String {
@@ -49,7 +49,7 @@ pub fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<String>> {
4949
let mut files: Vec<String> = Vec::new();
5050

5151
if !path.exists() {
52-
return Err("File not found".into());
52+
return Err(err_msg("File not found"));
5353
} else if path.is_file() {
5454
path.file_name()
5555
.and_then(|name| name.to_str())

src/docbuilder/chroot_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use utils::{get_package, source_path, copy_dir, copy_doc_dir,
55
update_sources, parse_rustc_version, command_result};
66
use db::{connect_db, add_package_into_database, add_build_into_database, add_path_into_database};
77
use cargo::core::Package;
8+
use cargo::util::CargoResultExt;
89
use std::process::Command;
910
use std::path::PathBuf;
1011
use std::fs::remove_dir_all;
1112
use postgres::Connection;
1213
use rustc_serialize::json::Json;
13-
use error::{Result, ResultExt};
14+
use error::Result;
1415

1516

1617
/// List of targets supported by docs.rs
@@ -115,7 +116,6 @@ impl DocBuilder {
115116

116117
/// Builds documentation of a package with cratesfyi in chroot environment
117118
fn build_package_in_chroot(&self, package: &Package) -> ChrootBuilderResult {
118-
use std::error::Error as StdError;
119119
debug!("Building package in chroot");
120120
let (rustc_version, cratesfyi_version) = self.get_versions();
121121
let cmd = format!("cratesfyi doc {} ={}",
@@ -134,7 +134,7 @@ impl DocBuilder {
134134
}
135135
Err(e) => {
136136
ChrootBuilderResult {
137-
output: e.description().to_owned(),
137+
output: e.to_string(),
138138
build_success: false,
139139
have_doc: false,
140140
have_examples: self.have_examples(&package),
@@ -356,7 +356,7 @@ impl DocBuilder {
356356
let rustc_version = parse_rustc_version(&res.rustc_version)?;
357357

358358
if !res.build_success {
359-
return Err(format!("Failed to build empty crate for: {}", res.rustc_version).into());
359+
return Err(format_err!("Failed to build empty crate for: {}", res.rustc_version));
360360
}
361361

362362
info!("Copying essential files for: {}", res.rustc_version);

src/docbuilder/crates.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fs;
55
use std::path::PathBuf;
66
use rustc_serialize::json::Json;
77
use error::Result;
8-
8+
use failure::err_msg;
99

1010
fn crates_from_file<F>(path: &PathBuf, func: &mut F) -> Result<()>
1111
where F: FnMut(&str, &str) -> ()
@@ -28,13 +28,13 @@ fn crates_from_file<F>(path: &PathBuf, func: &mut F) -> Result<()>
2828
Err(_) => continue,
2929
};
3030

31-
let obj = try!(data.as_object().ok_or("Not a JSON object"));
31+
let obj = try!(data.as_object().ok_or_else(|| err_msg("Not a JSON object")));
3232
let crate_name = try!(obj.get("name")
3333
.and_then(|n| n.as_string())
34-
.ok_or("`name` not found in JSON object"));
34+
.ok_or_else(|| err_msg("`name` not found in JSON object")));
3535
let vers = try!(obj.get("vers")
3636
.and_then(|n| n.as_string())
37-
.ok_or("`vers` not found in JSON object"));
37+
.ok_or_else(|| err_msg("`vers` not found in JSON object")));
3838

3939
// Skip yanked crates
4040
if obj.get("yanked").and_then(|n| n.as_boolean()).unwrap_or(false) {
@@ -63,7 +63,7 @@ pub fn crates_from_path<F>(path: &PathBuf, func: &mut F) -> Result<()>
6363
{
6464

6565
if !path.is_dir() {
66-
return Err("Not a directory".into());
66+
return Err(err_msg("Not a directory"));
6767
}
6868

6969
for file in try!(path.read_dir()) {

src/docbuilder/metadata.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::Path;
33
use cargo::core::Package;
44
use toml::Value;
55
use error::Result;
6-
6+
use failure::err_msg;
77

88
/// Metadata for custom builds
99
///
@@ -61,14 +61,14 @@ pub struct Metadata {
6161

6262
impl Metadata {
6363
pub fn from_package(pkg: &Package) -> Result<Metadata> {
64-
let src_path = pkg.manifest_path().parent().ok_or("Source path not available")?;
64+
let src_path = pkg.manifest_path().parent().ok_or_else(|| err_msg("Source path not available"))?;
6565
for c in ["Cargo.toml.orig", "Cargo.toml"].iter() {
6666
let manifest_path = src_path.clone().join(c);
6767
if manifest_path.exists() {
6868
return Ok(Metadata::from_manifest(manifest_path));
6969
}
7070
}
71-
Err("Manifest not found".into())
71+
Err(err_msg("Manifest not found"))
7272
}
7373

7474
pub fn from_manifest<P: AsRef<Path>>(path: P) -> Metadata {

src/docbuilder/options.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{env, fmt};
44
use std::path::PathBuf;
55
use error::Result;
6-
6+
use failure::err_msg;
77

88
#[derive(Clone)]
99
pub struct DocBuilderOptions {
@@ -94,16 +94,16 @@ impl DocBuilderOptions {
9494

9595
pub fn check_paths(&self) -> Result<()> {
9696
if !self.destination.exists() {
97-
return Err("Destination path not exists".into());
97+
return Err(err_msg("Destination path not exists"));
9898
}
9999
if !self.chroot_path.exists() {
100-
return Err("Chroot path not exists".into());
100+
return Err(err_msg("Chroot path not exists"));
101101
}
102102
if !self.crates_io_index_path.exists() {
103-
return Err("crates.io-index path not exists".into());
103+
return Err(err_msg("crates.io-index path not exists"));
104104
}
105105
if !self.crates_io_index_path.exists() {
106-
return Err("Logs path not exists".into());
106+
return Err(err_msg("Logs path not exists"));
107107
}
108108
Ok(())
109109
}

src/error.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
11
//! Errors used in cratesfyi
22
3-
use std::io;
4-
use rustc_serialize::json;
5-
use postgres;
6-
use cargo;
7-
use reqwest;
8-
use magic::MagicError;
9-
use git2;
10-
use regex;
3+
use std::result::Result as StdResult;
114

5+
pub use failure::{Error, ResultExt};
126

13-
error_chain! {
14-
foreign_links {
15-
IoError(io::Error);
16-
JsonBuilderError(json::BuilderError);
17-
PostgresConnectError(postgres::error::ConnectError);
18-
PostgresError(postgres::error::Error);
19-
ReqwestError(reqwest::Error);
20-
Git2Error(git2::Error);
21-
MagicError(MagicError);
22-
CargoError(cargo::CargoError);
23-
RegexError(regex::Error);
24-
}
25-
}
7+
pub type Result<T> = StdResult<T, Error>;

0 commit comments

Comments
 (0)