Class ErrorKit

java.lang.Object
dev.comfast.util.ErrorKit

public class ErrorKit extends Object
ErrorKit is a set of useful functions for error handling.
  • Constructor Details

    • ErrorKit

      public ErrorKit()
  • Method Details

    • _fail

      public static Supplier<RuntimeException> _fail(String errorMsg, Object... msgParams)
      Short supplier for RuntimeException, fits to Streams/Optionals

      Usage example:

      
       found = someStream.findFirst().orElseThrow(_fail("Not found '%s'", someParam))
       value = someOptional.orElseThrow(_fail("Oh no !"))
       
      Parameters:
      errorMsg - message if error happens
      msgParams - format params for errorMsg
      Returns:
      Supplier for RuntimeException, fits to Streams/Optionals
    • rethrow

      public static <T> T rethrow(Supplier<T> getter, String errorMsg, Object... msgParams)
      Similarly to lombok SneakyThrows, wraps any checked / unchecked Exception into RuntimeException

      Adds additional error message and original Exception as cause.

      Usage example:
      
       rethrow(() -> somethingThrowsCheckedException(), "Something failed");
       rethrow(() -> somethingCanFail(), "Something failed, see: '%s'", "some param");
       
      Type Parameters:
      T - function return type
      Parameters:
      getter - wrapped function
      errorMsg - message if error happen
      msgParams - format params for errorMsg
      Returns:
      function result