Skip to content

Commit 24b2e3a

Browse files
committed
wrap enrichment errors in a custom type
1 parent 0091dad commit 24b2e3a

File tree

9 files changed

+144
-86
lines changed

9 files changed

+144
-86
lines changed

lib/enrichment/src/get_enrichment_table_record.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ fn get_enrichment_table_record(
2929
})
3030
.transpose()?;
3131

32-
let data = enrichment_tables.find_table_row(
33-
table,
34-
case_sensitive,
35-
condition,
36-
select.as_ref().map(|select| select.as_ref()),
37-
wildcard.as_ref(),
38-
index,
39-
)?;
32+
let data = enrichment_tables
33+
.find_table_row(
34+
table,
35+
case_sensitive,
36+
condition,
37+
select.as_ref().map(|select| select.as_ref()),
38+
wildcard.as_ref(),
39+
index,
40+
)
41+
.map_err(Into::<ExpressionError>::into)?;
4042

4143
Ok(Value::Object(data))
4244
}

lib/enrichment/src/lib.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,42 @@ pub enum Case {
4646
Insensitive,
4747
}
4848

49+
#[derive(Clone, Debug, PartialEq, Eq)]
50+
pub enum Error {
51+
NoRowsFound(String),
52+
MoreThanOneRowFound(String),
53+
InvalidInput(String),
54+
TableError(String),
55+
}
56+
57+
impl Error {
58+
pub fn message(&self) -> String {
59+
match self {
60+
Error::NoRowsFound(message) => message.clone(),
61+
Error::MoreThanOneRowFound(message) => message.clone(),
62+
Error::InvalidInput(message) => message.clone(),
63+
Error::TableError(message) => message.clone(),
64+
}
65+
}
66+
}
67+
impl std::fmt::Display for Error {
68+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69+
write!(f, "{}", self.message())
70+
}
71+
}
72+
73+
impl std::error::Error for Error {}
74+
75+
impl From<Error> for vrl::prelude::ExpressionError {
76+
fn from(error: Error) -> Self {
77+
vrl::prelude::ExpressionError::Error {
78+
message: error.message(),
79+
labels: vec![],
80+
notes: vec![],
81+
}
82+
}
83+
}
84+
4985
/// Enrichment tables represent additional data sources that can be used to enrich the event data
5086
/// passing through Vector.
5187
pub trait Table: DynClone {
@@ -61,7 +97,7 @@ pub trait Table: DynClone {
6197
select: Option<&[String]>,
6298
wildcard: Option<&Value>,
6399
index: Option<IndexHandle>,
64-
) -> Result<ObjectMap, String>;
100+
) -> Result<ObjectMap, Error>;
65101

66102
/// Search the enrichment table data with the given condition.
67103
/// All conditions must match (AND).
@@ -73,14 +109,14 @@ pub trait Table: DynClone {
73109
select: Option<&[String]>,
74110
wildcard: Option<&Value>,
75111
index: Option<IndexHandle>,
76-
) -> Result<Vec<ObjectMap>, String>;
112+
) -> Result<Vec<ObjectMap>, Error>;
77113

78114
/// Hints to the enrichment table what data is going to be searched to allow it to index the
79115
/// data in advance.
80116
///
81117
/// # Errors
82118
/// Errors if the fields are not in the table.
83-
fn add_index(&mut self, case: Case, fields: &[&str]) -> Result<IndexHandle, String>;
119+
fn add_index(&mut self, case: Case, fields: &[&str]) -> Result<IndexHandle, Error>;
84120

85121
/// Returns a list of the field names that are in each index
86122
fn index_fields(&self) -> Vec<(Case, Vec<String>)>;

