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

Commit

Permalink
Use concrete Mentat error types rather than failure::Error. (#769) …
Browse files Browse the repository at this point in the history
…r=grisha

In the language of
https://github.com/withoutboats/failure/blob/868273409c826755812dbff1b67cc0ac3fa702f7/book/src/error-errorkind.md
Mentat is a mid-level API, not an application, and therefore we should
prefer our own error types.  Writing an initial consumer of Mentat (a
Rust logins API targeting Mozilla Lockbox), I have found this to be
true: my consumer wants to consume concrete Mentat error types.

This doesn't go "all the way" and convert all sub-crates to the
Error/ErrorKind, but it does go part of the way.
  • Loading branch information
ncalexan committed Jun 27, 2018
2 parents 72a9b30 + ae42784 commit 5fe4f12
Show file tree
Hide file tree
Showing 35 changed files with 447 additions and 405 deletions.
6 changes: 2 additions & 4 deletions core/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

/// Cache traits.

use failure;

use std::collections::{
BTreeSet,
};
Expand All @@ -35,7 +33,7 @@ pub trait CachedAttributes {
fn get_entids_for_value(&self, attribute: Entid, value: &TypedValue) -> Option<&BTreeSet<Entid>>;
}

pub trait UpdateableCache<Error=failure::Error> {
fn update<I>(&mut self, schema: &Schema, retractions: I, assertions: I) -> Result<(), Error>
pub trait UpdateableCache<E> {
fn update<I>(&mut self, schema: &Schema, retractions: I, assertions: I) -> Result<(), E>
where I: Iterator<Item=(Entid, Entid, TypedValue)>;
}
20 changes: 10 additions & 10 deletions db/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use edn;
use errors::{
DbError,
DbErrorKind,
Result,
};
use edn::types::Value;
Expand Down Expand Up @@ -159,7 +159,7 @@ lazy_static! {
:db/cardinality :db.cardinality/many}}"#;
edn::parse::value(s)
.map(|v| v.without_spans())
.map_err(|_| DbError::BadBootstrapDefinition("Unable to parse V1_SYMBOLIC_SCHEMA".into()))
.map_err(|_| DbErrorKind::BadBootstrapDefinition("Unable to parse V1_SYMBOLIC_SCHEMA".into()))
.unwrap()
};
}
Expand Down Expand Up @@ -210,14 +210,14 @@ fn symbolic_schema_to_triples(ident_map: &IdentMap, symbolic_schema: &Value) ->
for (ident, mp) in m {
let ident = match ident {
&Value::Keyword(ref ident) => ident,
_ => bail!(DbError::BadBootstrapDefinition(format!("Expected namespaced keyword for ident but got '{:?}'", ident))),
_ => bail!(DbErrorKind::BadBootstrapDefinition(format!("Expected namespaced keyword for ident but got '{:?}'", ident))),
};
match *mp {
Value::Map(ref mpp) => {
for (attr, value) in mpp {
let attr = match attr {
&Value::Keyword(ref attr) => attr,
_ => bail!(DbError::BadBootstrapDefinition(format!("Expected namespaced keyword for attr but got '{:?}'", attr))),
_ => bail!(DbErrorKind::BadBootstrapDefinition(format!("Expected namespaced keyword for attr but got '{:?}'", attr))),
};

// We have symbolic idents but the transactor handles entids. Ad-hoc
Expand All @@ -232,20 +232,20 @@ fn symbolic_schema_to_triples(ident_map: &IdentMap, symbolic_schema: &Value) ->
Some(TypedValue::Keyword(ref k)) => {
ident_map.get(k)
.map(|entid| TypedValue::Ref(*entid))
.ok_or(DbError::UnrecognizedIdent(k.to_string()))?
.ok_or(DbErrorKind::UnrecognizedIdent(k.to_string()))?
},
Some(v) => v,
_ => bail!(DbError::BadBootstrapDefinition(format!("Expected Mentat typed value for value but got '{:?}'", value)))
_ => bail!(DbErrorKind::BadBootstrapDefinition(format!("Expected Mentat typed value for value but got '{:?}'", value)))
};

triples.push((ident.clone(), attr.clone(), typed_value));
}
},
_ => bail!(DbError::BadBootstrapDefinition("Expected {:db/ident {:db/attr value ...} ...}".into()))
_ => bail!(DbErrorKind::BadBootstrapDefinition("Expected {:db/ident {:db/attr value ...} ...}".into()))
}
}
},
_ => bail!(DbError::BadBootstrapDefinition("Expected {...}".into()))
_ => bail!(DbErrorKind::BadBootstrapDefinition("Expected {...}".into()))
}
Ok(triples)
}
Expand All @@ -266,11 +266,11 @@ fn symbolic_schema_to_assertions(symbolic_schema: &Value) -> Result<Vec<Value>>
value.clone()]));
}
},
_ => bail!(DbError::BadBootstrapDefinition("Expected {:db/ident {:db/attr value ...} ...}".into()))
_ => bail!(DbErrorKind::BadBootstrapDefinition("Expected {:db/ident {:db/attr value ...} ...}".into()))
}
}
},
_ => bail!(DbError::BadBootstrapDefinition("Expected {...}".into()))
_ => bail!(DbErrorKind::BadBootstrapDefinition("Expected {...}".into()))
}
Ok(assertions)
}
Expand Down
23 changes: 14 additions & 9 deletions db/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ use std::sync::Arc;

