szaszm commented on code in PR #2246:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2246#discussion_r3928224222
##########
minifi_rust/minifi_native/src/api/processor_wrappers/flow_file_transform.rs:
##########
@@ -24,50 +23,68 @@ use crate::api::property::{GetControllerService,
GetProperty};
use crate::api::raw_processor::{MultiThreadedTrigger, SingleThreadedTrigger};
use crate::{
GetAttribute, LogLevel, Logger, MinifiError, MultiThreaded,
OnTriggerResult, ProcessContext,
- ProcessSession, Relationship, Schedule, SingleThreaded, info,
+ ProcessError, ProcessSession, Relationship, Schedule, SingleThreaded,
impl_with_attributes,
+ info,
};
-use std::collections::HashMap;
+
+use minifi_native::InputStream;
+use std::borrow::Cow;
+
+pub type FlowFileAttribute = (Cow<'static, str>, Cow<'static, str>);
Review Comment:
Why Cow? Minifi copies the strings AFAIK, so there is no need for the rust
bindings to own them.
##########
minifi_rust/minifi_native/src/api/errors.rs:
##########
@@ -23,87 +24,176 @@ use std::fmt;
use std::num::{NonZeroU32, ParseFloatError, ParseIntError};
use std::str::ParseBoolError;
-#[derive(Debug, Clone)]
-pub enum ParseError {
- Strum(strum::ParseError),
- Bool(ParseBoolError),
- Int(ParseIntError),
- Duration(humantime::DurationError),
- Size(byte_unit::ParseError),
- Nul(NulError),
- Float(ParseFloatError),
- Other,
-}
-
#[derive(Debug)]
-pub enum MinifiError {
- UnknownError,
- StatusError((Cow<'static, str>, NonZeroU32)),
- MissingRequiredAttribute(Cow<'static, str>),
- MissingRequiredProperty(Cow<'static, str>),
- ControllerServiceError(Cow<'static, str>),
- ValidationError(Cow<'static, str>),
- ScheduleError(Cow<'static, str>),
- TriggerError(Cow<'static, str>),
- Parse(ParseError),
- MissingFlowFileError,
- IoError(std::io::Error),
+pub struct RouteError {
+ pub relationship: &'static str,
+ pub source: Box<dyn Error + Send + Sync + 'static>,
+ pub log_level: LogLevel,
}
-impl From<std::io::Error> for MinifiError {
- fn from(error: std::io::Error) -> Self {
- MinifiError::IoError(error)
+impl RouteError {
+ pub(crate) fn log<L: crate::Logger>(&self, logger: &L) {
+ logger.log(
+ self.log_level,
+ format_args!(
+ "Routing flow file to '{}': {}",
+ self.relationship, self.source
+ ),
+ );
}
}
-impl From<strum::ParseError> for MinifiError {
- fn from(err: strum::ParseError) -> Self {
- MinifiError::Parse(ParseError::Strum(err))
+impl fmt::Display for RouteError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "route to '{}' due to: {}",
+ self.relationship, self.source
+ )
}
}
-impl From<ParseBoolError> for MinifiError {
- fn from(err: ParseBoolError) -> Self {
- MinifiError::Parse(ParseError::Bool(err))
- }
+impl Error for RouteError {}
+
+#[derive(Debug)]
+pub enum ProcessError {
+ Route(RouteError),
+ Fatal(MinifiError),
}
-impl From<ParseIntError> for MinifiError {
- fn from(err: ParseIntError) -> Self {
- MinifiError::Parse(ParseError::Int(err))
+impl From<RouteError> for ProcessError {
+ fn from(err: RouteError) -> Self {
+ ProcessError::Route(err)
}
}
-impl From<humantime::DurationError> for MinifiError {
- fn from(err: humantime::DurationError) -> Self {
- MinifiError::Parse(ParseError::Duration(err))
+impl From<MinifiError> for ProcessError {
+ fn from(err: MinifiError) -> Self {
+ ProcessError::Fatal(err)
}
}
-impl From<byte_unit::ParseError> for MinifiError {
- fn from(err: byte_unit::ParseError) -> Self {
- MinifiError::Parse(ParseError::Size(err))
+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 {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ ProcessError::Route(err) => write!(f, "{}", err),
+ ProcessError::Fatal(err) => write!(f, "{}", err),
+ }
}
}
-impl From<NulError> for MinifiError {
- fn from(err: NulError) -> Self {
- MinifiError::Parse(ParseError::Nul(err))
+impl Error for ProcessError {}
+
+pub trait RouteErrorExt<T> {
+ fn route_err(self, rel: &Relationship, level: LogLevel) -> Result<T,
ProcessError>;
+
+ fn route_to(self, relationship: &'static str, level: LogLevel) ->
Result<T, ProcessError>;
+
+ fn route_err_to_failure(self) -> Result<T, ProcessError>;
+}
+
+impl<T, E> RouteErrorExt<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> {
+ self.route_to(rel.name, level)
}
+
+ fn route_to(self, relationship_name: &'static str, level: LogLevel) ->
Result<T, ProcessError> {
+ self.map_err(|e| {
+ ProcessError::Route(RouteError {
+ relationship: relationship_name,
+ source: e.into(),
+ log_level: level,
+ })
+ })
+ }
+
+ fn route_err_to_failure(self) -> Result<T, ProcessError> {
+ self.route_to("failure", LogLevel::Warn)
+ }
+}
+
+#[derive(Debug)]
+pub enum MinifiError {
+ UnknownError,
+ StatusError((Cow<'static, str>, NonZeroU32)),
+ MissingRequiredAttribute(Cow<'static, str>),
+ MissingRequiredProperty(Cow<'static, str>),
+ UnscheduledProcessor,
+ ValidationError(Cow<'static, str>),
+ CustomError(Cow<'static, str>),
Review Comment:
Why the Cows? (herd? 😄)
##########
minifi_rust/minifi_native/src/api/processor_wrappers/utils/with_attributes.rs:
##########
@@ -0,0 +1,50 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+/// Adds implementation with_attribute and with_attribute(s)
+/// used by TransformedFlowFile<'a>, GeneratedFlowFile<'a>,
TransformStreamResult
+/// extracted to macro to avoid code duplication
+#[macro_export]
+macro_rules! impl_with_attributes {
+ ($name:ident $(<$lt:lifetime>)?) => {
+ impl $(<$lt>)? $name $(<$lt>)? {
+ #[must_use]
+ pub fn with_attribute(
+ mut self,
+ key: impl Into<std::borrow::Cow<'static, str>>,
+ value: impl Into<std::borrow::Cow<'static, str>>,
Review Comment:
why Cow?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]