This is an automated email from the ASF dual-hosted git repository.
martinzink pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 3c8ffb8c4 MINIFICPP-2901 Rust route_to_failure by default, rollback
explicit (#2262)
3c8ffb8c4 is described below
commit 3c8ffb8c4ce345fcb9bb9066ce5b32d9832c02c3
Author: Martin Zink <[email protected]>
AuthorDate: Thu Nov 19 10:47:59 2026 +0100
MINIFICPP-2901 Rust route_to_failure by default, rollback explicit (#2262)
---
.github/references/docker_manifest.json | 4 +
minifi_rust/CMakeLists.txt | 28 +-
.../minifi_pgp/src/processors/decrypt_content.rs | 28 +-
.../minifi_pgp/src/processors/encrypt_content.rs | 55 ++-
.../features/error-handling.feature | 4 +-
.../minifi_rs_playground/minifi_rs_playground.md | 7 +-
.../src/processors/asciify_german.rs | 10 +-
.../src/processors/asciify_german/tests.rs | 2 +-
.../src/processors/count_actual_logging.rs | 6 +-
.../src/processors/duplicate_text.rs | 16 +-
.../src/processors/generate_flow_file.rs | 6 +-
.../src/processors/get_file.rs | 6 +-
.../src/processors/kamikaze_processor.rs | 8 +-
.../src/processors/kamikaze_processor/tests.rs | 4 +-
.../src/processors/log_attribute.rs | 7 +-
.../src/processors/lorem_ipsum_cs_user.rs | 4 +-
.../src/processors/put_file.rs | 17 +-
.../src/processors/zoo_processor.rs | 6 +-
.../src/low_level_processors/classify_output.rs | 58 ++--
.../low_level_processors/filter_bounding_boxes.rs | 50 ++-
.../src/low_level_processors/image_to_tensor.rs | 30 +-
.../src/low_level_processors/invoke_tract_model.rs | 17 +-
.../minifi_tensor/src/processors/classify_image.rs | 17 +-
.../minifi_tensor/src/processors/detect_object.rs | 17 +-
.../src/processors/draw_bounding_box.rs | 27 +-
minifi_rust/minifi_native/src/api/errors.rs | 385 ++++++++++++++++-----
.../api/processor_wrappers/complex_processor.rs | 14 +-
.../src/api/processor_wrappers/flow_file_source.rs | 17 +-
.../flow_file_stream_transform.rs | 162 +++++++--
.../api/processor_wrappers/flow_file_transform.rs | 147 +++++---
minifi_rust/minifi_native/src/api/raw_processor.rs | 6 +-
minifi_rust/minifi_native/src/api/relationship.rs | 70 ++++
.../src/c_ffi/c_ffi_processor_definition.rs | 21 +-
minifi_rust/minifi_native/src/lib.rs | 2 +-
.../minifi_native/src/mock/mock_process_session.rs | 6 +-
minifi_rust/minifi_native/src/test_utils.rs | 50 ++-
36 files changed, 894 insertions(+), 420 deletions(-)
diff --git a/.github/references/docker_manifest.json
b/.github/references/docker_manifest.json
index 64de854b7..465fe2847 100644
--- a/.github/references/docker_manifest.json
+++ b/.github/references/docker_manifest.json
@@ -12317,6 +12317,10 @@
"inputRequirement": "INPUT_REQUIRED",
"isSingleThreaded": "true",
"supportedRelationships": [
+ {
+ "name": "failure",
+ "description": "Flowfiles that could not be
duplicated are routed here"
+ },
{
"name": "success",
"description": {}
diff --git a/minifi_rust/CMakeLists.txt b/minifi_rust/CMakeLists.txt
index b745c43ba..0d12a6d8c 100644
--- a/minifi_rust/CMakeLists.txt
+++ b/minifi_rust/CMakeLists.txt
@@ -40,14 +40,32 @@ endif()
include(CTest)
+find_program(CARGO_NEXTEST_EXECUTABLE cargo-nextest)
+
# Build the test binaries during the build
add_custom_target(cargo_build_tests ALL
COMMAND cargo test --no-run
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Building Rust test binaries")
-add_test(
- NAME cargo_tests
- COMMAND cargo test
- WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-)
+if (CARGO_NEXTEST_EXECUTABLE)
+ message(STATUS "Found cargo-nextest, using it for Rust tests.")
+ add_test(
+ NAME cargo_tests
+ COMMAND cargo nextest run
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+ )
+ add_test(
+ NAME cargo_doctests
+ COMMAND cargo test --doc
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+ )
+else()
+ message(WARNING "cargo-nextest not found. Falling back to standard cargo
test. "
+ "For faster test execution, install it via: cargo install
cargo-nextest --locked")
+ add_test(
+ NAME cargo_tests
+ COMMAND cargo test
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+ )
+endif()
diff --git
a/minifi_rust/extensions/minifi_pgp/src/processors/decrypt_content.rs
b/minifi_rust/extensions/minifi_pgp/src/processors/decrypt_content.rs
index d9ac22d9b..73c4fe735 100644
--- a/minifi_rust/extensions/minifi_pgp/src/processors/decrypt_content.rs
+++ b/minifi_rust/extensions/minifi_pgp/src/processors/decrypt_content.rs
@@ -9,7 +9,7 @@ use
crate::controller_services::private_key_service::PGPPrivateKeyService;
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
FlowFileStreamTransform, GetControllerService, GetProperty, InputStream,
Logger, MinifiError,
- OutputStream, ProcessError, RouteErrorExt, Schedule, TransformStreamResult,
+ OutputStream, Relationship, Schedule, TransformError,
TransformStreamResult,
};
use pgp::composed::{Message, TheRing};
@@ -62,32 +62,26 @@ impl DecryptContentPGP {
}
impl FlowFileStreamTransform for DecryptContentPGP {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<Ctx: GetProperty + GetControllerService, LoggerImpl: Logger>(
&self,
context: &Ctx,
input_stream: &mut dyn InputStream,
output_stream: &mut dyn OutputStream,
_logger: &LoggerImpl,
- ) -> Result<TransformStreamResult, ProcessError> {
+ ) -> Result<TransformStreamResult, TransformError> {
let private_key_service =
context.get_controller_service(&PRIVATE_KEY_SERVICE)?;
- let msg = Message::from_reader(input_stream)
- .map(|(msg, _header)| msg)
- .route_err_to_failure()?;
+ let msg = Message::from_reader(input_stream).map(|(msg, _header)|
msg)?;
- let mut decrypted_msg = self
- .decrypt_msg(msg, private_key_service)
- .route_err_to_failure()?;
+ let mut decrypted_msg = self.decrypt_msg(msg, private_key_service)?;
if decrypted_msg.is_compressed() {
- decrypted_msg = decrypted_msg
- .decompress()
- .map_err(MinifiError::other)
- .route_err_to_failure()?
+ decrypted_msg = decrypted_msg.decompress()?
};
- let _written_bytes =
- std::io::copy(&mut decrypted_msg.into_inner(),
output_stream).route_err_to_failure()?;
+ let _written_bytes = std::io::copy(&mut decrypted_msg.into_inner(),
output_stream)?;
Ok(TransformStreamResult::new(&SUCCESS))
}
@@ -246,7 +240,7 @@ mod tests {
assert_eq!(res.write_status(), IoState::Ok);
assert_eq!(output, result_bytes);
}
- Err(_) => test::assert_routed_to(res, &FAILURE),
+ Err(_) => test::assert_stream_routed_to::<DecryptContentPGP>(res,
&FAILURE),
}
}
@@ -423,7 +417,7 @@ mod tests {
let mut ciphertext = std::io::Cursor::new(ciphertext);
let res =
decrypt_content.transform(&context, &mut ciphertext, &mut output,
&MockLogger::new());
- test::assert_routed_to(res, &FAILURE);
+ test::assert_stream_routed_to::<DecryptContentPGP>(res, &FAILURE);
}
#[test]
@@ -456,6 +450,6 @@ mod tests {
&logger,
);
- test::assert_routed_to(res, &FAILURE);
+ test::assert_stream_routed_to::<DecryptContentPGP>(res, &FAILURE);
}
}
diff --git
a/minifi_rust/extensions/minifi_pgp/src/processors/encrypt_content.rs
b/minifi_rust/extensions/minifi_pgp/src/processors/encrypt_content.rs
index bd109a65d..a14507a7a 100644
--- a/minifi_rust/extensions/minifi_pgp/src/processors/encrypt_content.rs
+++ b/minifi_rust/extensions/minifi_pgp/src/processors/encrypt_content.rs
@@ -5,8 +5,8 @@
use crate::controller_services::encryption_key::{EncryptionTarget,
select_encryption_target};
use minifi_native::{
FlowFileStreamTransform, GetAttribute, GetControllerService, GetId,
GetProperty, InputStream,
- Logger, MinifiError, OutputStream, ProcessError, RouteErrorExt, Schedule,
- TransformStreamResult,
+ Logger, MinifiError, OutputStream, Relationship, Schedule, TransformError,
+ TransformStreamResult, route_to_err,
};
use pgp::composed::{ArmorOptions, MessageBuilder, SignedPublicKey};
use pgp::types::{Password, StringToKey};
@@ -48,11 +48,9 @@ impl EncryptContentPGP {
output_stream: &mut dyn OutputStream,
pub_key: Option<&SignedPublicKey>,
file_name: String,
- ) -> Result<(), MinifiError> {
+ ) -> Result<(), TransformError> {
if pub_key.is_none() && self.symmetric_password.is_none() {
- return Err(MinifiError::custom(
- "No password or public key to encrypt with",
- ));
+ route_to_err!("No password or public key to encrypt with");
}
let mut builder = MessageBuilder::from_reader(file_name,
input_stream).seipd_v1(
@@ -62,29 +60,29 @@ impl EncryptContentPGP {
if let Some(pub_key) = pub_key {
match select_encryption_target(pub_key)? {
- EncryptionTarget::Primary(primary_key) => builder
- .encrypt_to_key(rand::thread_rng(), primary_key)
- .map_err(MinifiError::other)?,
- EncryptionTarget::Subkey(subkey) => builder
- .encrypt_to_key(rand::thread_rng(), subkey)
- .map_err(MinifiError::other)?,
+ EncryptionTarget::Primary(primary_key) => {
+ builder.encrypt_to_key(rand::thread_rng(), primary_key)?
+ }
+ EncryptionTarget::Subkey(subkey) => {
+ builder.encrypt_to_key(rand::thread_rng(), subkey)?
+ }
};
}
if let Some(password) = &self.symmetric_password {
- builder
- .encrypt_with_password(string_to_key(), password)
- .map_err(MinifiError::other)?;
+ builder.encrypt_with_password(string_to_key(), password)?;
}
match self.file_encoding {
- FileEncoding::Ascii => builder
- .to_armored_writer(rand::thread_rng(),
ArmorOptions::default(), output_stream)
- .map_err(MinifiError::other),
- FileEncoding::Binary => builder
- .to_writer(rand::thread_rng(), output_stream)
- .map_err(MinifiError::other),
- }
+ FileEncoding::Ascii => builder.to_armored_writer(
+ rand::thread_rng(),
+ ArmorOptions::default(),
+ output_stream,
+ )?,
+ FileEncoding::Binary => builder.to_writer(rand::thread_rng(),
output_stream)?,
+ };
+
+ Ok(())
}
}
@@ -130,6 +128,8 @@ impl EncryptContentPGP {
}
impl FlowFileStreamTransform for EncryptContentPGP {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<
Ctx: GetProperty + GetControllerService + GetAttribute + GetId,
LoggerImpl: Logger,
@@ -139,15 +139,14 @@ impl FlowFileStreamTransform for EncryptContentPGP {
input_stream: &mut dyn InputStream,
output_stream: &mut dyn OutputStream,
_logger: &LoggerImpl,
- ) -> Result<TransformStreamResult, ProcessError> {
+ ) -> Result<TransformStreamResult, TransformError> {
let file_name = match context.get_attribute("filename")? {
Some(file_name) => file_name,
None => context.get_id()?,
};
- let public_key = Self::get_public_key(context).route_err_to_failure()?;
+ let public_key = Self::get_public_key(context)?;
- self.encrypt_bytes(input_stream, output_stream, public_key, file_name)
- .route_err_to_failure()?;
+ self.encrypt_bytes(input_stream, output_stream, public_key,
file_name)?;
Ok(TransformStreamResult::new(&SUCCESS)
.with_attribute(FILE_ENCODING_ATTR.name,
self.file_encoding.into_str()))
@@ -351,7 +350,7 @@ mod tests {
EncryptContentPGP::schedule(&context,
&MockLogger::new()).expect("should schedule");
let res = processor.transform(&context, &mut input_stream, &mut
result, &MockLogger::new());
- test::assert_routed_to(res, &FAILURE);
+ test::assert_stream_routed_to::<EncryptContentPGP>(res, &FAILURE);
}
#[test]
@@ -374,6 +373,6 @@ mod tests {
EncryptContentPGP::schedule(&context,
&MockLogger::new()).expect("should schedule");
let res = processor.transform(&context, &mut input_stream, &mut
result, &MockLogger::new());
- test::assert_routed_to(res, &FAILURE);
+ test::assert_stream_routed_to::<EncryptContentPGP>(res, &FAILURE);
}
}
diff --git
a/minifi_rust/extensions/minifi_rs_playground/features/error-handling.feature
b/minifi_rust/extensions/minifi_rs_playground/features/error-handling.feature
index 799602908..912ce0be9 100644
---
a/minifi_rust/extensions/minifi_rs_playground/features/error-handling.feature
+++
b/minifi_rust/extensions/minifi_rs_playground/features/error-handling.feature
@@ -25,7 +25,7 @@ Feature: API error handling and logging
When the MiNiFi instance starts up
- Then the Minifi logs contain the following message: "KamikazeProcessorRs]
[error] Error during schedule: CustomError("it was designed to fail during
schedule")" in less than 10 seconds
+ Then the Minifi logs contain the following message: "KamikazeProcessorRs]
[error] Error during schedule: it was designed to fail during schedule" in less
than 10 seconds
And the Minifi logs contain the following message: "(KamikazeProcessorRs):
Process Schedule Operation: Error while scheduling processor" in less than 10
seconds
Scenario: Minifi handles errors from trigger
@@ -35,7 +35,7 @@ Feature: API error handling and logging
When the MiNiFi instance starts up
- Then the Minifi logs contain the following message: "KamikazeProcessorRs]
[error] Error during trigger CustomError("it was designed to fail in trigger")"
in less than 10 seconds
+ Then the Minifi logs contain the following message: "KamikazeProcessorRs]
[error] Error during trigger it was designed to fail in trigger" in less than
10 seconds
And the Minifi logs contain the following message: "Trigger and commit
failed for processor KamikazeProcessorRs" in less than 10 seconds
Scenario: Panic in extension's schedule crashes the agent aswell
diff --git
a/minifi_rust/extensions/minifi_rs_playground/minifi_rs_playground.md
b/minifi_rust/extensions/minifi_rs_playground/minifi_rs_playground.md
index d644ca44b..a77fd6a2a 100644
--- a/minifi_rust/extensions/minifi_rs_playground/minifi_rs_playground.md
+++ b/minifi_rust/extensions/minifi_rs_playground/minifi_rs_playground.md
@@ -81,9 +81,10 @@ In the list below, the names of required properties appear
in bold. Any other pr
### Relationships
-| Name | Description |
-|---------|-------------|
-| success | |
+| Name | Description |
+|---------|--------------------------------------------------------|
+| failure | Flowfiles that could not be duplicated are routed here |
+| success | |
## GenerateFlowFileRs
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/asciify_german.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/asciify_german.rs
index 87d80f091..d3e18d691 100644
---
a/minifi_rust/extensions/minifi_rs_playground/src/processors/asciify_german.rs
+++
b/minifi_rust/extensions/minifi_rs_playground/src/processors/asciify_german.rs
@@ -8,7 +8,7 @@ use crate::processors::asciify_german::relationships::FAILURE;
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
FlowFileStreamTransform, GetProperty, InputStream, Logger, MinifiError,
OutputStream,
- ProcessError, Schedule, TransformStreamResult,
+ Relationship, Schedule, TransformError, TransformStreamResult,
route_to_err,
};
mod relationships;
@@ -26,13 +26,15 @@ impl Schedule for AsciifyGerman {
}
impl FlowFileStreamTransform for AsciifyGerman {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<Ctx: GetProperty, LoggerImpl: Logger>(
&self,
_context: &Ctx,
input_stream: &mut dyn InputStream,
output_stream: &mut dyn OutputStream,
_logger: &LoggerImpl,
- ) -> Result<TransformStreamResult, ProcessError> {
+ ) -> Result<TransformStreamResult, TransformError> {
let mut byte = [0u8; 1];
while input_stream.read(&mut byte)? > 0 {
@@ -43,9 +45,7 @@ impl FlowFileStreamTransform for AsciifyGerman {
0xC3 => {
let mut next = [0u8; 1];
if input_stream.read(&mut next)? == 0 {
- return Err(ProcessError::route_to_failure(
- "Truncated multi-byte sequence at EOF",
- ));
+ route_to_err!("Truncated multi-byte sequence at EOF");
}
match next[0] {
0xA4 => output_stream.write_all(b"ae")?, // รค
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/asciify_german/tests.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/asciify_german/tests.rs
index b5d763e22..a6de7edcd 100644
---
a/minifi_rust/extensions/minifi_rs_playground/src/processors/asciify_german/tests.rs
+++
b/minifi_rust/extensions/minifi_rs_playground/src/processors/asciify_german/tests.rs
@@ -71,5 +71,5 @@ fn truncated_umlaut_at_eof_routes_to_failure() {
let mut output_vec: Vec<u8> = Vec::new();
let result = asciify_german.transform(&context, &mut input_stream, &mut
output_vec, &logger);
- test::assert_routed_to(result, &FAILURE);
+ test::assert_stream_routed_to::<AsciifyGerman>(result, &FAILURE);
}
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/count_actual_logging.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/count_actual_logging.rs
index d362f709d..52f01184e 100644
---
a/minifi_rust/extensions/minifi_rs_playground/src/processors/count_actual_logging.rs
+++
b/minifi_rust/extensions/minifi_rs_playground/src/processors/count_actual_logging.rs
@@ -7,8 +7,8 @@
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
GetProperty, Logger, MinifiError, MutTrigger, OnTriggerResult,
OutputAttribute, ProcessContext,
- ProcessError, ProcessSession, ProcessorDefinition,
ProcessorInputRequirement,
- PropertyDefinition, Relationship, Schedule, debug, info, trace,
+ ProcessSession, ProcessorDefinition, ProcessorInputRequirement,
PropertyDefinition,
+ Relationship, Schedule, debug, info, trace,
};
#[derive(Debug, ComponentIdentifier)]
@@ -38,7 +38,7 @@ impl MutTrigger for CountActualLogging {
_context: &mut PC,
_session: &mut PS,
logger: &L,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/duplicate_text.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/duplicate_text.rs
index 6455d1e60..b4659a3a9 100644
---
a/minifi_rust/extensions/minifi_rs_playground/src/processors/duplicate_text.rs
+++
b/minifi_rust/extensions/minifi_rs_playground/src/processors/duplicate_text.rs
@@ -5,8 +5,9 @@
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
GetAttribute, GetControllerService, GetProperty, InputStream, Logger,
MinifiError,
- MutFlowFileStreamTransform, OutputAttribute, OutputStream, ProcessError,
ProcessorDefinition,
- ProcessorInputRequirement, PropertyDefinition, Relationship, Schedule,
TransformStreamResult,
+ MutFlowFileStreamTransform, OutputAttribute, OutputStream,
ProcessorDefinition,
+ ProcessorInputRequirement, PropertyDefinition, Relationship, Schedule,
TransformError,
+ TransformStreamResult,
};
#[derive(Debug, ComponentIdentifier)]
@@ -17,6 +18,11 @@ pub(crate) const SUCCESS: Relationship = Relationship {
description: "",
};
+pub(crate) const FAILURE: Relationship = Relationship {
+ name: "failure",
+ description: "Flowfiles that could not be duplicated are routed here",
+};
+
impl Schedule for DuplicateStreamText {
fn schedule<Ctx: GetProperty, L: Logger>(
_context: &Ctx,
@@ -30,13 +36,15 @@ impl Schedule for DuplicateStreamText {
}
impl MutFlowFileStreamTransform for DuplicateStreamText {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<Ctx: GetProperty + GetControllerService + GetAttribute,
LoggerImpl: Logger>(
&mut self,
_context: &Ctx,
input_stream: &mut dyn InputStream,
output_stream: &mut dyn OutputStream,
_logger: &LoggerImpl,
- ) -> Result<TransformStreamResult, ProcessError> {
+ ) -> Result<TransformStreamResult, TransformError> {
let mut byte = [0u8; 1];
while input_stream.read(&mut byte)? > 0 {
let _ = output_stream.write(&byte)?;
@@ -52,6 +60,6 @@ impl ProcessorDefinition for DuplicateStreamText {
const SUPPORTS_DYNAMIC_PROPERTIES: bool = false;
const SUPPORTS_DYNAMIC_RELATIONSHIPS: bool = false;
const OUTPUT_ATTRIBUTES: &'static [OutputAttribute] = &[];
- const RELATIONSHIPS: &'static [Relationship] = &[SUCCESS];
+ const RELATIONSHIPS: &'static [Relationship] = &[SUCCESS, FAILURE];
const PROPERTIES: &'static [PropertyDefinition] = &[];
}
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/generate_flow_file.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/generate_flow_file.rs
index b703d2df1..ff3dfbdcd 100644
---
a/minifi_rust/extensions/minifi_rs_playground/src/processors/generate_flow_file.rs
+++
b/minifi_rust/extensions/minifi_rs_playground/src/processors/generate_flow_file.rs
@@ -6,8 +6,8 @@
use minifi_native::macros::{ComponentIdentifier, PropertyType};
use minifi_native::{
- GetProperty, Logger, MinifiError, OnTriggerResult, ProcessContext,
ProcessError,
- ProcessSession, Schedule, Trigger,
+ GetProperty, Logger, MinifiError, OnTriggerResult, ProcessContext,
ProcessSession, Schedule,
+ Trigger,
};
use rand::RngExt;
use rand::distr::Alphanumeric;
@@ -134,7 +134,7 @@ impl Trigger for GenerateFlowFileRs {
context: &mut PC,
session: &mut PS,
_logger: &L,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/get_file.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/get_file.rs
index 77f0909e5..bf5eac0fc 100644
--- a/minifi_rust/extensions/minifi_rs_playground/src/processors/get_file.rs
+++ b/minifi_rust/extensions/minifi_rs_playground/src/processors/get_file.rs
@@ -13,8 +13,8 @@ use crate::processors::get_file::properties::{
};
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
- GetProperty, IoState, Logger, MinifiError, OnTriggerResult,
ProcessContext, ProcessError,
- ProcessSession, Schedule, Trigger, debug, trace, warn,
+ GetProperty, IoState, Logger, MinifiError, OnTriggerResult,
ProcessContext, ProcessSession,
+ Schedule, Trigger, debug, trace, warn,
};
use std::collections::VecDeque;
use std::error;
@@ -256,7 +256,7 @@ impl Trigger for GetFileRs {
context: &mut PC,
session: &mut PS,
logger: &L,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/kamikaze_processor.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/kamikaze_processor.rs
index 40fba9906..73aa02ec6 100644
---
a/minifi_rust/extensions/minifi_rs_playground/src/processors/kamikaze_processor.rs
+++
b/minifi_rust/extensions/minifi_rs_playground/src/processors/kamikaze_processor.rs
@@ -12,8 +12,8 @@ use crate::processors::kamikaze_processor::properties::{
};
use minifi_native::macros::{ComponentIdentifier, PropertyType};
use minifi_native::{
- GetProperty, Logger, MinifiError, OnTriggerResult, ProcessContext,
ProcessError,
- ProcessSession, Schedule, Trigger,
+ GetProperty, Logger, MinifiError, OnTriggerResult, ProcessContext,
ProcessSession, Schedule,
+ Trigger,
};
use strum_macros::{Display, EnumString, IntoStaticStr, VariantNames};
@@ -68,7 +68,7 @@ impl Trigger for KamikazeProcessorRs {
context: &mut PC,
_session: &mut PS,
_logger: &L,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
@@ -76,7 +76,7 @@ impl Trigger for KamikazeProcessorRs {
{
match self.trigger_behaviour {
KamikazeBehaviour::ReturnErr => {
- Err(MinifiError::custom("it was designed to fail in
trigger").into())
+ Err(MinifiError::custom("it was designed to fail in trigger"))
}
KamikazeBehaviour::ReturnOk => Ok(OnTriggerResult::Ok),
KamikazeBehaviour::Panic => {
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/kamikaze_processor/tests.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/kamikaze_processor/tests.rs
index 04f814036..adf65b5bb 100644
---
a/minifi_rust/extensions/minifi_rs_playground/src/processors/kamikaze_processor/tests.rs
+++
b/minifi_rust/extensions/minifi_rs_playground/src/processors/kamikaze_processor/tests.rs
@@ -4,7 +4,7 @@
use super::*;
use crate::processors::kamikaze_processor::properties::{SCHEDULE_BEHAVIOUR,
TRIGGER_BEHAVIOUR};
-use minifi_native::{MockLogger, MockProcessContext, MockProcessSession,
ProcessError};
+use minifi_native::{MockLogger, MockProcessContext, MockProcessSession};
use std::panic::AssertUnwindSafe;
#[test]
@@ -64,7 +64,7 @@ fn on_trigger_err() {
let mut session = MockProcessSession::new();
assert!(matches!(
processor.trigger(&mut context, &mut session, &MockLogger::new()),
- Err(ProcessError::Fatal(MinifiError::CustomError(_)))
+ Err(MinifiError::CustomError(_))
));
}
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/log_attribute.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/log_attribute.rs
index b479c2261..3e459e15d 100644
---
a/minifi_rust/extensions/minifi_rs_playground/src/processors/log_attribute.rs
+++
b/minifi_rust/extensions/minifi_rs_playground/src/processors/log_attribute.rs
@@ -8,9 +8,8 @@ use
crate::processors::log_attribute::properties::{FLOW_FILES_TO_LOG, LOG_LEVEL,
use minifi_native::StandardPropertyValidator::NonBlankValidator;
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
- GetProperty, LogLevel, Logger, MinifiError, OnTriggerResult,
ProcessContext, ProcessError,
- ProcessSession, PropertyConstraints, PropertySchema, PropertyType,
Schedule, Trigger, debug,
- log, trace,
+ GetProperty, LogLevel, Logger, MinifiError, OnTriggerResult,
ProcessContext, ProcessSession,
+ PropertyConstraints, PropertySchema, PropertyType, Schedule, Trigger,
debug, log, trace,
};
mod properties;
@@ -92,7 +91,7 @@ impl Trigger for LogAttributeRs {
_context: &mut PC,
session: &mut PS,
logger: &L,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/lorem_ipsum_cs_user.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/lorem_ipsum_cs_user.rs
index df3076448..b9772e8a7 100644
---
a/minifi_rust/extensions/minifi_rs_playground/src/processors/lorem_ipsum_cs_user.rs
+++
b/minifi_rust/extensions/minifi_rs_playground/src/processors/lorem_ipsum_cs_user.rs
@@ -12,7 +12,7 @@ use
crate::processors::lorem_ipsum_cs_user::relationships::SUCCESS;
use minifi_native::macros::{ComponentIdentifier, PropertyType};
use minifi_native::{
Content, FlowFileSource, GeneratedFlowFile, GetControllerService,
GetProperty, Logger,
- MinifiError, ProcessError, Schedule, trace,
+ MinifiError, Schedule, trace,
};
use strum_macros::{Display, EnumString, IntoStaticStr, VariantNames};
@@ -45,7 +45,7 @@ impl FlowFileSource for LoremIpsumCSUser {
&self,
context: &'a mut Context,
logger: &LoggerImpl,
- ) -> Result<Vec<GeneratedFlowFile<'a>>, ProcessError> {
+ ) -> Result<Vec<GeneratedFlowFile<'a>>, MinifiError> {
trace!(logger, "generate call {:?}", self);
let dummy_controller_service =
context.get_controller_service(&DUMMY_CONTROLLER_SERVICE)?;
trace!(
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/put_file.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/put_file.rs
index 1108af584..377b2c767 100644
--- a/minifi_rust/extensions/minifi_rs_playground/src/processors/put_file.rs
+++ b/minifi_rust/extensions/minifi_rs_playground/src/processors/put_file.rs
@@ -9,7 +9,8 @@ use
crate::processors::put_file::unix_permissions::PutFileUnixPermissions;
use minifi_native::macros::{ComponentIdentifier, PropertyType};
use minifi_native::{
FlowFileTransform, GetAttribute, GetControllerService, GetId, GetProperty,
InputStream, Logger,
- MinifiError, ProcessError, RouteErrorExt, Schedule, TransformedFlowFile,
trace, warn,
+ MinifiError, Relationship, Schedule, TransformError, TransformErrorExt,
TransformedFlowFile,
+ trace, warn,
};
use std::path::{Path, PathBuf};
use strum_macros::{Display, EnumString, IntoStaticStr, VariantNames};
@@ -85,7 +86,7 @@ impl PutFileRs {
input_stream: &mut dyn InputStream,
logger: &L,
destination: &Path,
- ) -> Result<(), MinifiError>
+ ) -> Result<(), TransformError>
where
L: Logger,
{
@@ -96,7 +97,11 @@ impl PutFileRs {
}
}
let mut file = std::fs::File::create(destination)?;
- std::io::copy(input_stream, &mut file)?;
+ // rollback since we want to retry if the disk is full
+ // not sure if we want to do this in the long term,
+ // but we wanted to test the rollback_err aswell
+ // and this is not production code (only playground)
+ std::io::copy(input_stream, &mut file).rollback_err()?;
match self.unix_permissions.set_file_permissions(destination) {
Ok(_) => {}
Err(err) => {
@@ -149,6 +154,8 @@ impl Schedule for PutFileRs {
}
impl FlowFileTransform for PutFileRs {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<
'a,
Context: GetProperty + GetControllerService + GetAttribute + GetId,
@@ -158,10 +165,10 @@ impl FlowFileTransform for PutFileRs {
context: &Context,
input_stream: &'a mut dyn InputStream,
logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
trace!(logger, "on_trigger: {:?}", self);
- let destination_path =
Self::get_destination_path(context).route_err_to_failure()?;
+ let destination_path = Self::get_destination_path(context)?;
if self.directory_is_full(&destination_path) {
warn!(logger, "Directory is full");
diff --git
a/minifi_rust/extensions/minifi_rs_playground/src/processors/zoo_processor.rs
b/minifi_rust/extensions/minifi_rs_playground/src/processors/zoo_processor.rs
index 7ee270339..85f20aff6 100644
---
a/minifi_rust/extensions/minifi_rs_playground/src/processors/zoo_processor.rs
+++
b/minifi_rust/extensions/minifi_rs_playground/src/processors/zoo_processor.rs
@@ -8,8 +8,8 @@ use crate::controller_services::animal_controller_apis::{
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
GetProperty, Logger, MinifiError, OnTriggerResult, OutputAttribute,
ProcessContext,
- ProcessError, ProcessSession, ProcessorDefinition,
ProcessorInputRequirement, Property,
- PropertyDefinition, Relationship, Schedule, Trigger, critical, info,
property_definitions,
+ ProcessSession, ProcessorDefinition, ProcessorInputRequirement, Property,
PropertyDefinition,
+ Relationship, Schedule, Trigger, critical, info, property_definitions,
};
pub(crate) const CAN_FLY_SERVICE: Property<dyn CanFlyControllerApi> =
@@ -39,7 +39,7 @@ impl Trigger for ZooProcessorRs {
context: &mut Context,
_session: &mut Session,
logger: &Lggr,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
Context: ProcessContext,
Session: ProcessSession<FlowFile = Context::FlowFile>,
diff --git
a/minifi_rust/extensions/minifi_tensor/src/low_level_processors/classify_output.rs
b/minifi_rust/extensions/minifi_tensor/src/low_level_processors/classify_output.rs
index f6008ed36..e9d463b9f 100644
---
a/minifi_rust/extensions/minifi_tensor/src/low_level_processors/classify_output.rs
+++
b/minifi_rust/extensions/minifi_tensor/src/low_level_processors/classify_output.rs
@@ -3,7 +3,7 @@
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
use crate::low_level_processors::classify_output::classify_output_def::{
- CLASS_COUNT_ATTR, CLASS_TOP1_CONFIDENCE_ATTR, CLASS_TOP1_ID_ATTR,
CLASS_TOP1_NAME_ATTR,
+ CLASS_COUNT_ATTR, CLASS_TOP1_CONFIDENCE_ATTR, CLASS_TOP1_ID_ATTR,
CLASS_TOP1_NAME_ATTR, FAILURE,
};
use crate::utils::score_activation::{ScoreActivation, SoftmaxTerms};
use crate::utils::tensor_helpers::{deserialize_tensors, tensor_as_f32,
tensor_shape};
@@ -15,8 +15,8 @@ pub(crate) use classify_output_def::{
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
Content, FlowFileTransform, GetAttribute, GetId, GetProperty, InputStream,
Logger, MinifiError,
- ProcessError, PropertyConstraints, PropertySchema, PropertyType,
RouteErrorExt, Schedule,
- TransformedFlowFile, warn,
+ PropertyConstraints, PropertySchema, PropertyType, Relationship, Schedule,
TransformError,
+ TransformedFlowFile, route_to_err, warn,
};
use serde::Serialize;
use tract::Tensor;
@@ -121,13 +121,10 @@ impl ClassifyOutput {
context: &Context,
logger: &LoggerImpl,
tensors: Vec<Tensor>,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
- let score_floats =
- tensor_as_f32(&tensors,
self.score_output_index).route_err_to_failure()?;
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
+ let score_floats = tensor_as_f32(&tensors, self.score_output_index)?;
if score_floats.is_empty() {
- return Err(ProcessError::route_to_failure(
- "Score tensor is empty; nothing to classify",
- ));
+ route_to_err!("Score tensor is empty; nothing to classify");
}
// A classifier head is a single score vector: shape [num_classes] or
@@ -135,12 +132,12 @@ impl ClassifyOutput {
// leading axis > 1 (a real batch) would silently mix rows and yield
// class ids past num_classes. Reject it rather than produce garbage.
// (`ImageToTensor` emits batch=1 today; this just enforces the
contract.)
- let shape = tensor_shape(&tensors,
self.score_output_index).route_err_to_failure()?;
+ let shape = tensor_shape(&tensors, self.score_output_index)?;
if shape.iter().rev().skip(1).any(|&d| d != 1) {
- return Err(ProcessError::route_to_failure(format!(
+ route_to_err!(
"ClassifyOutput expects a single score vector (shape
[num_classes] or \
[1, .., num_classes]); got {shape:?}. A batch dimension > 1
is not supported."
- )));
+ );
}
let finite: Vec<(usize, f32)> = score_floats
@@ -183,9 +180,7 @@ impl ClassifyOutput {
let (content, extra_attribute) = match
context.get_property(&OUTPUT_ATTRIBUTE_NAME)? {
None => (
- Some(Content::Buffer(
- serde_json::to_vec(&predictions).route_err_to_failure()?,
- )),
+ Some(Content::Buffer(serde_json::to_vec(&predictions)?)),
Some((
MIME_TYPE_ATTR.name.to_string(),
"application/json".to_string(),
@@ -193,10 +188,7 @@ impl ClassifyOutput {
),
Some(output_attr) => (
None,
- Some((
- output_attr,
-
serde_json::to_string(&predictions).route_err_to_failure()?,
- )),
+ Some((output_attr, serde_json::to_string(&predictions)?)),
),
};
@@ -220,13 +212,15 @@ impl ClassifyOutput {
}
impl FlowFileTransform for ClassifyOutput {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<'a, Context: GetProperty + GetAttribute + GetId, LoggerImpl:
Logger>(
&self,
context: &Context,
input_stream: &'a mut dyn InputStream,
logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
- let tensors = deserialize_tensors(context,
input_stream).route_err_to_failure()?;
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
+ let tensors = deserialize_tensors(context, input_stream)?;
self.classify(context, logger, tensors)
}
}
@@ -235,7 +229,7 @@ impl FlowFileTransform for ClassifyOutput {
mod tests {
use super::classify_output_def::FAILURE;
use super::*;
- use minifi_native::{LogLevel, MockLogger, MockProcessContext};
+ use minifi_native::{LogLevel, MockLogger, MockProcessContext, test};
use std::io::Cursor;
use std::io::Write;
use tempfile::NamedTempFile;
@@ -533,13 +527,8 @@ mod tests {
.attributes
.insert("tensor.0.shape".into(), "2,3".into());
let mut stream = Cursor::new(build_payload(&scores));
- let err = processor
- .transform(&context, &mut stream, &MockLogger::new())
- .expect_err("batched scores should be rejected");
- match err {
- ProcessError::Route(route) => assert_eq!(route.relationship,
FAILURE.name),
- other => panic!("expected route to failure, got {other:?}"),
- }
+ let res = processor.transform(&context, &mut stream,
&MockLogger::new());
+ test::assert_routed_to::<ClassifyOutput>(res, &FAILURE);
}
#[test]
@@ -547,14 +536,7 @@ mod tests {
let processor = make_processor(1, ScoreActivation::Softmax);
let context = MockProcessContext::new(); // no tensor.0.bytes
let mut stream = Cursor::new(vec![0u8; 4]);
- let err = processor
- .transform(&context, &mut stream, &MockLogger::new())
- .expect_err("missing attribute should route to failure via a Route
error");
- match err {
- ProcessError::Route(route) => {
- assert_eq!(route.relationship, FAILURE.name)
- }
- other => panic!("expected route to failure, got {other:?}"),
- }
+ let res = processor.transform(&context, &mut stream,
&MockLogger::new());
+ test::assert_routed_to::<ClassifyOutput>(res, &FAILURE);
}
}
diff --git
a/minifi_rust/extensions/minifi_tensor/src/low_level_processors/filter_bounding_boxes.rs
b/minifi_rust/extensions/minifi_tensor/src/low_level_processors/filter_bounding_boxes.rs
index 8a60d2820..e54608144 100644
---
a/minifi_rust/extensions/minifi_tensor/src/low_level_processors/filter_bounding_boxes.rs
+++
b/minifi_rust/extensions/minifi_tensor/src/low_level_processors/filter_bounding_boxes.rs
@@ -4,6 +4,7 @@
mod filter_bounding_boxes_def;
+use
crate::low_level_processors::filter_bounding_boxes::filter_bounding_boxes_def::FAILURE;
use crate::low_level_processors::image_to_tensor::ResizeMode;
use crate::utils::bounding_box::BoundingBox;
use crate::utils::dimensions::Dimensions;
@@ -17,7 +18,7 @@ pub(crate) use filter_bounding_boxes_def::{
use minifi_native::macros::{ComponentIdentifier, PropertyType};
use minifi_native::{
Content, FlowFileTransform, GetAttribute, GetId, GetProperty, InputStream,
Logger, MinifiError,
- ProcessError, RouteErrorExt, Schedule, TransformedFlowFile, debug, trace,
+ Relationship, Schedule, TransformError, TransformedFlowFile, debug,
route_to_err, trace,
};
use strum_macros::{Display, EnumString, IntoStaticStr, VariantNames};
use tract::Tensor;
@@ -146,23 +147,18 @@ impl FilterBoundingBoxes {
&self,
context: &Context,
filtered_boxes: Vec<BoundingBox>,
- ) -> Result<TransformedFlowFile<'a>, MinifiError> {
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
let output_attr = context.get_property(&OUTPUT_ATTRIBUTE_NAME)?;
let content = if output_attr.is_some() {
None
} else {
- Some(Content::Buffer(
-
serde_json::to_vec(&filtered_boxes).map_err(MinifiError::other)?,
- ))
+ Some(Content::Buffer(serde_json::to_vec(&filtered_boxes)?))
};
let mut transformed = TransformedFlowFile::new(&SUCCESS, content)
.with_attribute("object.count", filtered_boxes.len().to_string());
if let Some(attr) = output_attr {
- transformed = transformed.with_attribute(
- attr,
-
serde_json::to_string(&filtered_boxes).map_err(MinifiError::other)?,
- )
+ transformed = transformed.with_attribute(attr,
serde_json::to_string(&filtered_boxes)?)
} else {
transformed = transformed.with_attribute(MIME_TYPE_ATTR.name,
"application/json");
}
@@ -177,10 +173,9 @@ impl FilterBoundingBoxes {
orig_dim: Dimensions,
target_dim: Dimensions,
resize_mode: ResizeMode,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
- let score_floats =
- tensor_as_f32(&tensors,
self.score_output_index).route_err_to_failure()?;
- let box_floats = tensor_as_f32(&tensors,
self.box_output_index).route_err_to_failure()?;
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
+ let score_floats = tensor_as_f32(&tensors, self.score_output_index)?;
+ let box_floats = tensor_as_f32(&tensors, self.box_output_index)?;
let (scale_x, scale_y, pad_x, pad_y) = match resize_mode {
ResizeMode::Letterbox => {
@@ -201,16 +196,12 @@ impl FilterBoundingBoxes {
};
if !box_floats.len().is_multiple_of(4) {
- return Err(ProcessError::route_to_failure(
- "Box tensor byte length is not a multiple of 16 (4 f32 per
box)",
- ));
+ route_to_err!("Box tensor byte length is not a multiple of 16 (4
f32 per box)");
}
let num_boxes = box_floats.len() / 4;
if num_boxes == 0 {
debug!(logger, "No boxes to filter; emitting empty array");
- return self
- .result_via_output_attribute(context, vec![])
- .route_err_to_failure();
+ return self.result_via_output_attribute(context, vec![]);
}
let make_box = |i: usize, class_id: usize, confidence: f32| ->
BoundingBox {
@@ -239,15 +230,15 @@ impl FilterBoundingBoxes {
match self.class_output_index {
// Separate class-id tensor: one score and one class id per box
Some(class_index) => {
- let class_floats = tensor_as_f32(&tensors,
class_index).route_err_to_failure()?;
+ let class_floats = tensor_as_f32(&tensors, class_index)?;
if score_floats.len() != num_boxes || class_floats.len() !=
num_boxes {
- return Err(ProcessError::route_to_failure(format!(
+ route_to_err!(
"'Class output index' mode expects one score and one
class id per box \
(num_boxes={}, scores={}, classes={})",
num_boxes,
score_floats.len(),
class_floats.len()
- )));
+ );
}
trace!(
logger,
@@ -279,11 +270,11 @@ impl FilterBoundingBoxes {
// Per-class score matrix: argmax over classes per box.
None => {
if !score_floats.len().is_multiple_of(num_boxes) {
- return Err(ProcessError::route_to_failure(format!(
+ route_to_err!(
"Scores length ({}) not divisible by number of boxes
({})",
score_floats.len(),
num_boxes
- )));
+ );
}
let num_classes = score_floats.len() / num_boxes;
trace!(
@@ -317,7 +308,6 @@ impl FilterBoundingBoxes {
BoundingBox::apply_non_maximum_suppression(valid_boxes,
self.iou_threshold);
self.result_via_output_attribute(context, filtered_boxes)
- .route_err_to_failure()
}
}
@@ -331,17 +321,19 @@ fn resize_mode_from_attributes<Context:
GetAttribute>(context: &Context) -> Resi
}
impl FlowFileTransform for FilterBoundingBoxes {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<'a, Context: GetProperty + GetAttribute + GetId, LoggerImpl:
Logger>(
&self,
context: &Context,
input_stream: &'a mut dyn InputStream,
logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
- let orig_dim =
Dimensions::original_from_attributes(context).route_err_to_failure()?;
- let target_dim =
Dimensions::target_from_attributes(context).route_err_to_failure()?;
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
+ let orig_dim = Dimensions::original_from_attributes(context)?;
+ let target_dim = Dimensions::target_from_attributes(context)?;
let resize_mode = resize_mode_from_attributes(context);
- let tensors = deserialize_tensors(context,
input_stream).route_err_to_failure()?;
+ let tensors = deserialize_tensors(context, input_stream)?;
self.filter(context, logger, tensors, orig_dim, target_dim,
resize_mode)
}
diff --git
a/minifi_rust/extensions/minifi_tensor/src/low_level_processors/image_to_tensor.rs
b/minifi_rust/extensions/minifi_tensor/src/low_level_processors/image_to_tensor.rs
index 16b1884b7..bd9ae5167 100644
---
a/minifi_rust/extensions/minifi_tensor/src/low_level_processors/image_to_tensor.rs
+++
b/minifi_rust/extensions/minifi_tensor/src/low_level_processors/image_to_tensor.rs
@@ -3,7 +3,9 @@
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
pub(crate) mod image_to_tensor_def;
-use
crate::low_level_processors::image_to_tensor::image_to_tensor_def::TENSOR_BYTES_ATTR;
+use crate::low_level_processors::image_to_tensor::image_to_tensor_def::{
+ FAILURE, TENSOR_BYTES_ATTR,
+};
use crate::utils::dimensions::{Dimensions, LetterboxGeometry};
use crate::utils::per_channel_f32::PerChannelF32;
use crate::utils::tensor_helpers::{MinifiDatumType, load_as_image};
@@ -19,7 +21,7 @@ use image_to_tensor_def::{IMG_TRG_HEIGHT_ATTR,
IMG_TRG_WIDTH_ATTR, TENSORS_LEN_A
use minifi_native::macros::{ComponentIdentifier, PropertyType};
use minifi_native::{
FlowFileTransform, GetAttribute, GetControllerService, GetId, GetProperty,
InputStream, Logger,
- MinifiError, ProcessError, RouteErrorExt, Schedule, TransformedFlowFile,
+ MinifiError, Relationship, Schedule, TransformError, TransformedFlowFile,
};
use std::num::NonZeroU32;
use strum_macros::{Display, EnumString, IntoStaticStr, VariantNames};
@@ -303,7 +305,7 @@ impl ImageToTensor {
self.resize_mode
}
- pub fn get_tensor(&self, img: image::DynamicImage) -> Result<Tensor,
MinifiError> {
+ pub fn get_tensor(&self, img: image::DynamicImage) -> Result<Tensor,
TransformError> {
let f32_data: Vec<f32> = self
.tensor_bytes(img)
.as_chunks::<4>()
@@ -312,12 +314,14 @@ impl ImageToTensor {
.map(|c| f32::from_le_bytes(*c))
.collect();
let shape = self.get_shape();
- let array = ndarray::Array::from_shape_vec(shape,
f32_data).map_err(MinifiError::other)?;
+ let array = ndarray::Array::from_shape_vec(shape, f32_data)?;
Ok(array.tract()?)
}
}
impl FlowFileTransform for ImageToTensor {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<
'a,
Context: GetProperty + GetControllerService + GetAttribute + GetId,
@@ -327,8 +331,8 @@ impl FlowFileTransform for ImageToTensor {
_context: &Context,
input_stream: &'a mut dyn InputStream,
_logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
- let img = load_as_image(input_stream).route_err_to_failure()?;
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
+ let img = load_as_image(input_stream)?;
let orig_dim = Dimensions::from_image(&img);
let tensor_bytes = self.tensor_bytes(img);
@@ -355,7 +359,7 @@ mod tests {
use super::image_to_tensor_def::{FAILURE, SUCCESS};
use super::*;
use image::{ImageFormat, RgbImage};
- use minifi_native::{MockLogger, MockProcessContext};
+ use minifi_native::{MockLogger, MockProcessContext, test};
use std::io::Cursor;
fn create_test_image_bytes() -> Vec<u8> {
@@ -512,16 +516,8 @@ mod tests {
let invalid_bytes = vec![0x00, 0x01, 0x02, 0x03, 0x04];
let mut input_stream = Cursor::new(invalid_bytes);
- let err = processor
- .transform(&context, &mut input_stream, &MockLogger::new())
- .expect_err("Invalid image should route to FAILURE via a Route
error");
-
- match err {
- ProcessError::Route(route) => {
- assert_eq!(route.relationship, FAILURE.name)
- }
- other => panic!("expected route to failure, got {other:?}"),
- }
+ let res = processor.transform(&context, &mut input_stream,
&MockLogger::new());
+ test::assert_routed_to::<ImageToTensor>(res, &FAILURE);
}
#[test]
diff --git
a/minifi_rust/extensions/minifi_tensor/src/low_level_processors/invoke_tract_model.rs
b/minifi_rust/extensions/minifi_tensor/src/low_level_processors/invoke_tract_model.rs
index 991ffadb7..13574b70c 100644
---
a/minifi_rust/extensions/minifi_tensor/src/low_level_processors/invoke_tract_model.rs
+++
b/minifi_rust/extensions/minifi_tensor/src/low_level_processors/invoke_tract_model.rs
@@ -8,7 +8,7 @@ use invoke_tract_model_def::*;
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
FlowFileTransform, GetAttribute, GetControllerService, GetId, GetProperty,
InputStream, Logger,
- MinifiError, ProcessError, RouteErrorExt, Schedule, TransformedFlowFile,
+ MinifiError, Relationship, Schedule, TransformError, TransformedFlowFile,
route_to_err,
};
use tract::__ndarray_interop::TensorInterface;
use tract::Tensor;
@@ -33,6 +33,8 @@ impl Schedule for InvokeTractModel {
}
impl FlowFileTransform for InvokeTractModel {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<
'a,
Context: GetProperty + GetControllerService + GetAttribute + GetId,
@@ -42,18 +44,15 @@ impl FlowFileTransform for InvokeTractModel {
context: &Context,
input_stream: &'a mut dyn InputStream,
_logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
let controller_service =
context.get_controller_service(&TRACT_MODEL_SERVICE)?;
- let input_tensors: Vec<Tensor> =
- deserialize_tensors(context, input_stream).route_err_to_failure()?;
+ let input_tensors: Vec<Tensor> = deserialize_tensors(context,
input_stream)?;
if input_tensors.len() != 1 {
- return Err(ProcessError::route_to_failure("Invalid input"));
+ route_to_err!("Invalid input");
};
- let output_tensors = controller_service
- .run_inference(input_tensors)
- .route_err_to_failure()?;
+ let output_tensors = controller_service.run_inference(input_tensors)?;
let mut output_bytes = Vec::new();
let mut transformed = TransformedFlowFile::new(&SUCCESS, None)
.with_attribute("tensors.len", output_tensors.len().to_string());
@@ -61,7 +60,7 @@ impl FlowFileTransform for InvokeTractModel {
for (i, tensor) in output_tensors.iter().enumerate() {
let (datum_type, out_shape, raw_tensor_bytes) = tensor
.as_bytes()
- .map_err(|e| MinifiError::custom(format!("Failed to read
tensor bytes: {}", e)))?;
+ .map_err(|e| format!("Failed to read tensor bytes: {e}"))?;
output_bytes.extend_from_slice(raw_tensor_bytes);
diff --git
a/minifi_rust/extensions/minifi_tensor/src/processors/classify_image.rs
b/minifi_rust/extensions/minifi_tensor/src/processors/classify_image.rs
index 1f28facc9..d1c4cf3da 100644
--- a/minifi_rust/extensions/minifi_tensor/src/processors/classify_image.rs
+++ b/minifi_rust/extensions/minifi_tensor/src/processors/classify_image.rs
@@ -11,7 +11,7 @@ use classify_image_def::TRACT_MODEL_SERVICE;
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
FlowFileTransform, GetAttribute, GetControllerService, GetId, GetProperty,
InputStream, Logger,
- MinifiError, ProcessError, RouteErrorExt, Schedule, TransformedFlowFile,
+ MinifiError, Relationship, Schedule, TransformError, TransformedFlowFile,
};
use tract::Tensor;
@@ -40,6 +40,8 @@ impl Schedule for ClassifyImage {
}
impl FlowFileTransform for ClassifyImage {
+ const ERROR_RELATIONSHIP: &'static Relationship =
&classify_image_def::FAILURE;
+
fn transform<
'a,
Context: GetProperty + GetControllerService + GetAttribute + GetId,
@@ -49,20 +51,15 @@ impl FlowFileTransform for ClassifyImage {
context: &Context,
input_stream: &'a mut dyn InputStream,
logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
let tract_model_service =
context.get_controller_service(&TRACT_MODEL_SERVICE)?;
- let img = load_as_image(input_stream).route_err_to_failure()?;
+ let img = load_as_image(input_stream)?;
// ImageToTensor
- let input_tensor: Tensor = self
- .image_to_tensor
- .get_tensor(img)
- .route_err_to_failure()?;
+ let input_tensor: Tensor = self.image_to_tensor.get_tensor(img)?;
// InvokeTract
- let output_tensors = tract_model_service
- .run_inference(vec![input_tensor])
- .route_err_to_failure()?;
+ let output_tensors =
tract_model_service.run_inference(vec![input_tensor])?;
// ClassifyOutput
self.classify_output
diff --git
a/minifi_rust/extensions/minifi_tensor/src/processors/detect_object.rs
b/minifi_rust/extensions/minifi_tensor/src/processors/detect_object.rs
index d899d91ba..faa439338 100644
--- a/minifi_rust/extensions/minifi_tensor/src/processors/detect_object.rs
+++ b/minifi_rust/extensions/minifi_tensor/src/processors/detect_object.rs
@@ -12,7 +12,7 @@ use detect_object_def::TRACT_MODEL_SERVICE;
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
FlowFileTransform, GetAttribute, GetControllerService, GetId, GetProperty,
InputStream, Logger,
- MinifiError, ProcessError, RouteErrorExt, Schedule, TransformedFlowFile,
+ MinifiError, Relationship, Schedule, TransformError, TransformedFlowFile,
};
use tract::Tensor;
@@ -43,6 +43,8 @@ impl Schedule for DetectObject {
}
impl FlowFileTransform for DetectObject {
+ const ERROR_RELATIONSHIP: &'static Relationship =
&detect_object_def::FAILURE;
+
fn transform<
'a,
Context: GetProperty + GetControllerService + GetAttribute + GetId,
@@ -52,22 +54,17 @@ impl FlowFileTransform for DetectObject {
context: &Context,
input_stream: &'a mut dyn InputStream,
logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
let tract_model_service =
context.get_controller_service(&TRACT_MODEL_SERVICE)?;
- let img = load_as_image(input_stream).route_err_to_failure()?;
+ let img = load_as_image(input_stream)?;
let orig_dim = Dimensions::from_image(&img);
let target_dim = self.image_to_tensor.get_target_dim();
// ImageToTensor
- let input_tensor: Tensor = self
- .image_to_tensor
- .get_tensor(img)
- .route_err_to_failure()?;
+ let input_tensor: Tensor = self.image_to_tensor.get_tensor(img)?;
// InvokeTract
- let output_tensors = tract_model_service
- .run_inference(vec![input_tensor])
- .route_err_to_failure()?;
+ let output_tensors =
tract_model_service.run_inference(vec![input_tensor])?;
// FilterBoundingBox
self.filter_bounding_boxes.filter(
diff --git
a/minifi_rust/extensions/minifi_tensor/src/processors/draw_bounding_box.rs
b/minifi_rust/extensions/minifi_tensor/src/processors/draw_bounding_box.rs
index a53960dba..c623653fb 100644
--- a/minifi_rust/extensions/minifi_tensor/src/processors/draw_bounding_box.rs
+++ b/minifi_rust/extensions/minifi_tensor/src/processors/draw_bounding_box.rs
@@ -7,9 +7,8 @@ use image::Rgb;
use minifi_native::macros::ComponentIdentifier;
use minifi_native::{
FlowFileTransform, GetAttribute, GetControllerService, GetId, GetProperty,
InputStream, Logger,
- MinifiError, OutputAttribute, ProcessError, ProcessorDefinition,
ProcessorInputRequirement,
- Property, PropertyConstraints, PropertyType, Relationship, RouteErrorExt,
Schedule,
- TransformedFlowFile,
+ MinifiError, OutputAttribute, ProcessorDefinition,
ProcessorInputRequirement, Property,
+ PropertyConstraints, PropertyType, Relationship, Schedule, TransformError,
TransformedFlowFile,
};
use minifi_native::{PropertyDefinition, PropertySchema, property_definitions};
use std::io::Cursor;
@@ -92,6 +91,8 @@ impl PropertyType for LineColor {
}
impl FlowFileTransform for DrawBoundingBox {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<
'a,
Context: GetProperty + GetControllerService + GetAttribute + GetId,
@@ -101,31 +102,25 @@ impl FlowFileTransform for DrawBoundingBox {
context: &Context,
input_stream: &'a mut dyn InputStream,
_logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
- let line_thickness = context
- .get_property(&LINE_THICKNESS)
- .route_err_to_failure()?;
- let line_color =
context.get_property(&LINE_COLOR).route_err_to_failure()?;
- let boxes: Vec<BoundingBox> = context
- .get_property(&BOUNDING_BOXES)
- .route_err_to_failure()?;
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
+ let line_thickness = context.get_property(&LINE_THICKNESS)?;
+ let line_color = context.get_property(&LINE_COLOR)?;
+ let boxes: Vec<BoundingBox> = context.get_property(&BOUNDING_BOXES)?;
let mut image_bytes = Vec::new();
input_stream.read_to_end(&mut image_bytes)?;
- let format = image::guess_format(&image_bytes).route_err_to_failure()?;
+ let format = image::guess_format(&image_bytes)?;
let mut img = image::load_from_memory_with_format(&image_bytes, format)
- .map(|dyn_img| dyn_img.to_rgb8())
- .route_err_to_failure()?;
+ .map(|dyn_img| dyn_img.to_rgb8())?;
boxes
.iter()
.for_each(|bbox| bbox.draw_onto(&mut img, line_thickness,
line_color));
let mut output_bytes = Vec::new();
- img.write_to(&mut Cursor::new(&mut output_bytes), format)
- .route_err_to_failure()?;
+ img.write_to(&mut Cursor::new(&mut output_bytes), format)?;
Ok(TransformedFlowFile::new(
&SUCCESS,
diff --git a/minifi_rust/minifi_native/src/api/errors.rs
b/minifi_rust/minifi_native/src/api/errors.rs
index b0304a9c8..c9cf1c08b 100644
--- a/minifi_rust/minifi_native/src/api/errors.rs
+++ b/minifi_rust/minifi_native/src/api/errors.rs
@@ -43,87 +43,128 @@ impl fmt::Display for RouteError {
impl Error for RouteError {}
#[derive(Debug)]
-pub enum ProcessError {
+pub enum TransformError {
+ /// The error was explicitly routed to a specific relationship (e.g. via
+ /// [`TransformErrorExt::route_err`]).
Route(RouteError),
- Fatal(MinifiError),
+ /// An error propagated with `?` that has not been explicitly routed. The
+ /// processor wrapper routes it to the transform's declared error
+ /// relationship (see `ERROR_RELATIONSHIP` on the transform traits).
+ Bubbled(Box<dyn Error + Send + Sync + 'static>),
+ /// The processor asked to roll back the session (see
+ /// [`TransformErrorExt::rollback_err`]).
+ Rollback(MinifiError),
}
-impl ProcessError {
- pub fn route_to_failure<S: Into<Cow<'static, str>>>(reason: S) -> Self {
- ProcessError::Route(RouteError {
- relationship: "failure",
- source: Box::new(MinifiError::custom(reason)),
- log_level: LogLevel::Warn,
- })
+impl TransformError {
+ /// Resolve this error into either a concrete route or a rollback.
+ ///
+ /// `Bubbled` errors are routed to `default_relationship` at
[`LogLevel::Warn`];
+ /// already-`Route`d errors keep their relationship. Logging the returned
+ /// [`RouteError`] is left to the caller.
+ pub(crate) fn into_route(
+ self,
+ default_relationship: &Relationship,
+ ) -> Result<RouteError, MinifiError> {
+ match self {
+ TransformError::Route(route) => Ok(route),
+ TransformError::Bubbled(source) => Ok(RouteError {
+ relationship: default_relationship.name,
+ source,
+ log_level: LogLevel::Warn,
+ }),
+ TransformError::Rollback(err) => Err(err),
+ }
}
-}
-impl From<RouteError> for ProcessError {
- fn from(err: RouteError) -> Self {
- ProcessError::Route(err)
+ pub fn into_boxed(self) -> Box<dyn Error + Send + Sync + 'static> {
+ match self {
+ TransformError::Route(route) => Box::new(route),
+ TransformError::Bubbled(source) => source,
+ TransformError::Rollback(err) => Box::new(err),
+ }
}
}
-impl From<MinifiError> for ProcessError {
- fn from(err: MinifiError) -> Self {
- ProcessError::Fatal(err)
+impl<E> From<E> for TransformError
+where
+ E: Into<Box<dyn Error + Send + Sync + 'static>>,
+{
+ fn from(err: E) -> Self {
+ let boxed: Box<dyn Error + Send + Sync + 'static> = err.into();
+ match boxed.downcast::<RouteError>() {
+ Ok(route) => TransformError::Route(*route),
+ Err(source) => TransformError::Bubbled(source),
+ }
}
}
-macro_rules! process_error_from_fatal {
- ($($t:ty),* $(,)?) => {
- $(
- impl From<$t> for ProcessError {
- fn from(err: $t) -> Self {
- ProcessError::Fatal(MinifiError::from(err))
- }
- }
- )*
- };
-}
-
-process_error_from_fatal!(
- std::io::Error,
- strum::ParseError,
- ParseBoolError,
- ParseIntError,
- humantime::DurationError,
- byte_unit::ParseError,
- NulError,
- ParseFloatError,
- std::convert::Infallible,
-);
-
-impl fmt::Display for ProcessError {
+impl fmt::Display for TransformError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
- ProcessError::Route(err) => write!(f, "{}", err),
- ProcessError::Fatal(err) => write!(f, "{}", err),
+ TransformError::Route(err) => write!(f, "{}", err),
+ TransformError::Bubbled(err) => write!(f, "{}", err),
+ TransformError::Rollback(err) => write!(f, "{}", err),
}
}
}
-impl Error for ProcessError {}
+/// Return early from a transform with a message, routing the flow file to the
transform's
+/// `ERROR_RELATIONSHIP` (see [`TransformError::Bubbled`]).
+///
+/// Takes `format!` arguments, implicit captures included:
+///
+/// ```
+/// use minifi_native::{TransformError, route_to_err};
+///
+/// fn check(shape: &[usize]) -> Result<(), TransformError> {
+/// if shape.is_empty() {
+/// route_to_err!("Score tensor is empty; nothing to classify");
+/// }
+/// if shape.len() > 1 {
+/// route_to_err!("expected a single score vector, got {shape:?}");
+/// }
+/// Ok(())
+/// }
+///
+/// assert_eq!(
+/// check(&[2, 3]).unwrap_err().to_string(),
+/// "expected a single score vector, got [2, 3]"
+/// );
+/// ```
+///
+/// Names no error type: this works in any function whose error type converts
from `String`.
+/// To roll the session back instead of routing, use
[`TransformErrorExt::rollback_err`].
+#[macro_export]
+macro_rules! route_to_err {
+ ($($arg:tt)+) => {
+ return Err(::std::format!($($arg)+).into())
+ };
+}
-pub trait RouteErrorExt<T> {
- fn route_err(self, rel: &Relationship, level: LogLevel) -> Result<T,
ProcessError>;
+pub trait TransformErrorExt<T> {
+ fn route_err(self, rel: &Relationship, level: LogLevel) -> Result<T,
TransformError>;
- fn route_to(self, relationship: &'static str, level: LogLevel) ->
Result<T, ProcessError>;
+ fn route_to(self, relationship: &'static str, level: LogLevel) ->
Result<T, TransformError>;
- fn route_err_to_failure(self) -> Result<T, ProcessError>;
+ fn rollback_err(self) -> Result<T, TransformError>;
}
-impl<T, E> RouteErrorExt<T> for Result<T, E>
+impl<T, E> TransformErrorExt<T> for Result<T, E>
where
E: Into<Box<dyn Error + Send + Sync + 'static>>,
{
- fn route_err(self, rel: &Relationship, level: LogLevel) -> Result<T,
ProcessError> {
+ fn route_err(self, rel: &Relationship, level: LogLevel) -> Result<T,
TransformError> {
self.route_to(rel.name, level)
}
- fn route_to(self, relationship_name: &'static str, level: LogLevel) ->
Result<T, ProcessError> {
+ fn route_to(
+ self,
+ relationship_name: &'static str,
+ level: LogLevel,
+ ) -> Result<T, TransformError> {
self.map_err(|e| {
- ProcessError::Route(RouteError {
+ TransformError::Route(RouteError {
relationship: relationship_name,
source: e.into(),
log_level: level,
@@ -131,8 +172,14 @@ where
})
}
- fn route_err_to_failure(self) -> Result<T, ProcessError> {
- self.route_to("failure", LogLevel::Warn)
+ fn rollback_err(self) -> Result<T, TransformError> {
+ self.map_err(|e| {
+ let boxed: Box<dyn Error + Send + Sync + 'static> = e.into();
+ match boxed.downcast::<MinifiError>() {
+ Ok(minifi_error) => TransformError::Rollback(*minifi_error),
+ Err(other) =>
TransformError::Rollback(MinifiError::Other(other)),
+ }
+ })
}
}
@@ -249,8 +296,18 @@ impl fmt::Display for MinifiError {
_ => write!(f, "{} (Unknown Status Code: {})", context, code),
},
MinifiError::Other(err) => write!(f, "{}", err),
+ MinifiError::IoError(err) => write!(f, "{}", err),
MinifiError::ValidationError(msg) => write!(f, "{}", msg),
- _ => write!(f, "{:?}", self),
+ MinifiError::CustomError(msg) => write!(f, "{}", msg),
+ MinifiError::MissingRequiredAttribute(name) => {
+ write!(f, "missing required attribute '{}'", name)
+ }
+ MinifiError::MissingRequiredProperty(name) => {
+ write!(f, "missing required property '{}'", name)
+ }
+ MinifiError::MissingFlowFileError => write!(f, "no flow file
available"),
+ MinifiError::UnscheduledProcessor => write!(f, "processor is not
scheduled"),
+ MinifiError::UnknownError => write!(f, "unknown error"),
}
}
}
@@ -265,28 +322,16 @@ mod tests {
std::io::Error::other("boom")
}
- #[test]
- fn route_err_to_failure_uses_warn() {
- let res: Result<(), std::io::Error> = Err(io_err());
- match res.route_err_to_failure() {
- Err(ProcessError::Route(route)) => {
- assert_eq!(route.relationship, "failure");
- assert_eq!(route.log_level, LogLevel::Warn);
- assert_eq!(route.source.to_string(), "boom");
- }
- other => panic!("expected a route error, got {other:?}"),
- }
- }
+ const REJECT: Relationship = Relationship {
+ name: "reject",
+ description: "",
+ };
#[test]
fn route_err_uses_the_relationships_name() {
- const REJECT: Relationship = Relationship {
- name: "reject",
- description: "",
- };
let res: Result<(), std::io::Error> = Err(io_err());
match res.route_err(&REJECT, LogLevel::Info) {
- Err(ProcessError::Route(route)) => {
+ Err(TransformError::Route(route)) => {
assert_eq!(route.relationship, "reject");
assert_eq!(route.log_level, LogLevel::Info);
}
@@ -297,27 +342,201 @@ mod tests {
#[test]
fn ok_values_pass_through_unchanged() {
let res: Result<u8, std::io::Error> = Ok(5);
- assert_eq!(res.route_err_to_failure().unwrap(), 5);
+ assert_eq!(res.route_err(&REJECT, LogLevel::Info).unwrap(), 5);
}
#[test]
- fn minifi_error_converts_to_fatal_via_from() {
- let pe: ProcessError = MinifiError::custom("nope").into();
- assert!(matches!(
- pe,
- ProcessError::Fatal(MinifiError::CustomError(_))
- ));
+ fn minifi_error_converts_to_bubbled_via_from() {
+ let pe: TransformError = MinifiError::custom("nope").into();
+ assert!(matches!(pe, TransformError::Bubbled(_)));
}
#[test]
- fn raw_error_question_mark_becomes_fatal() {
- fn inner() -> Result<(), ProcessError> {
+ fn raw_error_question_mark_becomes_bubbled() {
+ fn inner() -> Result<(), TransformError> {
Err(io_err())?;
Ok(())
}
+ match inner() {
+ Err(TransformError::Bubbled(source)) => {
+ assert_eq!(source.to_string(), "boom");
+ }
+ other => panic!("expected a bubbled error, got {other:?}"),
+ }
+ }
+
+ #[test]
+ fn foreign_error_without_a_from_impl_becomes_bubbled() {
+ #[derive(Debug)]
+ struct Foreign;
+
+ impl fmt::Display for Foreign {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "foreign")
+ }
+ }
+
+ impl Error for Foreign {}
+
+ fn inner() -> Result<(), TransformError> {
+ Err(Foreign)?;
+ Ok(())
+ }
+ match inner() {
+ Err(TransformError::Bubbled(source)) =>
assert_eq!(source.to_string(), "foreign"),
+ other => panic!("expected a bubbled error, got {other:?}"),
+ }
+ }
+
+ #[test]
+ fn anyhow_error_becomes_bubbled() {
+ fn inner() -> Result<(), TransformError> {
+ Err(anyhow::anyhow!("boom"))?;
+ Ok(())
+ }
+ match inner() {
+ Err(TransformError::Bubbled(source)) =>
assert_eq!(source.to_string(), "boom"),
+ other => panic!("expected a bubbled error, got {other:?}"),
+ }
+ }
+
+ #[test]
+ fn bail_bubbles_a_literal_message() {
+ fn inner() -> Result<(), TransformError> {
+ route_to_err!("nothing to classify");
+ }
+ match inner() {
+ Err(TransformError::Bubbled(source)) => {
+ assert_eq!(source.to_string(), "nothing to classify")
+ }
+ other => panic!("expected a bubbled error, got {other:?}"),
+ }
+ }
+
+ // The message must go through `format!`, so implicit captures are
interpolated rather than
+ // printed as literal braces.
+ #[test]
+ fn bail_interpolates_implicit_captures() {
+ fn inner() -> Result<(), TransformError> {
+ let shape = [2, 3];
+ route_to_err!("expected a single score vector, got {shape:?}");
+ }
+ match inner() {
+ Err(TransformError::Bubbled(source)) => {
+ assert_eq!(
+ source.to_string(),
+ "expected a single score vector, got [2, 3]"
+ )
+ }
+ other => panic!("expected a bubbled error, got {other:?}"),
+ }
+ }
+
+ // Guards the reflexive `impl<T> From<T> for T`: a nested transform can be
propagated
+ // with `?` without being re-wrapped. Adding `impl Error for
TransformError` breaks this.
+ #[test]
+ fn transform_error_question_mark_is_not_rewrapped() {
+ fn nested() -> Result<(), TransformError> {
+ Err(MinifiError::validation("bad"))?;
+ Ok(())
+ }
+ fn outer() -> Result<(), TransformError> {
+ nested()?;
+ Ok(())
+ }
+ match outer() {
+ Err(TransformError::Bubbled(source)) =>
assert_eq!(source.to_string(), "bad"),
+ other => panic!("expected a bubbled error, got {other:?}"),
+ }
+ }
+
+ #[test]
+ fn route_error_question_mark_keeps_its_relationship() {
+ fn inner() -> Result<(), TransformError> {
+ Err(RouteError {
+ relationship: "reject",
+ source: Box::new(io_err()),
+ log_level: LogLevel::Info,
+ })?;
+ Ok(())
+ }
+ match inner() {
+ Err(TransformError::Route(route)) => {
+ assert_eq!(route.relationship, "reject");
+ assert_eq!(route.log_level, LogLevel::Info);
+ }
+ other => panic!("expected a route error, got {other:?}"),
+ }
+ }
+
+ #[test]
+ fn into_boxed_erases_every_variant() {
+ let route: TransformError = RouteError {
+ relationship: "reject",
+ source: Box::new(io_err()),
+ log_level: LogLevel::Info,
+ }
+ .into();
+ assert_eq!(
+ route.into_boxed().to_string(),
+ "route to 'reject' due to: boom"
+ );
+
+ let bubbled: TransformError = io_err().into();
+ assert_eq!(bubbled.into_boxed().to_string(), "boom");
+
+ let rollback: Result<(), MinifiError> =
Err(MinifiError::validation("bad"));
+ let rollback = rollback.rollback_err().unwrap_err();
+ assert_eq!(rollback.into_boxed().to_string(), "bad");
+ }
+
+ #[test]
+ fn into_route_routes_bubbled_to_default_relationship_at_warn() {
+ let bubbled: TransformError = io_err().into();
+ match bubbled.into_route(&REJECT) {
+ Ok(route) => {
+ assert_eq!(route.relationship, "reject");
+ assert_eq!(route.log_level, LogLevel::Warn);
+ assert_eq!(route.source.to_string(), "boom");
+ }
+ Err(e) => panic!("expected a route, got {e:?}"),
+ }
+ }
+
+ #[test]
+ fn into_route_keeps_explicit_relationship() {
+ let res: Result<(), std::io::Error> = Err(io_err());
+ let routed = res.route_to("explicit", LogLevel::Info).unwrap_err();
+ let route = routed.into_route(&REJECT).expect("should stay a route");
+ assert_eq!(route.relationship, "explicit");
+ assert_eq!(route.log_level, LogLevel::Info);
+ }
+
+ #[test]
+ fn into_route_propagates_rollback_as_minifi_error() {
+ let res: Result<(), MinifiError> = Err(MinifiError::validation("bad"));
+ let rollback = res.rollback_err().unwrap_err();
+ assert!(matches!(
+ rollback.into_route(&REJECT),
+ Err(MinifiError::ValidationError(_))
+ ));
+ }
+
+ #[test]
+ fn rollback_err_wraps_foreign_error_as_other() {
+ let res: Result<(), std::io::Error> = Err(io_err());
+ assert!(matches!(
+ res.rollback_err(),
+ Err(TransformError::Rollback(MinifiError::Other(_)))
+ ));
+ }
+
+ #[test]
+ fn rollback_err_preserves_minifi_error_variant() {
+ let res: Result<(), MinifiError> = Err(MinifiError::validation("bad"));
assert!(matches!(
- inner(),
- Err(ProcessError::Fatal(MinifiError::IoError(_)))
+ res.rollback_err(),
+ Err(TransformError::Rollback(MinifiError::ValidationError(_)))
));
}
}
diff --git
a/minifi_rust/minifi_native/src/api/processor_wrappers/complex_processor.rs
b/minifi_rust/minifi_native/src/api/processor_wrappers/complex_processor.rs
index aa79d436c..1b82329ed 100644
--- a/minifi_rust/minifi_native/src/api/processor_wrappers/complex_processor.rs
+++ b/minifi_rust/minifi_native/src/api/processor_wrappers/complex_processor.rs
@@ -5,7 +5,7 @@
use crate::api::raw_processor::{MultiThreadedTrigger, SingleThreadedTrigger};
use crate::{
ComponentIdentifier, Logger, MinifiError, MultiThreaded, OnTriggerResult,
ProcessContext,
- ProcessError, ProcessSession, Processor, ProcessorDefinition, Schedule,
SingleThreaded,
+ ProcessSession, Processor, ProcessorDefinition, Schedule, SingleThreaded,
};
pub trait MutTrigger {
@@ -14,7 +14,7 @@ pub trait MutTrigger {
context: &mut Ctx,
session: &mut Session,
logger: &Lggr,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
Ctx: ProcessContext,
Session: ProcessSession<FlowFile = Ctx::FlowFile>,
@@ -27,7 +27,7 @@ pub trait Trigger {
context: &mut Context,
session: &mut Session,
logger: &Lggr,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
Context: ProcessContext,
Session: ProcessSession<FlowFile = Context::FlowFile>,
@@ -46,7 +46,7 @@ where
&mut self,
context: &mut PC,
session: &mut PS,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
@@ -54,7 +54,7 @@ where
if let Some(ref mut scheduled_impl) = self.scheduled_impl {
scheduled_impl.trigger(context, session, &self.logger)
} else {
- Err(MinifiError::UnscheduledProcessor.into())
+ Err(MinifiError::UnscheduledProcessor)
}
}
}
@@ -69,7 +69,7 @@ where
&self,
context: &mut PC,
session: &mut PS,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
@@ -77,7 +77,7 @@ where
if let Some(ref scheduled_impl) = self.scheduled_impl {
scheduled_impl.trigger(context, session, &self.logger)
} else {
- Err(MinifiError::UnscheduledProcessor.into())
+ Err(MinifiError::UnscheduledProcessor)
}
}
}
diff --git
a/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_source.rs
b/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_source.rs
index 405617bfc..1392b977a 100644
--- a/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_source.rs
+++ b/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_source.rs
@@ -7,8 +7,7 @@ use crate::api::raw_processor::{MultiThreadedTrigger,
SingleThreadedTrigger};
use crate::{FlowFileAttribute, impl_with_attributes};
use crate::{
GetControllerService, GetProperty, Logger, MinifiError, MultiThreaded,
OnTriggerResult,
- ProcessContext, ProcessError, ProcessSession, Processor, Relationship,
Schedule,
- SingleThreaded,
+ ProcessContext, ProcessSession, Processor, Relationship, Schedule,
SingleThreaded,
};
pub struct GeneratedFlowFile<'a> {
@@ -38,7 +37,7 @@ pub trait FlowFileSource {
&self,
context: &'a mut Context,
logger: &LoggerImpl,
- ) -> Result<Vec<GeneratedFlowFile<'a>>, ProcessError>;
+ ) -> Result<Vec<GeneratedFlowFile<'a>>, MinifiError>;
}
pub trait MutFlowFileSource {
@@ -46,13 +45,13 @@ pub trait MutFlowFileSource {
&mut self,
context: &'a mut Context,
logger: &LoggerImpl,
- ) -> Result<Vec<GeneratedFlowFile<'a>>, ProcessError>;
+ ) -> Result<Vec<GeneratedFlowFile<'a>>, MinifiError>;
}
fn handle_generated_flow_files<PC, PS>(
session: &mut PS,
generated_flow_files: Vec<GeneratedFlowFile>,
-) -> Result<OnTriggerResult, ProcessError>
+) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
@@ -88,7 +87,7 @@ where
&self,
context: &mut PC,
session: &mut PS,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
@@ -97,7 +96,7 @@ where
let files = scheduled_impl.generate(context, &self.logger)?;
handle_generated_flow_files::<PC, PS>(session, files)
} else {
- Err(MinifiError::UnscheduledProcessor.into())
+ Err(MinifiError::UnscheduledProcessor)
}
}
}
@@ -112,7 +111,7 @@ where
&mut self,
context: &mut PC,
session: &mut PS,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
@@ -121,7 +120,7 @@ where
let files = scheduled_impl.generate(context, &self.logger)?;
handle_generated_flow_files::<PC, PS>(session, files)
} else {
- Err(MinifiError::UnscheduledProcessor.into())
+ Err(MinifiError::UnscheduledProcessor)
}
}
}
diff --git
a/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_stream_transform.rs
b/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_stream_transform.rs
index 4253c179d..d1898e140 100644
---
a/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_stream_transform.rs
+++
b/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_stream_transform.rs
@@ -8,8 +8,8 @@ use crate::api::raw_processor::{MultiThreadedTrigger,
SingleThreadedTrigger};
use crate::{FlowFileAttribute, impl_with_attributes};
use crate::{
GetAttribute, GetControllerService, GetProperty, InputStream, LogLevel,
Logger, MinifiError,
- MultiThreaded, OnTriggerResult, OutputStream, ProcessContext,
ProcessError, ProcessSession,
- Processor, Relationship, Schedule, SingleThreaded,
+ MultiThreaded, OnTriggerResult, OutputStream, ProcessContext,
ProcessSession, Processor,
+ ProcessorDefinition, Relationship, Schedule, SingleThreaded,
TransformError,
};
use minifi_native::GetId;
@@ -59,7 +59,20 @@ impl TransformStreamResult {
impl_with_attributes!(TransformStreamResult);
-pub trait FlowFileStreamTransform {
+pub trait FlowFileStreamTransform: ProcessorDefinition {
+ /// Relationship that errors propagated with `?` (i.e.
[`TransformError::Bubbled`])
+ /// are routed to. Must be one of [`ProcessorDefinition::RELATIONSHIPS`].
+ ///
+ /// To roll the session back instead of routing, return
[`TransformError::Rollback`] or use
+ /// [`crate::TransformErrorExt::rollback_err`].
+ const ERROR_RELATIONSHIP: &'static Relationship;
+
+ /// Forced by the processor wrapper's trigger; implementors should not
override it.
+ const ERROR_RELATIONSHIP_DECLARED: () = assert!(
+ crate::api::relationship::is_declared(Self::RELATIONSHIPS,
Self::ERROR_RELATIONSHIP),
+ "ERROR_RELATIONSHIP must also be listed in this processor's
RELATIONSHIPS"
+ );
+
fn transform<
Ctx: GetProperty + GetControllerService + GetAttribute + GetId,
LoggerImpl: Logger,
@@ -69,17 +82,30 @@ pub trait FlowFileStreamTransform {
input_stream: &mut dyn InputStream,
output_stream: &mut dyn OutputStream,
logger: &LoggerImpl,
- ) -> Result<TransformStreamResult, ProcessError>;
+ ) -> Result<TransformStreamResult, TransformError>;
}
-pub trait MutFlowFileStreamTransform {
+pub trait MutFlowFileStreamTransform: ProcessorDefinition {
+ /// Relationship that errors propagated with `?` (i.e.
[`TransformError::Bubbled`])
+ /// are routed to. Must be one of [`ProcessorDefinition::RELATIONSHIPS`].
+ ///
+ /// To roll the session back instead of routing, return
[`TransformError::Rollback`] or use
+ /// [`crate::TransformErrorExt::rollback_err`].
+ const ERROR_RELATIONSHIP: &'static Relationship;
+
+ /// Forced by the processor wrapper's trigger; implementors should not
override it.
+ const ERROR_RELATIONSHIP_DECLARED: () = assert!(
+ crate::api::relationship::is_declared(Self::RELATIONSHIPS,
Self::ERROR_RELATIONSHIP),
+ "ERROR_RELATIONSHIP must also be listed in this processor's
RELATIONSHIPS"
+ );
+
fn transform<Ctx: GetProperty + GetControllerService + GetAttribute,
LoggerImpl: Logger>(
&mut self,
context: &Ctx,
input_stream: &mut dyn InputStream,
output_stream: &mut dyn OutputStream,
logger: &LoggerImpl,
- ) -> Result<TransformStreamResult, ProcessError>;
+ ) -> Result<TransformStreamResult, TransformError>;
}
pub struct FlowFileStreamTransformProcessorType {}
@@ -88,8 +114,9 @@ fn handle_stream_transform<PC, PS, L, F>(
context: &mut PC,
session: &mut PS,
logger: &L,
+ error_relationship: &Relationship,
mut transform_fn: F,
-) -> Result<OnTriggerResult, ProcessError>
+) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
@@ -98,7 +125,7 @@ where
&ContextSessionFlowFileBundle<PC, PS>,
&mut dyn InputStream,
&mut dyn OutputStream,
- ) -> Result<TransformStreamResult, ProcessError>,
+ ) -> Result<TransformStreamResult, TransformError>,
{
if let Some(mut flow_file) = session.get() {
let simple_context = ContextSessionFlowFileBundle::new(context,
session, Some(&flow_file));
@@ -107,13 +134,13 @@ where
session.write_stream(&flow_file, |output_stream| {
let transformed = match transform_fn(&simple_context,
input_stream, output_stream) {
Ok(t) => t,
- Err(ProcessError::Route(route)) => {
- route.log(logger);
-
TransformStreamResult::route_without_changes_by_name(route.relationship)
- }
- Err(ProcessError::Fatal(e)) => {
- return Err(e);
- }
+ Err(err) => match err.into_route(error_relationship) {
+ Ok(route) => {
+ route.log(logger);
+
TransformStreamResult::route_without_changes_by_name(route.relationship)
+ }
+ Err(minifi_error) => return Err(minifi_error),
+ },
};
Ok((
@@ -149,17 +176,23 @@ where
&self,
context: &mut PC,
session: &mut PS,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
{
+ // Const defaults are lazy; this forces the check.
+ let () = Implementation::ERROR_RELATIONSHIP_DECLARED;
if let Some(ref scheduled_impl) = self.scheduled_impl {
- handle_stream_transform(context, session, &self.logger, |ctx,
input, output| {
- scheduled_impl.transform(ctx, input, output, &self.logger)
- })
+ handle_stream_transform(
+ context,
+ session,
+ &self.logger,
+ Implementation::ERROR_RELATIONSHIP,
+ |ctx, input, output| scheduled_impl.transform(ctx, input,
output, &self.logger),
+ )
} else {
- Err(MinifiError::UnscheduledProcessor.into())
+ Err(MinifiError::UnscheduledProcessor)
}
}
}
@@ -174,30 +207,105 @@ where
&mut self,
context: &mut PC,
session: &mut PS,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
{
+ // Const defaults are lazy; this forces the check.
+ let () = Implementation::ERROR_RELATIONSHIP_DECLARED;
if let Some(ref mut scheduled_impl) = self.scheduled_impl {
- handle_stream_transform(context, session, &self.logger, |ctx,
input, output| {
- scheduled_impl.transform(ctx, input, output, &self.logger)
- })
+ handle_stream_transform(
+ context,
+ session,
+ &self.logger,
+ Implementation::ERROR_RELATIONSHIP,
+ |ctx, input, output| scheduled_impl.transform(ctx, input,
output, &self.logger),
+ )
} else {
- Err(MinifiError::UnscheduledProcessor.into())
+ Err(MinifiError::UnscheduledProcessor)
}
}
}
#[cfg(test)]
mod tests {
- use crate::Relationship;
- use minifi_native::TransformStreamResult;
+ use super::*;
+ use crate::api::RawProcessor;
+ use crate::api::errors::TransformErrorExt;
+ use crate::{
+ MockFlowFile, MockLogger, MockProcessContext, MockProcessSession,
OutputAttribute,
+ ProcessorInputRequirement, PropertyDefinition,
+ };
const TEST_RELATIONSHIP: Relationship = Relationship {
name: "test",
description: "test desc",
};
+
+ const FAILURE: Relationship = Relationship {
+ name: "failure",
+ description: "test failure relationship",
+ };
+
+ struct PartialWriteThenError;
+ impl Schedule for PartialWriteThenError {
+ fn schedule<Ctx: GetProperty, L: Logger>(_c: &Ctx, _l: &L) ->
Result<Self, MinifiError> {
+ Ok(PartialWriteThenError)
+ }
+ }
+ impl ProcessorDefinition for PartialWriteThenError {
+ const DESCRIPTION: &'static str = "test stream transform";
+ const INPUT_REQUIREMENT: ProcessorInputRequirement =
ProcessorInputRequirement::Required;
+ const SUPPORTS_DYNAMIC_PROPERTIES: bool = false;
+ const SUPPORTS_DYNAMIC_RELATIONSHIPS: bool = false;
+ const OUTPUT_ATTRIBUTES: &'static [OutputAttribute] = &[];
+ const RELATIONSHIPS: &'static [Relationship] = &[TEST_RELATIONSHIP,
FAILURE];
+ const PROPERTIES: &'static [PropertyDefinition] = &[];
+ }
+ impl FlowFileStreamTransform for PartialWriteThenError {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
+ fn transform<Ctx: GetProperty + GetControllerService + GetAttribute,
LoggerImpl: Logger>(
+ &self,
+ _context: &Ctx,
+ _input_stream: &mut dyn InputStream,
+ output_stream: &mut dyn OutputStream,
+ _logger: &LoggerImpl,
+ ) -> Result<TransformStreamResult, TransformError> {
+ output_stream.write_all(b"PARTIAL").rollback_err()?;
+ Err(MinifiError::custom("boom"))?
+ }
+ }
+
+ #[test]
+ fn bubbled_error_routes_to_error_relationship_with_unchanged_content() {
+ let mut processor: Processor<
+ PartialWriteThenError,
+ FlowFileStreamTransformProcessorType,
+ MultiThreaded,
+ MockLogger,
+ > = Processor::new(MockLogger::new());
+ processor.scheduled_impl = Some(PartialWriteThenError);
+
+ let mut context = MockProcessContext::new();
+ let mut session = MockProcessSession::new();
+ session
+ .input_flow_files
+ .push(MockFlowFile::with_content(b"original"));
+
+ let result = MultiThreadedTrigger::trigger(&processor, &mut context,
&mut session);
+ assert_eq!(
+ result.expect("should route to failure, not roll back"),
+ OnTriggerResult::Ok
+ );
+
+ let transferred = session.transferred_flow_files.borrow();
+ assert_eq!(transferred.len(), 1);
+ assert_eq!(transferred[0].relationship, "failure");
+ assert_eq!(*transferred[0].flow_file.content.borrow(), b"original");
+ }
+
#[test]
fn test_with_attributes() {
let mut gen_ff = TransformStreamResult::new(&TEST_RELATIONSHIP);
diff --git
a/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_transform.rs
b/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_transform.rs
index 2479436e2..3799fb298 100644
---
a/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_transform.rs
+++
b/minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_transform.rs
@@ -10,7 +10,8 @@ use crate::api::property::{GetControllerService, GetProperty};
use crate::api::raw_processor::{MultiThreadedTrigger, SingleThreadedTrigger};
use crate::{
GetAttribute, LogLevel, Logger, MinifiError, MultiThreaded,
OnTriggerResult, ProcessContext,
- ProcessError, ProcessSession, Relationship, Schedule, SingleThreaded,
impl_with_attributes,
+ ProcessSession, ProcessorDefinition, Relationship, Schedule,
SingleThreaded, TransformError,
+ impl_with_attributes,
};
use minifi_native::{InputStream, trace};
@@ -87,7 +88,20 @@ impl<'a> TransformedFlowFile<'a> {
impl_with_attributes!(TransformedFlowFile<'a>);
-pub trait FlowFileTransform {
+pub trait FlowFileTransform: ProcessorDefinition {
+ /// Relationship that errors propagated with `?` (i.e.
[`TransformError::Bubbled`])
+ /// are routed to. Must be one of [`ProcessorDefinition::RELATIONSHIPS`].
+ ///
+ /// To roll the session back instead of routing, return
[`TransformError::Rollback`] or use
+ /// [`crate::TransformErrorExt::rollback_err`].
+ const ERROR_RELATIONSHIP: &'static Relationship;
+
+ /// Forced by the processor wrapper's trigger; implementors should not
override it.
+ const ERROR_RELATIONSHIP_DECLARED: () = assert!(
+ crate::api::relationship::is_declared(Self::RELATIONSHIPS,
Self::ERROR_RELATIONSHIP),
+ "ERROR_RELATIONSHIP must also be listed in this processor's
RELATIONSHIPS"
+ );
+
fn transform<
'a,
Context: GetProperty + GetControllerService + GetAttribute + GetId,
@@ -97,10 +111,23 @@ pub trait FlowFileTransform {
context: &Context,
input_stream: &'a mut dyn InputStream,
logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError>;
+ ) -> Result<TransformedFlowFile<'a>, TransformError>;
}
-pub trait MutFlowFileTransform {
+pub trait MutFlowFileTransform: ProcessorDefinition {
+ /// Relationship that errors propagated with `?` (i.e.
[`TransformError::Bubbled`])
+ /// are routed to. Must be one of [`ProcessorDefinition::RELATIONSHIPS`].
+ ///
+ /// To roll the session back instead of routing, return
[`TransformError::Rollback`] or use
+ /// [`crate::TransformErrorExt::rollback_err`].
+ const ERROR_RELATIONSHIP: &'static Relationship;
+
+ /// Forced by the processor wrapper's trigger; implementors should not
override it.
+ const ERROR_RELATIONSHIP_DECLARED: () = assert!(
+ crate::api::relationship::is_declared(Self::RELATIONSHIPS,
Self::ERROR_RELATIONSHIP),
+ "ERROR_RELATIONSHIP must also be listed in this processor's
RELATIONSHIPS"
+ );
+
fn transform<
'a,
Context: GetProperty + GetControllerService + GetAttribute,
@@ -110,7 +137,7 @@ pub trait MutFlowFileTransform {
context: &Context,
input_stream: &'a mut dyn InputStream,
logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError>;
+ ) -> Result<TransformedFlowFile<'a>, TransformError>;
}
pub struct FlowFileTransformProcessorType {}
@@ -119,8 +146,9 @@ fn handle_transform<PC, PS, L, F>(
context: &mut PC,
session: &mut PS,
logger: &L,
+ error_relationship: &Relationship,
mut transform_fn: F,
-) -> Result<OnTriggerResult, ProcessError>
+) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
@@ -128,7 +156,7 @@ where
F: for<'stream> FnMut(
&ContextSessionFlowFileBundle<'_, PC, PS>,
&'stream mut dyn InputStream,
- ) -> Result<TransformedFlowFile<'stream>, ProcessError>,
+ ) -> Result<TransformedFlowFile<'stream>, TransformError>,
{
if let Some(mut flow_file) = session.get() {
let simple_context = ContextSessionFlowFileBundle::new(context,
session, Some(&flow_file));
@@ -136,13 +164,13 @@ where
let (attrs_to_add, relationship) = session.read_stream(&flow_file,
|input_stream| {
let transformed = match transform_fn(&simple_context,
input_stream) {
Ok(transform_success) => transform_success,
- Err(ProcessError::Route(route)) => {
- route.log(logger);
-
TransformedFlowFile::route_without_changes_by_name(route.relationship)
- }
- Err(ProcessError::Fatal(e)) => {
- return Err(e);
- }
+ Err(err) => match err.into_route(error_relationship) {
+ Ok(route) => {
+ route.log(logger);
+
TransformedFlowFile::route_without_changes_by_name(route.relationship)
+ }
+ Err(minifi_error) => return Err(minifi_error),
+ },
};
trace!(logger, "{:?}", transformed);
@@ -184,17 +212,23 @@ where
&self,
context: &mut PC,
session: &mut PS,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
{
+ // Const defaults are lazy; this forces the check.
+ let () = Implementation::ERROR_RELATIONSHIP_DECLARED;
if let Some(ref scheduled_impl) = self.scheduled_impl {
- handle_transform(context, session, &self.logger, |ctx, input| {
- scheduled_impl.transform(ctx, input, &self.logger)
- })
+ handle_transform(
+ context,
+ session,
+ &self.logger,
+ Implementation::ERROR_RELATIONSHIP,
+ |ctx, input| scheduled_impl.transform(ctx, input,
&self.logger),
+ )
} else {
- Err(MinifiError::UnscheduledProcessor.into())
+ Err(MinifiError::UnscheduledProcessor)
}
}
}
@@ -209,17 +243,23 @@ where
&mut self,
context: &mut PC,
session: &mut PS,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>,
{
+ // Const defaults are lazy; this forces the check.
+ let () = Implementation::ERROR_RELATIONSHIP_DECLARED;
if let Some(ref mut scheduled_impl) = self.scheduled_impl {
- handle_transform(context, session, &self.logger, |ctx, input| {
- scheduled_impl.transform(ctx, input, &self.logger)
- })
+ handle_transform(
+ context,
+ session,
+ &self.logger,
+ Implementation::ERROR_RELATIONSHIP,
+ |ctx, input| scheduled_impl.transform(ctx, input,
&self.logger),
+ )
} else {
- Err(MinifiError::UnscheduledProcessor.into())
+ Err(MinifiError::UnscheduledProcessor)
}
}
}
@@ -231,7 +271,13 @@ mod tests {
use crate::api::raw_processor::MultiThreadedTrigger;
use crate::{
GetControllerService, GetId, MockFlowFile, MockLogger,
MockProcessContext,
- MockProcessSession, ProcessError, RouteErrorExt,
+ MockProcessSession, OutputAttribute, ProcessorInputRequirement,
PropertyDefinition,
+ TransformError,
+ };
+
+ const FAILURE: Relationship = Relationship {
+ name: "failure",
+ description: "test failure relationship",
};
struct RouteToFailure;
@@ -240,7 +286,18 @@ mod tests {
Ok(RouteToFailure)
}
}
+ impl ProcessorDefinition for RouteToFailure {
+ const DESCRIPTION: &'static str = "test transform";
+ const INPUT_REQUIREMENT: ProcessorInputRequirement =
ProcessorInputRequirement::Required;
+ const SUPPORTS_DYNAMIC_PROPERTIES: bool = false;
+ const SUPPORTS_DYNAMIC_RELATIONSHIPS: bool = false;
+ const OUTPUT_ATTRIBUTES: &'static [OutputAttribute] = &[];
+ const RELATIONSHIPS: &'static [Relationship] = &[FAILURE];
+ const PROPERTIES: &'static [PropertyDefinition] = &[];
+ }
impl FlowFileTransform for RouteToFailure {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<
'a,
Context: GetProperty + GetControllerService + GetAttribute + GetId,
@@ -250,22 +307,35 @@ mod tests {
_context: &Context,
_input_stream: &'a mut dyn InputStream,
_logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
let bad: Result<TransformedFlowFile<'a>, std::io::Error> =
Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"bad data",
));
- bad.route_err_to_failure()
+ // Propagated with `?`, so it becomes `Bubbled` and the wrapper
routes
+ // it to `ERROR_RELATIONSHIP`.
+ Ok(bad?)
}
}
- struct FatalTransform;
- impl Schedule for FatalTransform {
+ struct RollbackTransform;
+ impl Schedule for RollbackTransform {
fn schedule<Ctx: GetProperty, L: Logger>(_c: &Ctx, _l: &L) ->
Result<Self, MinifiError> {
- Ok(FatalTransform)
+ Ok(RollbackTransform)
}
}
- impl FlowFileTransform for FatalTransform {
+ impl ProcessorDefinition for RollbackTransform {
+ const DESCRIPTION: &'static str = "test transform";
+ const INPUT_REQUIREMENT: ProcessorInputRequirement =
ProcessorInputRequirement::Required;
+ const SUPPORTS_DYNAMIC_PROPERTIES: bool = false;
+ const SUPPORTS_DYNAMIC_RELATIONSHIPS: bool = false;
+ const OUTPUT_ATTRIBUTES: &'static [OutputAttribute] = &[];
+ const RELATIONSHIPS: &'static [Relationship] = &[FAILURE];
+ const PROPERTIES: &'static [PropertyDefinition] = &[];
+ }
+ impl FlowFileTransform for RollbackTransform {
+ const ERROR_RELATIONSHIP: &'static Relationship = &FAILURE;
+
fn transform<
'a,
Context: GetProperty + GetControllerService + GetAttribute + GetId,
@@ -275,8 +345,8 @@ mod tests {
_context: &Context,
_input_stream: &'a mut dyn InputStream,
_logger: &LoggerImpl,
- ) -> Result<TransformedFlowFile<'a>, ProcessError> {
- Err(ProcessError::Fatal(MinifiError::custom("real error")))
+ ) -> Result<TransformedFlowFile<'a>, TransformError> {
+ Err(TransformError::Rollback(MinifiError::custom("real error")))
}
}
@@ -313,24 +383,21 @@ mod tests {
}
#[test]
- fn fatal_error_propagates_and_transfers_nothing() {
+ fn rollback_error_propagates_and_transfers_nothing() {
let mut processor: Processor<
- FatalTransform,
+ RollbackTransform,
FlowFileTransformProcessorType,
MultiThreaded,
MockLogger,
> = Processor::new(MockLogger::new());
- processor.scheduled_impl = Some(FatalTransform);
+ processor.scheduled_impl = Some(RollbackTransform);
let mut context = MockProcessContext::new();
let mut session = seeded_session();
let result = MultiThreadedTrigger::trigger(&processor, &mut context,
&mut session);
- assert!(matches!(
- result,
- Err(ProcessError::Fatal(MinifiError::CustomError(_)))
- ));
+ assert!(matches!(result, Err(MinifiError::CustomError(_))));
assert_eq!(session.num_of_transferred_flow_files(), 0);
}
diff --git a/minifi_rust/minifi_native/src/api/raw_processor.rs
b/minifi_rust/minifi_native/src/api/raw_processor.rs
index a53506984..39aebdb15 100644
--- a/minifi_rust/minifi_native/src/api/raw_processor.rs
+++ b/minifi_rust/minifi_native/src/api/raw_processor.rs
@@ -3,7 +3,7 @@
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
use crate::api::errors::MinifiError;
-use crate::{LogLevel, Logger, ProcessContext, ProcessError, ProcessSession};
+use crate::{LogLevel, Logger, ProcessContext, ProcessSession};
pub enum ProcessorInputRequirement {
Required,
@@ -56,7 +56,7 @@ pub trait SingleThreadedTrigger: RawProcessor<Threading =
SingleThreaded> {
&mut self,
context: &mut PC,
session: &mut PS,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>;
@@ -67,7 +67,7 @@ pub trait MultiThreadedTrigger: RawProcessor<Threading =
MultiThreaded> {
&self,
context: &mut PC,
session: &mut PS,
- ) -> Result<OnTriggerResult, ProcessError>
+ ) -> Result<OnTriggerResult, MinifiError>
where
PC: ProcessContext,
PS: ProcessSession<FlowFile = PC::FlowFile>;
diff --git a/minifi_rust/minifi_native/src/api/relationship.rs
b/minifi_rust/minifi_native/src/api/relationship.rs
index c24065422..13bfd251a 100644
--- a/minifi_rust/minifi_native/src/api/relationship.rs
+++ b/minifi_rust/minifi_native/src/api/relationship.rs
@@ -15,3 +15,73 @@ impl Display for Relationship {
write!(f, "{}", self.name)
}
}
+
+const fn names_match(lhs: &str, rhs: &str) -> bool {
+ let (lhs, rhs) = (lhs.as_bytes(), rhs.as_bytes());
+ if lhs.len() != rhs.len() {
+ return false;
+ }
+ let mut i = 0;
+ while i < lhs.len() {
+ if lhs[i] != rhs[i] {
+ return false;
+ }
+ i += 1;
+ }
+ true
+}
+
+/// Whether `relationship` is one of `declared`, comparing by name.
+pub const fn is_declared(declared: &[Relationship], relationship:
&Relationship) -> bool {
+ let mut i = 0;
+ while i < declared.len() {
+ if names_match(declared[i].name, relationship.name) {
+ return true;
+ }
+ i += 1;
+ }
+ false
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const SUCCESS: Relationship = Relationship {
+ name: "success",
+ description: "",
+ };
+ const FAILURE: Relationship = Relationship {
+ name: "failure",
+ description: "",
+ };
+
+ #[test]
+ fn finds_a_declared_relationship() {
+ assert!(is_declared(&[SUCCESS, FAILURE], &FAILURE));
+ }
+
+ #[test]
+ fn rejects_an_undeclared_relationship() {
+ assert!(!is_declared(&[SUCCESS], &FAILURE));
+ assert!(!is_declared(&[], &FAILURE));
+ }
+
+ #[test]
+ fn matches_by_name_not_by_identity() {
+ const OTHER_FAILURE: Relationship = Relationship {
+ name: "failure",
+ description: "a different description",
+ };
+ assert!(is_declared(&[OTHER_FAILURE], &FAILURE));
+ }
+
+ #[test]
+ fn does_not_match_a_name_that_is_merely_a_prefix() {
+ const FAIL: Relationship = Relationship {
+ name: "fail",
+ description: "",
+ };
+ assert!(!is_declared(&[FAIL], &FAILURE));
+ }
+}
diff --git a/minifi_rust/minifi_native/src/c_ffi/c_ffi_processor_definition.rs
b/minifi_rust/minifi_native/src/c_ffi/c_ffi_processor_definition.rs
index 4f89d419b..f314edb4c 100644
--- a/minifi_rust/minifi_native/src/c_ffi/c_ffi_processor_definition.rs
+++ b/minifi_rust/minifi_native/src/c_ffi/c_ffi_processor_definition.rs
@@ -14,37 +14,26 @@ use crate::c_ffi::CffiLogger;
use crate::c_ffi::c_ffi_output_attribute::COutputAttributes;
use crate::c_ffi::c_ffi_property::CProperties;
use crate::{
- ComponentIdentifier, LogLevel, MultiThreaded, OutputAttribute, Processor,
ProcessorDefinition,
- PropertyDefinition, Schedule, SingleThreaded,
+ ComponentIdentifier, LogLevel, MinifiError, MultiThreaded,
OutputAttribute, Processor,
+ ProcessorDefinition, PropertyDefinition, Schedule, SingleThreaded,
};
-use crate::{OnTriggerResult, ProcessError, Relationship};
+use crate::{OnTriggerResult, Relationship};
use minifi_native_sys::*;
fn process_error_to_status<P: RawProcessor>(
processor: &P,
- result: Result<OnTriggerResult, ProcessError>,
+ result: Result<OnTriggerResult, MinifiError>,
) -> minifi_status {
match result {
Ok(OnTriggerResult::Ok) => minifi_status_MINIFI_STATUS_SUCCESS,
Ok(OnTriggerResult::Yield) =>
minifi_status_MINIFI_STATUS_PROCESSOR_YIELD,
- Err(ProcessError::Fatal(err)) => {
+ Err(err) => {
processor.log(
LogLevel::Error,
format_args!("Error during trigger {}", err),
);
err.to_status()
}
- Err(ProcessError::Route(route)) => {
- processor.log(
- LogLevel::Warn,
- format_args!(
- "Cannot route to '{}' at the top level (no current flow
file); \
- failing the trigger: {}",
- route.relationship, route.source
- ),
- );
- minifi_status_MINIFI_STATUS_UNKNOWN_ERROR
- }
}
}
diff --git a/minifi_rust/minifi_native/src/lib.rs
b/minifi_rust/minifi_native/src/lib.rs
index 16109a552..bb7b7acf6 100644
--- a/minifi_rust/minifi_native/src/lib.rs
+++ b/minifi_rust/minifi_native/src/lib.rs
@@ -8,7 +8,7 @@ pub mod c_ffi;
pub mod mock;
pub mod test_utils;
-pub use api::errors::{MinifiError, ProcessError, RouteError, RouteErrorExt};
+pub use api::errors::{MinifiError, RouteError, TransformError,
TransformErrorExt};
pub use api::component_definition_traits::{
ComponentIdentifier, ControllerServiceDefinition, ProcessorDefinition,
diff --git a/minifi_rust/minifi_native/src/mock/mock_process_session.rs
b/minifi_rust/minifi_native/src/mock/mock_process_session.rs
index 4e885b4a3..1686647e2 100644
--- a/minifi_rust/minifi_native/src/mock/mock_process_session.rs
+++ b/minifi_rust/minifi_native/src/mock/mock_process_session.rs
@@ -95,8 +95,10 @@ impl ProcessSession for MockProcessSession {
{
let mut new_content: Vec<u8> = Vec::new();
let mut cursor = std::io::Cursor::new(&mut new_content);
- let (r, _state) = callback(&mut cursor)?;
- *flow_file.content.borrow_mut() = new_content;
+ let (r, state) = callback(&mut cursor)?;
+ if state == IoState::Ok {
+ *flow_file.content.borrow_mut() = new_content;
+ }
Ok(r)
}
diff --git a/minifi_rust/minifi_native/src/test_utils.rs
b/minifi_rust/minifi_native/src/test_utils.rs
index 089860498..8e4e50d54 100644
--- a/minifi_rust/minifi_native/src/test_utils.rs
+++ b/minifi_rust/minifi_native/src/test_utils.rs
@@ -1,16 +1,48 @@
-use crate::{ProcessError, Relationship, TransformStreamResult};
+use crate::{
+ FlowFileStreamTransform, FlowFileTransform, Relationship, TransformError,
+ TransformStreamResult, TransformedFlowFile,
+};
-pub fn assert_routed_to(
- res: Result<TransformStreamResult, ProcessError>,
+/// Assert that the error of a [`FlowFileTransform::transform`] call ends up
routed to
+/// `expected_relationship`.
+///
+/// Resolves the error the way the processor wrapper does, so a bubbled error
counts as a route
+/// to `T`'s [`FlowFileTransform::ERROR_RELATIONSHIP`] and a rollback fails
the assertion.
+pub fn assert_routed_to<T: FlowFileTransform>(
+ res: Result<TransformedFlowFile<'_>, TransformError>,
+ expected_relationship: &Relationship,
+) {
+ assert_error_routed_to(
+ res.map(|_| ()),
+ T::ERROR_RELATIONSHIP,
+ expected_relationship,
+ );
+}
+
+/// [`assert_routed_to`] for [`FlowFileStreamTransform::transform`] results.
+pub fn assert_stream_routed_to<T: FlowFileStreamTransform>(
+ res: Result<TransformStreamResult, TransformError>,
+ expected_relationship: &Relationship,
+) {
+ assert_error_routed_to(
+ res.map(|_| ()),
+ T::ERROR_RELATIONSHIP,
+ expected_relationship,
+ );
+}
+
+fn assert_error_routed_to(
+ res: Result<(), TransformError>,
+ error_relationship: &Relationship,
expected_relationship: &Relationship,
) {
match res {
- Err(ProcessError::Route(route)) => {
- assert_eq!(route.relationship, expected_relationship.name)
- }
- Err(other) => {
- panic!("expected route to '{expected_relationship}', got fatal
error: {other:?}")
- }
+ Err(err) => match err.into_route(error_relationship) {
+ Ok(route) => assert_eq!(route.relationship,
expected_relationship.name),
+ Err(rollback) => {
+ panic!("expected route to '{expected_relationship}', got
rollback: {rollback:?}")
+ }
+ },
Ok(_) => panic!("expected route to '{expected_relationship}', got Ok"),
}
}