use std::iter::Peekable;

use failure::{
ResultExt,
};

use num;

use rusqlite;
Expand Down Expand Up @@ -107,6 +111,7 @@ use db::{

use errors::{
DbError,
DbErrorKind,
Result,
};

Expand Down Expand Up @@ -895,7 +900,7 @@ impl AttributeCaches {
let table = if is_fulltext { "fulltext_datoms" } else { "datoms" };
let sql = format!("SELECT a, e, v, value_type_tag FROM {} WHERE a = ? ORDER BY a ASC, e ASC", table);
let args: Vec<&rusqlite::types::ToSql> = vec![&attribute];
let mut stmt = sqlite.prepare(&sql)?;
let mut stmt = sqlite.prepare(&sql).context(DbErrorKind::CacheUpdateFailed)?;
let replacing = true;
self.repopulate_from_aevt(schema, &mut stmt, args, replacing)
}
Expand Down Expand Up @@ -1154,7 +1159,7 @@ impl CachedAttributes for AttributeCaches {
}
}

impl UpdateableCache for AttributeCaches {
impl UpdateableCache<DbError> for AttributeCaches {
fn update<I>(&mut self, schema: &Schema, retractions: I, assertions: I) -> Result<()>
where I: Iterator<Item=(Entid, Entid, TypedValue)> {
self.update_with_fallback(None, schema, retractions, assertions)
Expand Down Expand Up @@ -1236,7 +1241,7 @@ impl SQLiteAttributeCache {
let a = attribute.into();

// The attribute must exist!
let _ = schema.attribute_for_entid(a).ok_or_else(|| DbError::UnknownAttribute(a))?;
let _ = schema.attribute_for_entid(a).ok_or_else(|| DbErrorKind::UnknownAttribute(a))?;
let caches = self.make_mut();
caches.forward_cached_attributes.insert(a);
caches.repopulate(schema, sqlite, a)
Expand All @@ -1247,7 +1252,7 @@ impl SQLiteAttributeCache {
let a = attribute.into();

// The attribute must exist!
let _ = schema.attribute_for_entid(a).ok_or_else(|| DbError::UnknownAttribute(a))?;
let _ = schema.attribute_for_entid(a).ok_or_else(|| DbErrorKind::UnknownAttribute(a))?;

let caches = self.make_mut();
caches.reverse_cached_attributes.insert(a);
Expand Down Expand Up @@ -1276,7 +1281,7 @@ impl SQLiteAttributeCache {
}
}

impl UpdateableCache for SQLiteAttributeCache {
impl UpdateableCache<DbError> for SQLiteAttributeCache {
fn update<I>(&mut self, schema: &Schema, retractions: I, assertions: I) -> Result<()>
where I: Iterator<Item=(Entid, Entid, TypedValue)> {
self.make_mut().update(schema, retractions, assertions)
Expand Down Expand Up @@ -1354,7 +1359,7 @@ impl InProgressSQLiteAttributeCache {
let a = attribute.into();

// The attribute must exist!
let _ = schema.attribute_for_entid(a).ok_or_else(|| DbError::UnknownAttribute(a))?;
let _ = schema.attribute_for_entid(a).ok_or_else(|| DbErrorKind::UnknownAttribute(a))?;

if self.is_attribute_cached_forward(a) {
return Ok(());
Expand All @@ -1370,7 +1375,7 @@ impl InProgressSQLiteAttributeCache {
let a = attribute.into();

// The attribute must exist!
let _ = schema.attribute_for_entid(a).ok_or_else(|| DbError::UnknownAttribute(a))?;
let _ = schema.attribute_for_entid(a).ok_or_else(|| DbErrorKind::UnknownAttribute(a))?;

if self.is_attribute_cached_reverse(a) {
return Ok(());
Expand All @@ -1386,7 +1391,7 @@ impl InProgressSQLiteAttributeCache {
let a = attribute.into();

// The attribute must exist!
let _ = schema.attribute_for_entid(a).ok_or_else(|| DbError::UnknownAttribute(a))?;
let _ = schema.attribute_for_entid(a).ok_or_else(|| DbErrorKind::UnknownAttribute(a))?;

// TODO: reverse-index unique by default?
let reverse_done = self.is_attribute_cached_reverse(a);
Expand Down Expand Up @@ -1424,7 +1429,7 @@ impl InProgressSQLiteAttributeCache {
}
}

impl UpdateableCache for InProgressSQLiteAttributeCache {
impl UpdateableCache<DbError> for InProgressSQLiteAttributeCache {
fn update<I>(&mut self, schema: &Schema, retractions: I, assertions: I) -> Result<()>
where I: Iterator<Item=(Entid, Entid, TypedValue)> {
self.overlay.update_with_fallback(Some(&self.inner), schema, retractions, assertions)
Expand Down
Loading

0 comments on commit 5fe4f12

Please sign in to comment.