Class ExceptionHandler<C>

java.lang.Object
org.apache.nifi.processor.util.pattern.ExceptionHandler<C>

public class ExceptionHandler<C> extends Object

ExceptionHandler provides a structured Exception handling logic composed by reusable partial functions.

Benefits of using ExceptionHandler:

  • Externalized error handling code which provides cleaner program only focusing on the expected path.
  • Classify specific Exceptions into ErrorTypes, consolidated error handling based on error type.
  • Context aware error handling, RollbackOnFailure for instance.
    • Field Details

    • Constructor Details

      • ExceptionHandler

        public ExceptionHandler()
    • Method Details

      • mapException

        public void mapException(Function<Exception,ErrorTypes> mapException)
        Specify a function that maps an Exception to certain ErrorType.
      • adjustError

        public void adjustError(BiFunction<C,ErrorTypes,ErrorTypes.Result> adjustError)

        Specify a function that adjust ErrorType based on a function context.

        For example, RollbackOnFailure.createAdjustError(ComponentLog) decides whether a process session should rollback or transfer input to failure or retry.

      • onError

        public void onError(ExceptionHandler.OnError<C,?> onError)

        Specify a default OnError function that will be called if one is not explicitly specified when execute(Object, Object, Procedure) is called.

      • execute

        public <I> boolean execute(C context, I input, ExceptionHandler.Procedure<I> procedure) throws org.apache.nifi.processor.exception.ProcessException, DiscontinuedException

        Executes specified procedure function with the input.

        Default OnError function will be called when an exception is thrown.

        Parameters:
        context - function context
        input - input for procedure
        procedure - a function that does something with the input
        Returns:
        True if the procedure finished without issue. False if procedure threw an Exception but it was handled by ExceptionHandler.OnError.
        Throws:
        org.apache.nifi.processor.exception.ProcessException - Thrown if the exception was not handled by ExceptionHandler.OnError
        DiscontinuedException - Indicating the exception was handled by ExceptionHandler.OnError but process should stop immediately without processing any further input
      • execute

        public <I> boolean execute(C context, I input, ExceptionHandler.Procedure<I> procedure, ExceptionHandler.OnError<C,I> onError) throws org.apache.nifi.processor.exception.ProcessException, DiscontinuedException

        Executes specified procedure function with the input.

        Parameters:
        context - function context
        input - input for procedure
        procedure - a function that does something with the input
        onError - specify ExceptionHandler.OnError function for this execution
        Returns:
        True if the procedure finished without issue. False if procedure threw an Exception but it was handled by ExceptionHandler.OnError.
        Throws:
        org.apache.nifi.processor.exception.ProcessException - Thrown if the exception was not handled by ExceptionHandler.OnError
        DiscontinuedException - Indicating the exception was handled by ExceptionHandler.OnError but process should stop immediately without processing any further input
      • penalize

        private static org.apache.nifi.flowfile.FlowFile penalize(org.apache.nifi.processor.ProcessContext context, org.apache.nifi.processor.ProcessSession session, org.apache.nifi.flowfile.FlowFile flowFile, ErrorTypes.Penalty penalty)
      • createOnError

        public static <C> ExceptionHandler.OnError<C,org.apache.nifi.flowfile.FlowFile> createOnError(org.apache.nifi.processor.ProcessContext context, org.apache.nifi.processor.ProcessSession session, RoutingResult routingResult, org.apache.nifi.processor.Relationship relFailure, org.apache.nifi.processor.Relationship relRetry)
        Create a ExceptionHandler.OnError function instance that routes input based on ErrorTypes.Result destination and penalty.
        Parameters:
        context - process context is used to yield a processor
        session - process session is used to penalize a FlowFile
        routingResult - input FlowFile will be routed to a destination relationship in this RoutingResult
        relFailure - specify failure relationship of a processor
        relRetry - specify retry relationship of a processor
        Returns:
        composed function
      • createOnGroupError

        public static <C, I extends PartialFunctions.FlowFileGroup> ExceptionHandler.OnError<C,I> createOnGroupError(org.apache.nifi.processor.ProcessContext context, org.apache.nifi.processor.ProcessSession session, RoutingResult routingResult, org.apache.nifi.processor.Relationship relFailure, org.apache.nifi.processor.Relationship relRetry)
        Parameters:
        context - process context is used to yield a processor
        session - process session is used to penalize FlowFiles
        routingResult - input FlowFiles will be routed to a destination relationship in this RoutingResult
        relFailure - specify failure relationship of a processor
        relRetry - specify retry relationship of a processor
        Returns:
        composed function