martinzink commented on code in PR #2246:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2246#discussion_r3956287219
##########
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:
The error messages are either a string literal or something dynamic i used
cows so we can avoid using Strings but still support error messages with
dynamic info
--
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]