Convert tolstoy/ to failure.

This commit is contained in:
Grisha Kruglov 2018-06-07 14:28:46 -04:00 committed by Nick Alexander
parent 31de5be64f
commit 4e46adeba1
7 changed files with 48 additions and 82 deletions

View file

@ -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.");

View file

@ -5,6 +5,8 @@ workspace = ".."
authors = ["Grisha Kruglov <gkruglov@mozilla.com>"]
[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"

View file

@ -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<T> = ::std::result::Result<T, Error>;
#[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),
}

View file

@ -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,
};

View file

@ -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(())
}

View file

@ -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)
));
}

View file

@ -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)?))
}