Skip to content

Commit 90db1e6

Browse files
committed
Auto merge of #359 - pietroalbini:failure, r=pietroalbini
Migrate from error-chain to failure
2 parents c2fcf3a + 51bf618 commit 90db1e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+830
-783
lines changed

Cargo.lock

Lines changed: 13 additions & 12 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ chrono-humanize = "0.0.11"
6161
csv = "1.0.2"
6262
rusoto_credential = "0.13.0"
6363
nix = "0.11.0"
64+
failure = "0.1.3"
6465

6566
[dev-dependencies]
6667
assert_cmd = "0.10.1"

src/actions/experiments/create.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use actions::experiments::ExperimentError;
12
use chrono::Utc;
23
use config::Config;
34
use db::{Database, QueryUtils};
4-
use errors::*;
55
use experiments::{CapLints, CrateSelect, Experiment, GitHubIssue, Mode, Status};
6+
use prelude::*;
67
use toolchain::Toolchain;
78

89
pub struct CreateExperiment {
@@ -31,15 +32,15 @@ impl CreateExperiment {
3132
}
3233
}
3334

34-
pub fn apply(self, db: &Database, config: &Config) -> Result<()> {
35+
pub fn apply(self, db: &Database, config: &Config) -> Fallible<()> {
3536
// Ensure no duplicate experiments are created
3637
if Experiment::exists(db, &self.name)? {
37-
return Err(ErrorKind::ExperimentAlreadyExists(self.name).into());
38+
return Err(ExperimentError::AlreadyExists(self.name).into());
3839
}
3940

4041
// Ensure no experiment with duplicate toolchains is created
4142
if self.toolchains[0] == self.toolchains[1] {
42-
return Err(ErrorKind::DuplicateToolchains.into());
43+
return Err(ExperimentError::DuplicateToolchains.into());
4344
}
4445

4546
let crates = ::crates::lists::get_crates(self.crates, db, config)?;
@@ -74,16 +75,18 @@ impl CreateExperiment {
7475
}
7576

7677
Ok(())
77-
})
78+
})?;
79+
80+
Ok(())
7881
}
7982
}
8083

8184
#[cfg(test)]
8285
mod tests {
8386
use super::CreateExperiment;
87+
use actions::ExperimentError;
8488
use config::Config;
8589
use db::Database;
86-
use errors::*;
8790
use experiments::{CapLints, CrateSelect, Experiment, GitHubIssue, Mode, Status};
8891
use toolchain::{MAIN_TOOLCHAIN, TEST_TOOLCHAIN};
8992

@@ -154,10 +157,10 @@ mod tests {
154157
}.apply(&db, &config)
155158
.unwrap_err();
156159

157-
match err.kind() {
158-
ErrorKind::DuplicateToolchains => {}
159-
other => panic!("received unexpected error: {}", other),
160-
}
160+
assert_eq!(
161+
err.downcast_ref(),
162+
Some(&ExperimentError::DuplicateToolchains)
163+
);
161164
}
162165

163166
#[test]
@@ -191,9 +194,9 @@ mod tests {
191194
}.apply(&db, &config)
192195
.unwrap_err();
193196

194-
match err.kind() {
195-
ErrorKind::ExperimentAlreadyExists(name) => assert_eq!(name, "foo"),
196-
other => panic!("received unexpected error: {}", other),
197-
}
197+
assert_eq!(
198+
err.downcast_ref(),
199+
Some(&ExperimentError::AlreadyExists("foo".into()))
200+
);
198201
}
199202
}

src/actions/experiments/delete.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
use actions::experiments::ExperimentError;
12
use config::Config;
23
use db::{Database, QueryUtils};
3-
use errors::*;
44
use experiments::Experiment;
5+
use prelude::*;
56

67
pub struct DeleteExperiment {
78
pub name: String,
89
}
910