lib/enrichment/src/tables.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::{
3737
use arc_swap::ArcSwap;
3838
use vrl::value::{ObjectMap, Value};
3939

40-
use super::{Condition, IndexHandle, Table};
40+
use super::{Condition, Error, IndexHandle, Table};
4141
use crate::Case;
4242

4343
/// A hashmap of name => implementation of an enrichment table.
@@ -151,13 +151,13 @@ impl TableRegistry {
151151
table: &str,
152152
case: Case,
153153
fields: &[&str],
154-
) -> Result<IndexHandle, String> {
154+
) -> Result<IndexHandle, Error> {
155155
let mut locked = self.loading.lock().unwrap();
156156

157157
match *locked {
158-
None => Err("finish_load has been called".to_string()),
158+
None => Err(Error::TableError("finish_load has been called".into())),
159159
Some(ref mut tables) => match tables.get_mut(table) {
160-
None => Err(format!("table '{table}' not loaded")),
160+
None => Err(Error::TableError(format!("table '{table}' not loaded"))),
161161
Some(table) => table.add_index(case, fields),
162162
},
163163
}
@@ -218,15 +218,15 @@ impl TableSearch {
218218
select: Option<&[String]>,
219219
wildcard: Option<&Value>,
220220
index: Option<IndexHandle>,
221-
) -> Result<ObjectMap, String> {
221+
) -> Result<ObjectMap, Error> {
222222
let tables = self.0.load();
223223
if let Some(ref tables) = **tables {
224224
match tables.get(table) {
225-
None => Err(format!("table {table} not loaded")),
225+
None => Err(Error::TableError(format!("table {table} not loaded"))),
226226
Some(table) => table.find_table_row(case, condition, select, wildcard, index),
227227
}
228228
} else {
229-
Err("finish_load not called".to_string())
229+
Err(Error::TableError("finish_load not called".into()))
230230
}
231231
}
232232

@@ -241,15 +241,15 @@ impl TableSearch {
241241
select: Option<&[String]>,
242242
wildcard: Option<&Value>,
243243
index: Option<IndexHandle>,
244-
) -> Result<Vec<ObjectMap>, String> {
244+
) -> Result<Vec<ObjectMap>, Error> {
245245
let tables = self.0.load();
246246
if let Some(ref tables) = **tables {
247247
match tables.get(table) {
248-
None => Err(format!("table {table} not loaded")),
248+
None => Err(Error::TableError(format!("table {table} not loaded"))),
249249
Some(table) => table.find_table_rows(case, condition, select, wildcard, index),
250250
}
251251
} else {
252-
Err("finish_load not called".to_string())
252+
Err(Error::TableError("finish_load not called".into()))
253253
}
254254
}
255255
}
@@ -331,7 +331,7 @@ mod tests {
331331
let tables = registry.as_readonly();
332332

333333
assert_eq!(
334-
Err("finish_load not called".to_string()),
334+
Err(Error::TableError("finish_load not called".to_string())),
335335
tables.find_table_row(
336336
"dummy1",
337337
Case::Sensitive,
@@ -355,7 +355,7 @@ mod tests {
355355
registry.load(tables);
356356
registry.finish_load();
357357
assert_eq!(
358-
Err("finish_load has been called".to_string()),
358+
Err(Error::TableError("finish_load has been called".into())),
359359
registry.add_index("dummy1", Case::Sensitive, &["erk"])
360360
);
361361
}

lib/enrichment/src/test_util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55

66
use vrl::value::{ObjectMap, Value};
77

8-
use crate::{Case, Condition, IndexHandle, Table, TableRegistry};
8+
use crate::{Case, Condition, Error, IndexHandle, Table, TableRegistry};
99

1010
#[derive(Debug, Clone)]
1111
pub(crate) struct DummyEnrichmentTable {
@@ -41,7 +41,7 @@ impl Table for DummyEnrichmentTable {
4141
_select: Option<&[String]>,
4242
_wildcard: Option<&Value>,
4343
_index: Option<IndexHandle>,
44-
) -> Result<ObjectMap, String> {
44+
) -> Result<ObjectMap, Error> {
4545
Ok(self.data.clone())
4646
}
4747

@@ -52,11 +52,11 @@ impl Table for DummyEnrichmentTable {
5252
_select: Option<&[String]>,
5353
_wildcard: Option<&Value>,
5454
_index: Option<IndexHandle>,
55-
) -> Result<Vec<ObjectMap>, String> {
55+
) -> Result<Vec<ObjectMap>, Error> {
5656
Ok(vec![self.data.clone()])
5757
}
5858

59-
fn add_index(&mut self, _case: Case, fields: &[&str]) -> Result<IndexHandle, String> {
59+
fn add_index(&mut self, _case: Case, fields: &[&str]) -> Result<IndexHandle, Error> {
6060
let mut indexes = self.indexes.lock().unwrap();
6161
indexes.push(fields.iter().map(|s| (*s).to_string()).collect());
6262
Ok(IndexHandle(indexes.len() - 1))

lib/vector-vrl/tests/src/test_enrichment.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl enrichment::Table for TestEnrichmentTable {
1313
_select: Option<&[String]>,
1414
_wildcard: Option<&Value>,
1515
_index: Option<enrichment::IndexHandle>,
16-
) -> Result<ObjectMap, String> {
16+
) -> Result<ObjectMap, enrichment::Error> {
1717
let mut result = ObjectMap::new();
1818
result.insert("id".into(), Value::from(1));
1919
result.insert("firstname".into(), Value::from("Bob"));
@@ -29,7 +29,7 @@ impl enrichment::Table for TestEnrichmentTable {
2929
_select: Option<&[String]>,
3030
_wildcard: Option<&Value>,
3131
_index: Option<enrichment::IndexHandle>,
32-
) -> Result<Vec<ObjectMap>, String> {
32+
) -> Result<Vec<ObjectMap>, enrichment::Error> {
3333
let mut result1 = ObjectMap::new();
3434
result1.insert("id".into(), Value::from(1));
3535
result1.insert("firstname".into(), Value::from("Bob"));
@@ -47,7 +47,7 @@ impl enrichment::Table for TestEnrichmentTable {
4747
&mut self,
4848
_case: enrichment::Case,
4949
_fields: &[&str],
50-
) -> Result<enrichment::IndexHandle, String> {
50+
) -> Result<enrichment::IndexHandle, enrichment::Error> {
5151
Ok(enrichment::IndexHandle(1))
5252
}
5353

0 commit comments

Comments
 (0)