Improve error logging for field and index errors

Closes #113
This commit is contained in:
Tobie Morgan Hitchcock 2022-09-10 05:59:08 +01:00
parent 3b41217423
commit fde0c55d34
3 changed files with 9 additions and 2 deletions

View file

@ -19,6 +19,8 @@ impl<'a> Document<'a> {
if !opt.fields { if !opt.fields {
return Ok(()); return Ok(());
} }
// Get the record id
let rid = self.id.as_ref().unwrap();
// Loop through all field statements // Loop through all field statements
for fd in self.fd(opt, txn).await?.iter() { for fd in self.fd(opt, txn).await?.iter() {
// Loop over each field in document // Loop over each field in document
@ -53,6 +55,7 @@ impl<'a> Document<'a> {
// Process the ASSERT clause // Process the ASSERT clause
if !expr.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { if !expr.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
return Err(Error::FieldValue { return Err(Error::FieldValue {
thing: rid.to_string(),
value: val.to_string(), value: val.to_string(),
field: fd.name.clone(), field: fd.name.clone(),
check: expr.to_string(), check: expr.to_string(),

View file

@ -62,6 +62,7 @@ impl<'a> Document<'a> {
let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, &n, None); let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, &n, None);
if run.putc(key, rid, None).await.is_err() { if run.putc(key, rid, None).await.is_err() {
return Err(Error::IndexExists { return Err(Error::IndexExists {
thing: rid.to_string(),
index: ix.name.to_string(), index: ix.name.to_string(),
value: match n.len() { value: match n.len() {
1 => n.first().unwrap().to_string(), 1 => n.first().unwrap().to_string(),
@ -84,6 +85,7 @@ impl<'a> Document<'a> {
let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, &n, Some(&rid.id)); let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, &n, Some(&rid.id));
if run.putc(key, rid, None).await.is_err() { if run.putc(key, rid, None).await.is_err() {
return Err(Error::IndexExists { return Err(Error::IndexExists {
thing: rid.to_string(),
index: ix.name.to_string(), index: ix.name.to_string(),
value: match n.len() { value: match n.len() {
1 => n.first().unwrap().to_string(), 1 => n.first().unwrap().to_string(),

View file

@ -217,15 +217,17 @@ pub enum Error {
}, },
/// A database index entry for the specified record already exists /// A database index entry for the specified record already exists
#[error("Database index `{index}` already contains {value}")] #[error("Database index `{index}` already contains {value}, on record `{thing}`")]
IndexExists { IndexExists {
thing: String,
index: String, index: String,
value: String, value: String,
}, },
/// The specified field did not conform to the field ASSERT clause /// The specified field did not conform to the field ASSERT clause
#[error("Found {value} for field `{field}` but field must conform to: {check}")] #[error("Found {value} for field `{field}`, on record `{thing}`, but field must conform to: {check}")]
FieldValue { FieldValue {
thing: String,
value: String, value: String,
field: Idiom, field: Idiom,
check: String, check: String,