1011
impl DeleteExperiment {
11-
pub fn apply(self, db: &Database, _config: &Config) -> Result<()> {
12+
pub fn apply(self, db: &Database, _config: &Config) -> Fallible<()> {
1213
if !Experiment::exists(db, &self.name)? {
13-
return Err(ErrorKind::ExperimentNotFound(self.name).into());
14+
return Err(ExperimentError::NotFound(self.name).into());
1415
}
1516

1617
// This will also delete all the data related to this experiment, thanks to the foreign
@@ -24,10 +25,9 @@ impl DeleteExperiment {
2425
#[cfg(test)]
2526
mod tests {
2627
use super::DeleteExperiment;
27-
use actions::CreateExperiment;
28+
use actions::{CreateExperiment, ExperimentError};
2829
use config::Config;
2930
use db::Database;
30-
use errors::*;
3131
use experiments::Experiment;
3232

3333
#[test]
@@ -40,10 +40,10 @@ mod tests {
4040
}.apply(&db, &config)
4141
.unwrap_err();
4242

43-
match err.kind() {
44-
ErrorKind::ExperimentNotFound(name) => assert_eq!(name, "dummy"),
45-
other => panic!("unexpected error: {}", other),
46-
}
43+
assert_eq!(
44+
err.downcast_ref(),
45+
Some(&ExperimentError::NotFound("dummy".into()))
46+
);
4747
}
4848

4949
#[test]

src/actions/experiments/edit.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use actions::experiments::ExperimentError;
12
use config::Config;
23
use db::{Database, QueryUtils};
3-
use errors::*;
44
use experiments::{CapLints, CrateSelect, Experiment, Mode, Status};
5+
use prelude::*;
56
use toolchain::Toolchain;
67

78
pub struct EditExperiment {
@@ -26,18 +27,18 @@ impl EditExperiment {
2627
}
2728
}
2829

29-
pub fn apply(mut self, db: &Database, config: &Config) -> Result<bool> {
30+
pub fn apply(mut self, db: &Database, config: &Config) -> Fallible<bool> {
3031
let mut ex = match Experiment::get(db, &self.name)? {
3132
Some(ex) => ex,
32-
None => return Err(ErrorKind::ExperimentNotFound(self.name).into()),
33+
None => return Err(ExperimentError::NotFound(self.name).into()),
3334
};
3435

3536
// Ensure no change is made to running or complete experiments
3637
if ex.status != Status::Queued {
37-
return Err(ErrorKind::CanEditOnlyQueuedExperiments.into());
38+
return Err(ExperimentError::CanOnlyEditQueuedExperiments.into());
3839
}
3940

40-
db.transaction(|t| {
41+
Ok(db.transaction(|t| {
4142
let mut has_changed = false;
4243

4344
// Try to update both toolchains
@@ -47,7 +48,7 @@ impl EditExperiment {
4748

4849
// Ensure no duplicate toolchain is inserted
4950
if ex.toolchains[0] == ex.toolchains[1] {
50-
return Err(ErrorKind::DuplicateToolchains.into());
51+
return Err(ExperimentError::DuplicateToolchains.into());
5152
}
5253

5354
let changes = t.execute(
@@ -123,17 +124,16 @@ impl EditExperiment {
123124
}
124125

125126
Ok(has_changed)
126-
})
127+
})?)
127128
}
128129
}
129130

130131
#[cfg(test)]
131132
mod tests {
132133
use super::EditExperiment;
133-
use actions::CreateExperiment;
134+
use actions::{CreateExperiment, ExperimentError};
134135
use config::Config;
135136
use db::Database;
136-
use errors::*;
137137
use experiments::{CapLints, CrateSelect, Experiment, Mode, Status};
138138
use toolchain::{MAIN_TOOLCHAIN, TEST_TOOLCHAIN};
139139

@@ -217,10 +217,10 @@ mod tests {
217217
edit.toolchains[1] = Some(MAIN_TOOLCHAIN.clone());
218218

219219
let err = edit.apply(&db, &config).unwrap_err();
220-
match err.kind() {
221-
ErrorKind::DuplicateToolchains => {}
222-
other => panic!("received unexpected error: {}", other),
223-
}
220+
assert_eq!(
221+
err.downcast_ref(),
222+
Some(&ExperimentError::DuplicateToolchains)
223+
);
224224
}
225225

226226
#[test]
@@ -233,10 +233,10 @@ mod tests {
233233
let err = EditExperiment::dummy("foo")
234234
.apply(&db, &config)
235235
.unwrap_err();
236-
match err.kind() {
237-
ErrorKind::ExperimentNotFound(name) => assert_eq!(name, "foo"),
238-
other => panic!("received unexpected error: {}", other),
239-
}
236+
assert_eq!(
237+
err.downcast_ref(),
238+
Some(&ExperimentError::NotFound("foo".into()))
239+
);
240240
}
241241

242242
#[test]
@@ -255,9 +255,9 @@ mod tests {
255255
let err = EditExperiment::dummy("foo")
256256
.apply(&db, &config)
257257
.unwrap_err();
258-
match err.kind() {
259-
ErrorKind::CanEditOnlyQueuedExperiments => {}
260-
other => panic!("received unexpected error: {}", other),
261-
}
258+
assert_eq!(
259+
err.downcast_ref(),
260+
Some(&ExperimentError::CanOnlyEditQueuedExperiments)
261+
);
262262
}
263263
}

src/actions/experiments/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ mod edit;
55
pub use self::create::CreateExperiment;
66
pub use self::delete::DeleteExperiment;
77
pub use self::edit::EditExperiment;
8+
9+
#[derive(Debug, Fail)]
10+
#[cfg_attr(test, derive(PartialEq, Eq))]
11+
pub enum ExperimentError {
12+
#[fail(display = "experiment '{}' not found", _0)]
13+
NotFound(String),
14+
#[fail(display = "experiment '{}' already exists", _0)]
15+
AlreadyExists(String),
16+
#[fail(display = "duplicate toolchains provided")]
17+
DuplicateToolchains,
18+
#[fail(display = "it's only possible to edit queued experiments")]
19+
CanOnlyEditQueuedExperiments,
20+
}

src/actions/lists/update.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use config::Config;
22
use crates::lists::{GitHubList, List, LocalList, RegistryList};
33
use db::Database;
4-
use errors::*;
4+
use prelude::*;
55

66
pub struct UpdateLists {
77
pub github: bool,
@@ -20,7 +20,7 @@ impl Default for UpdateLists {
2020
}
2121

2222
impl UpdateLists {
23-
pub fn apply(self, db: &Database, _config: &Config) -> Result<()> {
23+
pub fn apply(self, db: &Database, _config: &Config) -> Fallible<()> {
2424
if self.github {
2525
info!("updating GitHub repositories list");
2626
GitHubList::default().update(db)?;

0 commit comments

Comments
 (0)