Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit 2e4df30

Browse files
Grisha Kruglovgrigoryk
authored andcommitted
convert tolstoy/ to failure
1 parent 56fad24 commit 2e4df30

7 files changed

Lines changed: 41 additions & 75 deletions

File tree

src/entity_builder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,7 @@ mod testing {
381381
extern crate mentat_db;
382382

383383
// For matching inside a test.
384-
use mentat_db::ErrorKind::{
385-
UnrecognizedEntid,
386-
};
384+
use mentat_db::DbError;
387385

388386
use ::{
389387
Conn,
@@ -424,7 +422,7 @@ mod testing {
424422
let mut in_progress = conn.begin_transaction(&mut sqlite).expect("begun successfully");
425423

426424
// This should fail: unrecognized entid.
427-
if let Err(Error(MentatError::DbError(UnrecognizedEntid(e)), _)) = in_progress.transact_terms(terms, tempids) {
425+
if let Ok(DbError::UnrecognizedEntid(e)) = in_progress.transact_terms(terms, tempids).expect_err("expected transact to fail").downcast() {
428426
assert_eq!(e, 999);
429427
} else {
430428
panic!("Should have rejected the entid.");

tolstoy/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ workspace = ".."
55
authors = ["Grisha Kruglov <gkruglov@mozilla.com>"]
66

77
[dependencies]
8+
failure = "0.1.1"
9+
failure_derive = "0.1.1"
810
futures = "0.1"
911
hyper = "0.11"
1012
tokio-core = "0.1"
@@ -15,8 +17,6 @@ serde_derive = "1.0"
1517
lazy_static = "0.2"
1618
uuid = { version = "0.5", features = ["v4", "serde"] }
1719

18-
error-chain = { git = "https://github.com/rnewman/error-chain", branch = "rnewman/sync" }
19-
2020
[dependencies.mentat_core]
2121
path = "../core"
2222

tolstoy/src/errors.rs

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,34 @@
1010

1111
#![allow(dead_code)]
1212

13-
use std;
14-
use hyper;
15-
use rusqlite;
16-
use uuid;
17-
use mentat_db;
18-
use serde_cbor;
19-
use serde_json;
13+
use failure::Error;
2014

21-
error_chain! {
22-
types {
23-
Error, ErrorKind, ResultExt, Result;
24-
}
25-
26-
foreign_links {
27-
IOError(std::io::Error);
28-
HttpError(hyper::Error);
29-
HyperUriError(hyper::error::UriError);
30-
SqlError(rusqlite::Error);
31-
UuidParseError(uuid::ParseError);
32-
Utf8Error(std::str::Utf8Error);
33-
JsonError(serde_json::Error);
34-
CborError(serde_cbor::error::Error);
35-
}
15+
#[macro_export]
16+
macro_rules! bail {
17+
($e:expr) => (
18+
return Err($e.into());
19+
)
20+
}
3621

37-
links {
38-
DbError(mentat_db::Error, mentat_db::ErrorKind);
39-
}
22+
pub type Result<T> = ::std::result::Result<T, Error>;
4023

41-
errors {
42-
TxIncorrectlyMapped(n: usize) {
43-
description("encountered more than one uuid mapping for tx")
44-
display("expected one, found {} uuid mappings for tx", n)
45-
}
24+
#[derive(Debug, Fail)]
25+
pub enum TolstoyError {
26+
#[fail(display = "Received bad response from the server: {}", _0)]
27+
BadServerResponse(String),
4628

47-
UnexpectedState(t: String) {
48-
description("encountered unexpected state")
49-
display("encountered unexpected state: {}", t)
50-
}
29+
#[fail(display = "encountered more than one metadata value for key: {}", _0)]
30+
DuplicateMetadata(String),
5131

52-
NotYetImplemented(t: String) {
53-
description("not yet implemented")
54-
display("not yet implemented: {}", t)
55-
}
32+
#[fail(display = "transaction processor didn't say it was done")]
33+
TxProcessorUnfinished,
5634

57-
DuplicateMetadata(k: String) {
58-
description("encountered more than one metadata value for key")
59-
display("encountered more than one metadata value for key: {}", k)
60-
}
35+
#[fail(display = "expected one, found {} uuid mappings for tx", _0)]
36+
TxIncorrectlyMapped(usize),
6137

62-
TxProcessorUnfinished {
63-
description("Tx processor couldn't finish")
64-
display("Tx processor couldn't finish")
65-
}
38+
#[fail(display = "encountered unexpected state: {}", _0)]
39+
UnexpectedState(String),
6640

67-
BadServerResponse(s: String) {
68-
description("Received bad response from the server")
69-
display("Received bad response from the server: {}", s)
70-
}
71-
}
41+
#[fail(display = "not yet implemented: {}", _0)]
42+
NotYetImplemented(String),
7243
}

tolstoy/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
99
// specific language governing permissions and limitations under the License.
1010

11-
// For error_chain:
12-
#![recursion_limit="128"]
13-
11+
extern crate failure;
1412
#[macro_use]
15-
extern crate error_chain;
13+
extern crate failure_derive;
1614

1715
#[macro_use]
1816
extern crate lazy_static;
@@ -33,16 +31,15 @@ extern crate mentat_core;
3331
extern crate rusqlite;
3432
extern crate uuid;
3533

34+
#[macro_use]
35+
pub mod errors;
3636
pub mod schema;
3737
pub mod metadata;
3838
pub mod tx_processor;
39-
pub mod errors;
4039
pub mod syncer;
4140
pub mod tx_mapper;
4241
pub use syncer::Syncer;
4342
pub use errors::{
44-
Error,
45-
ErrorKind,
43+
TolstoyError,
4644
Result,
47-
ResultExt,
4845
};

tolstoy/src/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use uuid::Uuid;
1515

1616
use schema;
1717
use errors::{
18-
ErrorKind,
18+
TolstoyError,
1919
Result,
2020
};
2121

@@ -42,7 +42,7 @@ impl HeadTrackable for SyncMetadataClient {
4242
let updated = tx.execute("UPDATE tolstoy_metadata SET value = ? WHERE key = ?",
4343
&[&uuid_bytes, &schema::REMOTE_HEAD_KEY])?;
4444
if updated != 1 {
45-
bail!(ErrorKind::DuplicateMetadata(schema::REMOTE_HEAD_KEY.into()));
45+
bail!(TolstoyError::DuplicateMetadata(schema::REMOTE_HEAD_KEY.into()));
4646
}
4747
Ok(())
4848
}

tolstoy/src/syncer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use metadata::HeadTrackable;
3131
use schema::ensure_current_version;
3232

3333
use errors::{
34-
ErrorKind,
34+
TolstoyError,
3535
Result,
3636
};
3737

@@ -179,7 +179,7 @@ impl Syncer {
179179
let mut uploader = UploadingTxReceiver::new(remote_client, remote_head);
180180
Processor::process(db_tx, from_tx, &mut uploader)?;
181181
if !uploader.is_done {
182-
bail!(ErrorKind::TxProcessorUnfinished);
182+
bail!(TolstoyError::TxProcessorUnfinished);
183183
}
184184
// Last tx uuid uploaded by the tx receiver.
185185
// It's going to be our new head.
@@ -222,7 +222,7 @@ impl Syncer {
222222
// without walking the table at all, and use the tx index.
223223
Processor::process(&db_tx, None, &mut inquiring_tx_receiver)?;
224224
if !inquiring_tx_receiver.is_done {
225-
bail!(ErrorKind::TxProcessorUnfinished);
225+
bail!(TolstoyError::TxProcessorUnfinished);
226226
}
227227
let have_local_changes = match inquiring_tx_receiver.last_tx {
228228
Some(tx) => {
@@ -257,15 +257,15 @@ impl Syncer {
257257
Syncer::upload_ours(&mut db_tx, Some(upload_from_tx), &remote_client, &remote_head)?;
258258
} else {
259259
d(&format!("Unable to fast-forward the server; missing local tx mapping"));
260-
bail!(ErrorKind::TxIncorrectlyMapped(0));
260+
bail!(TolstoyError::TxIncorrectlyMapped(0));
261261
}
262262

263263
// We diverged from the server.
264264
// We'll need to rebase/merge ourselves on top of it.
265265
} else {
266266
d(&format!("server changed since last sync."));
267267

268-
bail!(ErrorKind::NotYetImplemented(
268+
bail!(TolstoyError::NotYetImplemented(
269269
format!("Can't yet sync against changed server. Local head {:?}, remote head {:?}", locally_known_remote_head, remote_head)
270270
));
271271
}

tolstoy/src/tx_mapper.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use uuid::Uuid;
1414
use mentat_core::Entid;
1515

1616
use errors::{
17-
ErrorKind,
17+
TolstoyError,
1818
Result,
1919
};
2020

@@ -59,7 +59,7 @@ impl TxMapper {
5959
if txs.len() == 0 {
6060
return Ok(None);
6161
} else if txs.len() > 1 {
62-
bail!(ErrorKind::TxIncorrectlyMapped(txs.len()));
62+
bail!(TolstoyError::TxIncorrectlyMapped(txs.len()));
6363
}
6464
Ok(Some(txs.remove(0)?))
6565
}
@@ -79,7 +79,7 @@ impl TxMapper {
7979
if uuids.len() == 0 {
8080
return Ok(None);
8181
} else if uuids.len() > 1 {
82-
bail!(ErrorKind::TxIncorrectlyMapped(uuids.len()));
82+
bail!(TolstoyError::TxIncorrectlyMapped(uuids.len()));
8383
}
8484
Ok(Some(uuids.remove(0)?))
8585
}

0 commit comments

Comments
 (0)