Skip to content

Commit 0cb2900

Browse files
committed
improve all error message
1 parent 2ff17ee commit 0cb2900

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

src/error.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::io;
22

3-
use crate::key::Key;
3+
use crate::{key::Key, node_id::NodeMode, ItemId};
44

55
/// The different set of errors that arroy can encounter.
66
#[derive(Debug, thiserror::Error)]
@@ -52,6 +52,27 @@ pub enum Error {
5252
NeedBuild(u16),
5353

5454
/// Internal error
55-
#[error("Internal error: {:?} is missing in index `{}`", .0.node, .0.index)]
56-
MissingKey(Key),
55+
#[error("Internal error: {mode}({item}) is missing in index `{index}`")]
56+
MissingKey {
57+
/// The index that caused the error
58+
index: u16,
59+
/// The kind of item that was being queried
60+
mode: &'static str,
61+
/// The item ID queried
62+
item: ItemId,
63+
},
64+
}
65+
66+
impl Error {
67+
pub(crate) fn missing_key(key: Key) -> Self {
68+
Self::MissingKey {
69+
index: key.index,
70+
mode: match key.node.mode {
71+
NodeMode::Item => "Item",
72+
NodeMode::Tree => "Tree",
73+
NodeMode::Metadata => "Metadata",
74+
},
75+
item: key.node.item,
76+
}
77+
}
5778
}

src/reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<'t, D: Distance> Reader<'t, D> {
228228
};
229229

230230
let key = Key::new(self.index, item);
231-
match self.database.get(rtxn, &key)?.ok_or(Error::MissingKey(key))? {
231+
match self.database.get(rtxn, &key)?.ok_or(Error::missing_key(key))? {
232232
Node::Leaf(_) => {
233233
if candidates.map_or(true, |c| c.contains(item.item)) {
234234
nns.push(item.unwrap_item());
@@ -257,7 +257,7 @@ impl<'t, D: Distance> Reader<'t, D> {
257257
let mut nns_distances = Vec::with_capacity(nns.len());
258258
for nn in nns {
259259
let key = Key::item(self.index, nn);
260-
let leaf = match self.database.get(rtxn, &key)?.ok_or(Error::MissingKey(key))? {
260+
let leaf = match self.database.get(rtxn, &key)?.ok_or(Error::missing_key(key))? {
261261
Node::Leaf(leaf) => leaf,
262262
Node::Descendants(_) | Node::SplitPlaneNormal(_) => unreachable!(),
263263
};

src/tests/reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn open_unfinished_db() {
3434

3535
let rtxn = handle.env.read_txn().unwrap();
3636
let ret = Reader::<Euclidean>::open(&rtxn, 0, handle.database).map(|_| ()).unwrap_err();
37-
insta::assert_display_snapshot!(ret, @"Metadata are missing on index 0, did you build your database before trying to read it.");
37+
insta::assert_display_snapshot!(ret, @"Metadata are missing on index 0, did you build your database before trying to read it");
3838
}
3939

4040
#[test]
@@ -50,7 +50,7 @@ fn open_db_with_wrong_dimension() {
5050
let rtxn = handle.env.read_txn().unwrap();
5151
let reader = Reader::<Euclidean>::open(&rtxn, 0, handle.database).unwrap();
5252
let ret = reader.nns_by_vector(&rtxn, &[1.0, 2.0, 3.0], 5, None, None).unwrap_err();
53-
insta::assert_display_snapshot!(ret, @"Invalid vector dimensions. Got 3 but expected 2.");
53+
insta::assert_display_snapshot!(ret, @"Invalid vector dimensions. Got 3 but expected 2");
5454
}
5555

5656
#[test]

src/writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ impl<D: Distance> Writer<D> {
775775

776776
fn delete_tree(&self, wtxn: &mut RwTxn, node: NodeId) -> Result<()> {
777777
let key = Key::new(self.index, node);
778-
match self.database.get(wtxn, &key)?.ok_or(Error::MissingKey(key))? {
778+
match self.database.get(wtxn, &key)?.ok_or(Error::missing_key(key))? {
779779
// the leafs are shared between the trees, we MUST NOT delete them.
780780
Node::Leaf(_) => Ok(()),
781781
Node::Descendants(_) => {

0 commit comments

Comments
 (0)