diff --git a/src/entity_builder.rs b/src/entity_builder.rs index ffa1f56a..524b32b3 100644 --- a/src/entity_builder.rs +++ b/src/entity_builder.rs @@ -381,9 +381,7 @@ mod testing { extern crate mentat_db; // For matching inside a test. - use mentat_db::ErrorKind::{ - UnrecognizedEntid, - }; + use mentat_db::DbError; use ::{ Conn, @@ -424,7 +422,7 @@ mod testing { let mut in_progress = conn.begin_transaction(&mut sqlite).expect("begun successfully"); // This should fail: unrecognized entid. - if let Err(Error(MentatError::DbError(UnrecognizedEntid(e)), _)) = in_progress.transact_terms(terms, tempids) { + if let Ok(DbError::UnrecognizedEntid(e)) = in_progress.transact_terms(terms, tempids).expect_err("expected transact to fail").downcast() { assert_eq!(e, 999); } else { panic!("Should have rejected the entid."); diff --git a/tolstoy/Cargo.toml b/tolstoy/Cargo.toml index 119078e3..d864ef7d 100644 --- a/tolstoy/Cargo.toml +++ b/tolstoy/Cargo.toml @@ -5,6 +5,8 @@ workspace = ".." authors = ["Grisha Kruglov "] [dependencies] +failure = "0.1.1" +failure_derive = "0.1.1" futures = "0.1" hyper = "0.11" tokio-core = "0.1" @@ -15,8 +17,6 @@ serde_derive = "1.0" lazy_static = "0.2" uuid = { version = "0.5", features = ["v4", "serde"] } -error-chain = { git = "https://github.com/rnewman/error-chain", branch = "rnewman/sync" } - [dependencies.mentat_core] path = "../core" diff --git a/tolstoy/src/errors.rs b/tolstoy/src/errors.rs index de5dfdba..3b0a2e3c 100644 --- a/tolstoy/src/errors.rs +++ b/tolstoy/src/errors.rs @@ -10,63 +10,34 @@ #![allow(dead_code)] -use std; -use hyper; -use rusqlite; -use uuid; -use mentat_db; -use serde_cbor; -use serde_json; +use failure::Error; -error_chain! { - types { - Error, ErrorKind, ResultExt, Result; - } - - foreign_links { - IOError(std::io::Error); - HttpError(hyper::Error); - HyperUriError(hyper::error::UriError); - SqlError(rusqlite::Error); - UuidParseError(uuid::ParseError); - Utf8Error(std::str::Utf8Error); - JsonError(serde_json::Error); - CborError(serde_cbor::error::Error); - } - - links { - DbError(mentat_db::Error, mentat_db::ErrorKind); - } - - errors { - TxIncorrectlyMapped(n: usize) { - description("encountered more than one uuid mapping for tx") - display("expected one, found {} uuid mappings for tx", n) - } - - UnexpectedState(t: String) { - description("encountered unexpected state") - display("encountered unexpected state: {}", t) - } - - NotYetImplemented(t: String) { - description("not yet implemented") - display("not yet implemented: {}", t) - } - - DuplicateMetadata(k: String) { - description("encountered more than one metadata value for key") - display("encountered more than one metadata value for key: {}", k) - } - - TxProcessorUnfinished { - description("Tx processor couldn't finish") - display("Tx processor couldn't finish") - } - - BadServerResponse(s: String) { - description("Received bad response from the server") - display("Received bad response from the server: {}", s) - } - } +#[macro_export] +macro_rules! bail { + ($e:expr) => ( + return Err($e.into()); + ) +} + +pub type Result = ::std::result::Result; + +#[derive(Debug, Fail)] +pub enum TolstoyError { + #[fail(display = "Received bad response from the server: {}", _0)] + BadServerResponse(String), + + #[fail(display = "encountered more than one metadata value for key: {}", _0)] + DuplicateMetadata(String), + + #[fail(display = "transaction processor didn't say it was done")] + TxProcessorUnfinished, + + #[fail(display = "expected one, found {} uuid mappings for tx", _0)] + TxIncorrectlyMapped(usize), + + #[fail(display = "encountered unexpected state: {}", _0)] + UnexpectedState(String), + + #[fail(display = "not yet implemented: {}", _0)] + NotYetImplemented(String), } diff --git a/tolstoy/src/lib.rs b/tolstoy/src/lib.rs index 51306f2b..d4293c64 100644 --- a/tolstoy/src/lib.rs +++ b/tolstoy/src/lib.rs @@ -8,11 +8,9 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. -// For error_chain: -#![recursion_limit="128"] - +extern crate failure; #[macro_use] -extern crate error_chain; +extern crate failure_derive; #[macro_use] extern crate lazy_static; @@ -33,16 +31,15 @@ extern crate mentat_core; extern crate rusqlite; extern crate uuid; +#[macro_use] +pub mod errors; pub mod schema; pub mod metadata; pub mod tx_processor; -pub mod errors; pub mod syncer; pub mod tx_mapper; pub use syncer::Syncer; pub use errors::{ - Error, - ErrorKind, + TolstoyError, Result, - ResultExt, }; diff --git a/tolstoy/src/metadata.rs b/tolstoy/src/metadata.rs index b8196964..8c9b6579 100644 --- a/tolstoy/src/metadata.rs +++ b/tolstoy/src/metadata.rs @@ -15,7 +15,7 @@ use uuid::Uuid; use schema; use errors::{ - ErrorKind, + TolstoyError, Result, }; @@ -42,7 +42,7 @@ impl HeadTrackable for SyncMetadataClient { let updated = tx.execute("UPDATE tolstoy_metadata SET value = ? WHERE key = ?", &[&uuid_bytes, &schema::REMOTE_HEAD_KEY])?; if updated != 1 { - bail!(ErrorKind::DuplicateMetadata(schema::REMOTE_HEAD_KEY.into())); + bail!(TolstoyError::DuplicateMetadata(schema::REMOTE_HEAD_KEY.into())); } Ok(()) } diff --git a/tolstoy/src/syncer.rs b/tolstoy/src/syncer.rs index c4a77515..7abc296d 100644 --- a/tolstoy/src/syncer.rs +++ b/tolstoy/src/syncer.rs @@ -31,7 +31,7 @@ use metadata::HeadTrackable; use schema::ensure_current_version; use errors::{ - ErrorKind, + TolstoyError, Result, }; @@ -179,7 +179,7 @@ impl Syncer { let mut uploader = UploadingTxReceiver::new(remote_client, remote_head); Processor::process(db_tx, from_tx, &mut uploader)?; if !uploader.is_done { - bail!(ErrorKind::TxProcessorUnfinished); + bail!(TolstoyError::TxProcessorUnfinished); } // Last tx uuid uploaded by the tx receiver. // It's going to be our new head. @@ -222,7 +222,7 @@ impl Syncer { // without walking the table at all, and use the tx index. Processor::process(&db_tx, None, &mut inquiring_tx_receiver)?; if !inquiring_tx_receiver.is_done { - bail!(ErrorKind::TxProcessorUnfinished); + bail!(TolstoyError::TxProcessorUnfinished); } let have_local_changes = match inquiring_tx_receiver.last_tx { Some(tx) => { @@ -257,7 +257,7 @@ impl Syncer { Syncer::upload_ours(&mut db_tx, Some(upload_from_tx), &remote_client, &remote_head)?; } else { d(&format!("Unable to fast-forward the server; missing local tx mapping")); - bail!(ErrorKind::TxIncorrectlyMapped(0)); + bail!(TolstoyError::TxIncorrectlyMapped(0)); } // We diverged from the server. @@ -265,7 +265,7 @@ impl Syncer { } else { d(&format!("server changed since last sync.")); - bail!(ErrorKind::NotYetImplemented( + bail!(TolstoyError::NotYetImplemented( format!("Can't yet sync against changed server. Local head {:?}, remote head {:?}", locally_known_remote_head, remote_head) )); } diff --git a/tolstoy/src/tx_mapper.rs b/tolstoy/src/tx_mapper.rs index a5a84405..1ad8521b 100644 --- a/tolstoy/src/tx_mapper.rs +++ b/tolstoy/src/tx_mapper.rs @@ -14,7 +14,7 @@ use uuid::Uuid; use mentat_core::Entid; use errors::{ - ErrorKind, + TolstoyError, Result, }; @@ -59,7 +59,7 @@ impl TxMapper { if txs.len() == 0 { return Ok(None); } else if txs.len() > 1 { - bail!(ErrorKind::TxIncorrectlyMapped(txs.len())); + bail!(TolstoyError::TxIncorrectlyMapped(txs.len())); } Ok(Some(txs.remove(0)?)) } @@ -79,7 +79,7 @@ impl TxMapper { if uuids.len() == 0 { return Ok(None); } else if uuids.len() > 1 { - bail!(ErrorKind::TxIncorrectlyMapped(uuids.len())); + bail!(TolstoyError::TxIncorrectlyMapped(uuids.len())); } Ok(Some(uuids.remove(0)?)) }