Skip to content

Commit

Permalink
fix(plugin): lazily create error objects
Browse files Browse the repository at this point in the history
Signed-off-by: Grzegorz Nosek <[email protected]>
  • Loading branch information
gnosek authored and poiana committed Oct 18, 2024
1 parent a61d6b9 commit e295881
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 24 deletions.
4 changes: 2 additions & 2 deletions falco_plugin/src/plugin/base/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub unsafe extern "C" fn plugin_init<P: Plugin>(
rc: *mut ss_plugin_rc,
) -> *mut falco_plugin_api::ss_plugin_t {
let res = (|| -> Result<*mut PluginWrapper<P>, anyhow::Error> {
let init_input =
unsafe { init_input.as_ref() }.ok_or(anyhow::anyhow!("Got empty init_input"))?;
let init_input = unsafe { init_input.as_ref() }
.ok_or_else(|| anyhow::anyhow!("Got empty init_input"))?;

let init_config =
try_str_from_ptr(&init_input.config).context("Failed to get config string")?;
Expand Down
7 changes: 3 additions & 4 deletions falco_plugin/src/plugin/exported_tables/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,9 @@ where
let (type_id, index) = { (field.type_id, field.index) };

let value = unsafe {
DynamicFieldValue::from_data(value, type_id).ok_or(anyhow::anyhow!(
"Cannot store {:?} data (unsupported type)",
type_id
))?
DynamicFieldValue::from_data(value, type_id).ok_or_else(|| {
anyhow::anyhow!("Cannot store {:?} data (unsupported type)", type_id)
})?
};

entry.set(index, value)
Expand Down
6 changes: 3 additions & 3 deletions falco_plugin/src/plugin/listen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl CaptureListenInput {
let input = unsafe {
value
.as_ref()
.ok_or(anyhow::anyhow!("Got null event parse input"))?
.ok_or_else(|| anyhow::anyhow!("Got null event parse input"))?
};

let thread_pool = ThreadPool::try_from(input.owner, input.routine, last_error.clone())?;
Expand All @@ -53,13 +53,13 @@ impl CaptureListenInput {
input
.table_reader_ext
.as_ref()
.ok_or(anyhow::anyhow!("Got null reader vtable"))?
.ok_or_else(|| anyhow::anyhow!("Got null reader vtable"))?
};
let writer = unsafe {
input
.table_writer_ext
.as_ref()
.ok_or(anyhow::anyhow!("Got null writer vtable"))?
.ok_or_else(|| anyhow::anyhow!("Got null writer vtable"))?
};

let reader = TableReader::try_from(reader, last_error.clone())?;
Expand Down
6 changes: 3 additions & 3 deletions falco_plugin/src/plugin/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ impl ParseInput {
let input = unsafe {
value
.as_ref()
.ok_or(anyhow::anyhow!("Got null event parse input"))?
.ok_or_else(|| anyhow::anyhow!("Got null event parse input"))?
};

let reader = unsafe {
input
.table_reader_ext
.as_ref()
.ok_or(anyhow::anyhow!("Got null reader vtable"))?
.ok_or_else(|| anyhow::anyhow!("Got null reader vtable"))?
};
let writer = unsafe {
input
.table_writer_ext
.as_ref()
.ok_or(anyhow::anyhow!("Got null writer vtable"))?
.ok_or_else(|| anyhow::anyhow!("Got null writer vtable"))?
};

let reader = TableReader::try_from(reader, last_error.clone())?;
Expand Down
2 changes: 1 addition & 1 deletion falco_plugin/src/plugin/tables/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<M> Entry<M> {
unsafe {
self.raw_entry
.read_field_with_assoc::<V>(reader, field.field.field, &field.field.assoc_data)
.ok_or(anyhow::anyhow!("Could not read field value"))
.ok_or_else(|| anyhow::anyhow!("Could not read field value"))
.with_last_error(&reader.last_error)
}
}
Expand Down
4 changes: 2 additions & 2 deletions falco_plugin/src/plugin/tables/table/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl RawTable {
);
field
.as_mut()
.ok_or(anyhow::anyhow!("Failed to get table field {:?}", name))
.ok_or_else(|| anyhow::anyhow!("Failed to get table field {:?}", name))
.with_last_error(&tables_input.last_error)?;
field
};
Expand Down Expand Up @@ -93,7 +93,7 @@ impl RawTable {
);
field
.as_mut()
.ok_or(anyhow::anyhow!("Failed to add table field {:?}", name))
.ok_or_else(|| anyhow::anyhow!("Failed to add table field {:?}", name))
.with_last_error(&tables_input.last_error)?;
field
};
Expand Down
10 changes: 5 additions & 5 deletions falco_plugin_tests/tests/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl DummyPlugin {
let payload = event
.params
.event_data
.ok_or(anyhow::anyhow!("no payload in event"))?;
.ok_or_else(|| anyhow::anyhow!("no payload in event"))?;

let mut out = CString::default();
out.write_into(|w| w.write_all(payload))?;
Expand All @@ -123,7 +123,7 @@ impl DummyPlugin {
let payload = event
.params
.event_data
.ok_or(anyhow::anyhow!("no payload in event"))?;
.ok_or_else(|| anyhow::anyhow!("no payload in event"))?;

let mut out = CString::default();
out.write_into(|w| w.write_all(payload))?;
Expand All @@ -140,7 +140,7 @@ impl DummyPlugin {
let payload = event
.params
.event_data
.ok_or(anyhow::anyhow!("no payload in event"))?;
.ok_or_else(|| anyhow::anyhow!("no payload in event"))?;

let first_char = &payload[0..1];
let first_char = std::str::from_utf8(first_char)?;
Expand All @@ -162,7 +162,7 @@ impl DummyPlugin {
let payload = event
.params
.event_data
.ok_or(anyhow::anyhow!("no payload in event"))?;
.ok_or_else(|| anyhow::anyhow!("no payload in event"))?;

let first_char = &payload[0..1];
let first_char = std::str::from_utf8(first_char)?;
Expand All @@ -184,7 +184,7 @@ impl DummyPlugin {
ExtractFieldRequestArg::None => event
.params
.event_data
.ok_or(anyhow::anyhow!("no payload in event"))?,
.ok_or_else(|| anyhow::anyhow!("no payload in event"))?,
};

let first_char = &buf[0..1];
Expand Down
2 changes: 1 addition & 1 deletion falco_plugin_tests/tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl ParsePlugin for DummyPlugin {
let payload = event
.params
.event_data
.ok_or(anyhow::anyhow!("no payload in event"))?;
.ok_or_else(|| anyhow::anyhow!("no payload in event"))?;

let first_char = &payload[0..1];
let first_char = std::str::from_utf8(first_char)?;
Expand Down
2 changes: 1 addition & 1 deletion falco_plugin_tests/tests/parse_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl ParsePlugin for DummyPlugin {
let payload = event
.params
.event_data
.ok_or(anyhow::anyhow!("no payload in event"))?;
.ok_or_else(|| anyhow::anyhow!("no payload in event"))?;

let first_char = &payload[0..1];
let first_char = std::str::from_utf8(first_char)?;
Expand Down
2 changes: 1 addition & 1 deletion falco_plugin_tests/tests/parse_table_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl ParsePlugin for DummyPlugin {
let payload = event
.params
.event_data
.ok_or(anyhow::anyhow!("no payload in event"))?;
.ok_or_else(|| anyhow::anyhow!("no payload in event"))?;

let first_char = &payload[0..1];
let first_char = std::str::from_utf8(first_char)?;
Expand Down
2 changes: 1 addition & 1 deletion falco_plugin_tests/tests/parse_table_nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl ParsePlugin for DummyPlugin {
let payload = event
.params
.event_data
.ok_or(anyhow::anyhow!("no payload in event"))?;
.ok_or_else(|| anyhow::anyhow!("no payload in event"))?;

let first_char = &payload[0..1];
let first_char = std::str::from_utf8(first_char)?;
Expand Down

0 comments on commit e295881

Please sign in to comment.