Class ExceptionUtils

java.lang.Object
com.bonitasoft.processbuilder.extension.ExceptionUtils

public final class ExceptionUtils extends Object
A utility class for handling logging and exception throwing. This class centralizes error management to ensure consistency.
  • Method Details

    • logAndThrow

      public static <T extends Exception> T logAndThrow(Supplier<T> exceptionSupplier, String format, Object... args) throws T
      Logs a formatted error message and then throws a new instance of a specified exception. This method is a safe and generic way to handle exceptions by using a Supplier, which avoids the complexities and potential runtime errors of Reflection. It leverages the logging framework's ability to format messages, which is more efficient because the message is only constructed if the log level is active.
      Type Parameters:
      T - The type of the exception to be thrown. This must be a subclass of Exception.
      Parameters:
      exceptionSupplier - A Supplier that provides an instance of the exception to be thrown. The supplier's get() method should create and return a new exception instance, typically via a lambda or a constructor reference (e.g., IllegalArgumentException::new).
      format - A parameterized message format string compatible with the logging framework's formatters (e.g., using `{}`). This format string will be logged to the error level.
      args - A variable-length argument list of objects to be substituted into the format string. These objects correspond to the `{}` placeholders in the format string.
      Returns:
      The exception instance provided by the supplier.
      Throws:
      T - The exception instance provided by the exceptionSupplier.
    • logAndThrowWithMessage

      public static <T extends Exception> void logAndThrowWithMessage(Function<String,T> exceptionFunction, String format, Object... args) throws T
      Utility method to safely log an error message and subsequently throw a new exception instance containing that same formatted message.

      This ensures that the exception thrown, even if generic (like RuntimeException), always contains the context provided in the 'format' string, which is crucial for troubleshooting in environments that might discard the original cause's details.

      Type Parameters:
      T - The type of the Exception to be thrown, which must extend Exception.
      Parameters:
      exceptionFunction - A function that accepts the final formatted message (String) and returns a new exception of type T (e.g., message -> new MyCustomException(message)).
      format - The format string for the error message (compatible with String.format).
      args - Arguments referenced by the format specifiers in the format string.
      Throws:
      T - The exception created by the provided function.
    • logAndThrowWithClass

      public static <T extends Exception> void logAndThrowWithClass(Class<T> exceptionClass, String format, Object... args) throws T
      Logs an error message and then securely throws a new instance of the specified exception class.

      This method uses Java Reflection to reliably instantiate the exception by explicitly calling the constructor that accepts a single String argument (the message). This approach avoids the 'Ambiguous method overloading' errors often encountered in Groovy environments when using dynamic closures or constructors with null parameters.

      The method ensures that the thrown exception always contains the detailed, formatted error message, preventing common 'No message' issues in the calling environment.

      Type Parameters:
      T - The type of Exception to be thrown, which must extend Exception.
      Parameters:
      exceptionClass - The Class object of the exception to instantiate (e.g., IllegalArgumentException.class).
      format - The format string for the error message, compliant with String.format(String, Object...).
      args - The arguments referenced by the format specifiers in the format string.
      Throws:
      T - The instantiated and thrown exception of type T, containing the formatted message.
      RuntimeException - if the specified exception class does not have a public constructor that accepts a single String argument, or if instantiation fails due to reflection errors.