From fde0c55d34828dff461b87d22170b88c3487e8eb Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 10 Sep 2022 05:59:08 +0100 Subject: [PATCH] Improve error logging for field and index errors Closes #113 --- lib/src/doc/field.rs | 3 +++ lib/src/doc/index.rs | 2 ++ lib/src/err/mod.rs | 6 ++++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/src/doc/field.rs b/lib/src/doc/field.rs index f5a18c92..5309a85a 100644 --- a/lib/src/doc/field.rs +++ b/lib/src/doc/field.rs @@ -19,6 +19,8 @@ impl<'a> Document<'a> { if !opt.fields { return Ok(()); } + // Get the record id + let rid = self.id.as_ref().unwrap(); // Loop through all field statements for fd in self.fd(opt, txn).await?.iter() { // Loop over each field in document @@ -53,6 +55,7 @@ impl<'a> Document<'a> { // Process the ASSERT clause if !expr.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { return Err(Error::FieldValue { + thing: rid.to_string(), value: val.to_string(), field: fd.name.clone(), check: expr.to_string(), diff --git a/lib/src/doc/index.rs b/lib/src/doc/index.rs index 7ff5e926..927d022e 100644 --- a/lib/src/doc/index.rs +++ b/lib/src/doc/index.rs @@ -62,6 +62,7 @@ impl<'a> Document<'a> { 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() { return Err(Error::IndexExists { + thing: rid.to_string(), index: ix.name.to_string(), value: match n.len() { 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)); if run.putc(key, rid, None).await.is_err() { return Err(Error::IndexExists { + thing: rid.to_string(), index: ix.name.to_string(), value: match n.len() { 1 => n.first().unwrap().to_string(), diff --git a/lib/src/err/mod.rs b/lib/src/err/mod.rs index 4537ff8d..16c43d90 100644 --- a/lib/src/err/mod.rs +++ b/lib/src/err/mod.rs @@ -217,15 +217,17 @@ pub enum Error { }, /// 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 { + thing: String, index: String, value: String, }, /// 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 { + thing: String, value: String, field: Idiom, check: String,