Package no.digipost
Class DiggExceptions
- java.lang.Object
-
- no.digipost.DiggExceptions
-
public final class DiggExceptions extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static Consumer<Exception>rethrowAnyExceptionThis consumer rethrows any givenExceptionas an uncheckedRuntimeException.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T,R>
RapplyUnchecked(ThrowingFunction<T,R,? extends Throwable> function, T argument)Immediatelyapplythe givenfunctionwith the givenargument, and if needed, convert any thrown exceptions to unckecked.static RuntimeExceptionasUnchecked(Throwable t)Utility for acquiring aRuntimeExceptionfrom anyThrowable.static RuntimeExceptionasUnchecked(Throwable t, String message)Utility for acquiring aRuntimeExceptionwith a custom message from anyThrowable.static <X extends Throwable>
RuntimeExceptionasUnchecked(X t, Function<? super X,String> messageCreator)Utility for acquiring aRuntimeExceptionwith a custom message from anyThrowable.static Stream<Throwable>causalChainOf(Throwable t)static StringexceptionNameAndMessage(Throwable t)Generate a concise description of an exception/throwable containing the"friendly name"of the exception's type, and itsmessage.static <T> TgetUnchecked(ThrowingSupplier<T,? extends Throwable> supplier)Immediatelygeta result from the givensupplier, and if needed, convert any thrown exceptions to unckecked.static <T,U,X extends Throwable>
ThrowingBiConsumer<T,U,X>mayThrow(ThrowingBiConsumer<T,U,X> consumer)Convenience to acquire aThrowingBiConsumer-reference from a lambda expression.static <T,U,R,X extends Throwable>
ThrowingBiFunction<T,U,R,X>mayThrow(ThrowingBiFunction<T,U,R,X> bifunction)Convenience to acquire aThrowingBiFunction-reference from a lambda expression.static <T,X extends Throwable>
ThrowingConsumer<T,X>mayThrow(ThrowingConsumer<T,X> consumer)Convenience to acquire aThrowingConsumer-reference from a lambda expression.static <T,R,X extends Throwable>
ThrowingFunction<T,R,X>mayThrow(ThrowingFunction<T,R,X> function)Convenience to acquire aThrowingFunction-reference from a lambda expression.static <T,X extends Throwable>
ThrowingRunnable<X>mayThrow(ThrowingRunnable<X> runnable)Convenience to acquire aThrowingRunnable-reference from a lambda expression.static <T,X extends Throwable>
ThrowingSupplier<T,X>mayThrow(ThrowingSupplier<T,X> supplier)Convenience to acquire aThrowingSupplier-reference from a lambda expression.static <T extends Throwable>
Consumer<T>rethrow(Function<T,? extends RuntimeException> createUnchecked)Create a exception handler (Consumer) which simply rethrows given exceptions.static voidrunUnchecked(ThrowingRunnable<? extends Throwable> runnable)Immediatelyrunthe givenrunnable, and if needed, convert any thrown exceptions to unckecked.
-
-
-
Field Detail
-
rethrowAnyException
public static final Consumer<Exception> rethrowAnyException
This consumer rethrows any givenExceptionas an uncheckedRuntimeException.
-
-
Method Detail
-
causalChainOf
public static Stream<Throwable> causalChainOf(Throwable t)
- Parameters:
t- the Throwable to get the causal chain from.- Returns:
- the entire chain of causes, including the given Throwable.
-
exceptionNameAndMessage
public static String exceptionNameAndMessage(Throwable t)
Generate a concise description of an exception/throwable containing the"friendly name"of the exception's type, and itsmessage.- Parameters:
t- the exception/throwable- Returns:
- the description
-
asUnchecked
public static RuntimeException asUnchecked(Throwable t)
Utility for acquiring aRuntimeExceptionfrom anyThrowable. This method is appropriate to use when you have caught an exception which you have no ability to handle, and you need to just throw it from a context where you are not allowed to.If you want to add more context to the thrown exception (which you probably should), consider using
asUnchecked(Throwable, String)orasUnchecked(Throwable, Function).- Parameters:
t- the exception (Throwable)- Returns:
- a new
RuntimeExceptionwhich has the given exception as itscause, ortitself casted toRuntimeExceptionif possible
-
asUnchecked
public static <X extends Throwable> RuntimeException asUnchecked(X t, Function<? super X,String> messageCreator)
Utility for acquiring aRuntimeExceptionwith a custom message from anyThrowable. The result from this method will always be a new exception instance.- Type Parameters:
X- the type of the given exception- Parameters:
t- the exception (Throwable)messageCreator- a function which returns the message for the new exception- Returns:
- a new
RuntimeExceptionwhich has the result from themessageCreatorfunction as itsmessage, and the given exception as itscause
-
asUnchecked
public static RuntimeException asUnchecked(Throwable t, String message)
Utility for acquiring aRuntimeExceptionwith a custom message from anyThrowable. The result from this method will always be a new exception instance.- Parameters:
t- the exception (Throwable)message- the message for the new exception- Returns:
- a new
RuntimeExceptionwhich has the givenmessage, and the given exception as itscause
-
applyUnchecked
public static <T,R> R applyUnchecked(ThrowingFunction<T,R,? extends Throwable> function, T argument)
Immediatelyapplythe givenfunctionwith the givenargument, and if needed, convert any thrown exceptions to unckecked.- Parameters:
function- TheThrowingFunction.argument- The argument to pass toThrowingFunction.apply(Object)- Returns:
- the result.
-
getUnchecked
public static <T> T getUnchecked(ThrowingSupplier<T,? extends Throwable> supplier)
Immediatelygeta result from the givensupplier, and if needed, convert any thrown exceptions to unckecked.- Parameters:
supplier- TheThrowingSupplier.- Returns:
- the result.
-
runUnchecked
public static void runUnchecked(ThrowingRunnable<? extends Throwable> runnable)
Immediatelyrunthe givenrunnable, and if needed, convert any thrown exceptions to unckecked.- Parameters:
runnable- TheThrowingRunnable.
-
mayThrow
public static final <T,R,X extends Throwable> ThrowingFunction<T,R,X> mayThrow(ThrowingFunction<T,R,X> function)
Convenience to acquire aThrowingFunction-reference from a lambda expression.
-
mayThrow
public static final <T,U,R,X extends Throwable> ThrowingBiFunction<T,U,R,X> mayThrow(ThrowingBiFunction<T,U,R,X> bifunction)
Convenience to acquire aThrowingBiFunction-reference from a lambda expression.
-
mayThrow
public static final <T,X extends Throwable> ThrowingSupplier<T,X> mayThrow(ThrowingSupplier<T,X> supplier)
Convenience to acquire aThrowingSupplier-reference from a lambda expression.
-
mayThrow
public static final <T,X extends Throwable> ThrowingConsumer<T,X> mayThrow(ThrowingConsumer<T,X> consumer)
Convenience to acquire aThrowingConsumer-reference from a lambda expression.
-
mayThrow
public static final <T,U,X extends Throwable> ThrowingBiConsumer<T,U,X> mayThrow(ThrowingBiConsumer<T,U,X> consumer)
Convenience to acquire aThrowingBiConsumer-reference from a lambda expression.
-
mayThrow
public static final <T,X extends Throwable> ThrowingRunnable<X> mayThrow(ThrowingRunnable<X> runnable)
Convenience to acquire aThrowingRunnable-reference from a lambda expression.
-
rethrow
public static final <T extends Throwable> Consumer<T> rethrow(Function<T,? extends RuntimeException> createUnchecked)
Create a exception handler (Consumer) which simply rethrows given exceptions.- Parameters:
createUnchecked- a function to convert a (typically) checked exception toRuntimeException.- Returns:
- the
Consumerwhich rethrows any given exception.
-
-