public final class EhSupport
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.util.function.Consumer<java.lang.Throwable> |
defaultAbortHandler |
protected static java.util.function.Consumer<java.lang.Throwable> |
throwHandler
This is the rethrow handler to be used by the catch blocks below.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
abort(java.lang.String message)
Method that takes a message that will be used by the fatal exception handler wrapped in a
IllegalStateException. |
static void |
abort(java.lang.String message,
java.lang.Exception cause)
Method that takes a message that will be used by the fatal exception handler wrapped in a
IllegalStateException and an Exception to be added as the Cause. |
static <E> E |
checkResult(Result<E> result)
Checks if Result object is successful and returns the actual result value.
|
static void |
closeQuietlyOrException(java.lang.Throwable throwable,
java.io.Closeable closeable)
Convenience wrapper for
closeQuietlyOrFatal(Throwable, Object, ThrowsConsumer) for objects which
implement Closeable interface |
static <T> void |
closeQuietlyOrFatal(java.lang.Throwable throwable,
T closeable,
ThrowsConsumer<T> closeCallback)
Convenience wrapper for
tryCloseQuietly(Throwable, Object, ThrowsConsumer) |
static java.lang.Throwable |
enhance(java.lang.Throwable exception)
Enhance an exception if it is not already enhanced and return it.
|
static java.lang.Throwable |
enhance(java.lang.Throwable exception,
java.lang.String name,
java.lang.Object value)
Enhance an exception if it is not already enhanced, add a new property and then return it.
|
static void |
enhanceAndRethrow(ThrowsRunnable callback,
java.util.function.Consumer<BaseException> enhanceCallback)
If an exception if thrown from a simple callback it gets enhanced by the specified callback and then rethrown.
|
static void |
enhanceAndRethrow(ThrowsRunnable callback,
java.lang.String name,
java.lang.Object value)
If an exception if thrown from a simple callback it gets enhanced with the specified property and rethrown.
|
static <R> R |
enhanceAndRethrowFn(ThrowsSupplier<R> callback,
java.util.function.Consumer<BaseException> enhanceCallback)
If an exception if thrown from a functional callback it gets enhanced by the specified callback and then
rethrown.
|
static <R> R |
enhanceAndRethrowFn(ThrowsSupplier<R> callback,
java.lang.String name,
java.lang.Object value)
If an exception if thrown from a functional callback it gets enhanced with the specified property and rethrown.
|
static void |
enhancedFinally(ThrowsRunnable callback,
java.util.function.Consumer<java.lang.Throwable> handler)
Same as a normal finally block except that the handler will receive the current exception info if one has been
thrown.
|
static <R> R |
enhancedFinallyFn(ThrowsSupplier<R> callback,
java.util.function.Consumer<java.lang.Throwable> handler)
Same functionality as
enhancedFinally(ThrowsRunnable, Consumer) except that it takes a supplier callback
which returns value and then this helper returns the same value. |
static void |
ensure(boolean expectedCondition,
java.lang.String message,
java.lang.Object... args)
Checks that the expected condition is satisfied otherwise will throw
IllegalStateException. |
static void |
ensureArg(boolean expectedCondition,
java.lang.String message,
java.lang.Object... args)
Checks that the expected parameter condition is satisfied or will throw
IllegalArgumentException. |
static void |
ensureOrFatal(boolean expectedCondition,
java.lang.Exception cause,
java.lang.String message,
java.lang.Object... args)
Checks that the expected condition is satisfied otherwise will ultimately execute a fatal exception handler.
|
static void |
ensureOrFatal(boolean expectedCondition,
java.lang.String message,
java.lang.Object... args)
Checks that the expected condition is satisfied otherwise will ultimately execute a fatal exception handler.
|
static void |
fatalOnException(ThrowsRunnable callback)
Executes a callback that is expected to not throw an exception and if exception is thrown a fatal handler is
invoked (which by default will throw a non-recoverable exception).
|
static <R> R |
fatalOnExceptionFn(ThrowsSupplier<R> callback)
Executes a supplier callback that is expected to not throw an exception and if exception is thrown a fatal
handler is invoked (which by default will throw a non-recoverable exception).
|
static java.util.function.Consumer<java.lang.Throwable> |
getAbortHandler()
Returns the current fatal no-throw exception handler for the EH library.
|
static <R> R |
getExceptionDetails(java.lang.Throwable exception,
java.util.function.Function<java.lang.StringBuffer,R> callback)
Obtains the exception details dump, including all properties, for an exception.
|
static java.lang.String |
getExceptionDetailsAsString(java.lang.Throwable exception)
Obtains the exception details dump, including all properties, for an exception as string.
|
static BaseException |
getPropertiesException(java.lang.Throwable exception)
Obtains a properties exception associated with this exception.
|
static java.time.Instant |
getTimeThrown(java.lang.Throwable exception)
Obtains the time thrown / enhanced property of an exception.
|
static void |
printExceptionDetails(java.lang.Throwable exception,
java.io.PrintStream printStream)
Obtains the exception details dump, including all properties, for an exception into a print stream.
|
static void |
printExceptionDetails(java.lang.Throwable exception,
java.io.PrintWriter printWriter)
Obtains the exception details dump, including all properties, for an exception into a print writer.
|
static void |
propagate(ThrowsRunnable callback)
Executes a callback that throws exceptions and if exceptions are raised they get propagated as is, but all
exceptions are propagated as unchecked exceptions.
|
static <R> R |
propagateFn(ThrowsSupplier<R> callback)
Executes a supplier callback that throws exceptions and if exceptions are raised they get propagated as is, but
all exceptions are propagated as unchecked exceptions.
|
static void |
putProperty(java.lang.Throwable exception,
java.lang.String name,
java.lang.Object value)
Associate a property to an exception.
|
static void |
rethrow(java.lang.Throwable throwable)
Rethrow an
Throwable as is, but making it unchecked. |
protected static <T extends java.lang.Throwable> |
rethrowInternal(java.lang.Throwable throwable) |
static void |
setAbortHandler(java.util.function.Consumer<java.lang.Throwable> abortHandler)
Sets a new fatal no-throw exception handler for the EH library code
|
static void |
suppressExceptionsOrFatal(java.lang.Throwable throwable,
ThrowsRunnable callback)
Convenience wrapper for
trySuppressExceptions(Throwable, ThrowsRunnable) |
static void |
throwEnhanced(java.lang.Throwable exception)
Enhance an exception if it is not already enhanced and rethrow it.
|
static void |
throwEnhanced(java.lang.Throwable exception,
java.lang.String name,
java.lang.Object value)
Enhance an exception if it is not already enhanced, add a new property and then rethrow it.
|
static void |
tryCloseQuietly(java.lang.Throwable throwable,
java.io.Closeable closeable)
Convenience wrapper for
tryCloseQuietly(Throwable, Object, ThrowsConsumer) for objects which implement
Closeable interface |
static <T> void |
tryCloseQuietly(java.lang.Throwable throwable,
T closeable,
ThrowsConsumer<T> closeCallback)
Tries to close quietly a closable object of type T and throws if the exception thrown by close cannot be
suppressed (i.e.
|
static <E> E |
tryGetProperty(java.lang.Throwable exception,
java.lang.String name)
Tries to obtain the value of a property from an exception (if already associated).
|
static java.lang.String |
tryGetPropertyAsString(java.lang.Throwable exception,
java.lang.String name)
Tries to obtain the value of a property as string from an exception (if already associated).
|
static void |
trySuppressExceptions(java.lang.Throwable throwable,
ThrowsRunnable callback)
Executes a callback that throws and suppresses all exceptions if possible (i.e.
|
public static final java.util.function.Consumer<java.lang.Throwable> defaultAbortHandler
protected static java.util.function.Consumer<java.lang.Throwable> throwHandler
The derived class EhSupport can change it to something different which can allow more enhanced
behavior to be provided there (e.g. invoking throwEnhanced)
public static BaseException getPropertiesException(java.lang.Throwable exception)
exception - The exception to get the properties exception forpublic static java.lang.Throwable enhance(java.lang.Throwable exception)
exception - The exception to be enhancedpublic static java.lang.Throwable enhance(java.lang.Throwable exception,
java.lang.String name,
java.lang.Object value)
exception - The exception to be enhancedname - The name of the propertyvalue - The value of the propertypublic static void throwEnhanced(java.lang.Throwable exception)
exception - The exception to be enhancedpublic static void throwEnhanced(java.lang.Throwable exception,
java.lang.String name,
java.lang.Object value)
exception - The exception to be enhancedname - The name of the propertyvalue - The value of the propertypublic static void enhanceAndRethrow(ThrowsRunnable callback, java.util.function.Consumer<BaseException> enhanceCallback)
callback - The simple callback to guardenhanceCallback - The callback to be called to enhance the exception (if thrown)public static void enhanceAndRethrow(ThrowsRunnable callback, java.lang.String name, java.lang.Object value)
callback - The simple callback to guardname - The name of the propertyvalue - The value of the propertypublic static <R> R enhanceAndRethrowFn(ThrowsSupplier<R> callback, java.util.function.Consumer<BaseException> enhanceCallback)
R - The return type of the functional callbackcallback - The functional callback to guardenhanceCallback - The callback to be called to enhance the exception (if thrown)public static <R> R enhanceAndRethrowFn(ThrowsSupplier<R> callback, java.lang.String name, java.lang.Object value)
R - The return type of the functional callbackcallback - The functional callback to guardname - The name of the propertyvalue - The value of the propertypublic static java.time.Instant getTimeThrown(java.lang.Throwable exception)
exception - The exception to obtain the time thrown / enhanced propertypublic static void putProperty(java.lang.Throwable exception,
java.lang.String name,
java.lang.Object value)
exception - The exception to associate a property withname - The name of the propertyvalue - The value of the propertypublic static <E> E tryGetProperty(java.lang.Throwable exception,
java.lang.String name)
E - The expected type of the value of the propertyexception - The exception to obtain the property value fromname - The name of the propertypublic static java.lang.String tryGetPropertyAsString(java.lang.Throwable exception,
java.lang.String name)
exception - The exception to obtain the property value fromname - The name of the propertypublic static <R> R getExceptionDetails(java.lang.Throwable exception,
java.util.function.Function<java.lang.StringBuffer,R> callback)
R - The return type of the callback (if any)exception - The exception to obtain the exception details dump forcallback - The callback invoked with the string bufferpublic static java.lang.String getExceptionDetailsAsString(java.lang.Throwable exception)
exception - The exception to obtain the exception details dump forpublic static void printExceptionDetails(java.lang.Throwable exception,
java.io.PrintWriter printWriter)
exception - The exception to obtain the exception details dump forprintWriter - The print writer where to store thempublic static void printExceptionDetails(java.lang.Throwable exception,
java.io.PrintStream printStream)
exception - The exception to obtain the exception details dump forprintStream - The print stream where to store thempublic static <E> E checkResult(Result<E> result)
E - The type of the actual return valueresult - The Result object to be checkedprotected static <T extends java.lang.Throwable> void rethrowInternal(java.lang.Throwable throwable)
throws T extends java.lang.Throwable
T extends java.lang.Throwablepublic static void rethrow(java.lang.Throwable throwable)
Throwable as is, but making it unchecked.
Implements the 'sneaky throw' pattern to allow us to deal sensibly with checked exceptions.
For more details see here: https://www.google.com/#q=java+sneaky+throw
throwable - to be rethrown and unchecked.public static void propagate(ThrowsRunnable callback)
callback - The callback to executepublic static <R> R propagateFn(ThrowsSupplier<R> callback)
R - The return typecallback - The supplier callback to executepublic static void fatalOnException(ThrowsRunnable callback)
callback - The callback to executepublic static <R> R fatalOnExceptionFn(ThrowsSupplier<R> callback)
R - The return typecallback - The supplier callback to executepublic static void ensure(boolean expectedCondition,
java.lang.String message,
java.lang.Object... args)
IllegalStateException.expectedCondition - the provided parameter conditionmessage - the error message to display id the parameter condition is not satisfiedargs - list of args provided for formatting the messagepublic static void ensureArg(boolean expectedCondition,
java.lang.String message,
java.lang.Object... args)
IllegalArgumentException.expectedCondition - the provided parameter conditionmessage - the error message to display id the parameter condition is not satisfiedargs - list of args provided for formatting the messagepublic static void ensureOrFatal(boolean expectedCondition,
java.lang.String message,
java.lang.Object... args)
expectedCondition - the condition that must be satisfiedmessage - the message that will be passed to the exception handlerargs - list of args that are being checkedpublic static void ensureOrFatal(boolean expectedCondition,
java.lang.Exception cause,
java.lang.String message,
java.lang.Object... args)
expectedCondition - the condition that must be satisfiedcause - the exception to add as the cause.message - the message that will be passed to the exception handlerargs - list of args that are being checkedpublic static void abort(java.lang.String message,
java.lang.Exception cause)
IllegalStateException and an Exception to be added as the Cause.message - the message that will be used in the fatal exceptioncause - the exception to add as the cause.public static void abort(java.lang.String message)
IllegalStateException.message - the message that will be used in the fatal exceptionpublic static java.util.function.Consumer<java.lang.Throwable> getAbortHandler()
public static void setAbortHandler(java.util.function.Consumer<java.lang.Throwable> abortHandler)
abortHandler - The new fatal no-throw exception handler for the EH library code or null. If null is provided
then the default handler is used.public static void enhancedFinally(ThrowsRunnable callback, java.util.function.Consumer<java.lang.Throwable> handler)
callback - The callback to executehandler - The finally handler to execute (with extra exception info provided)public static <R> R enhancedFinallyFn(ThrowsSupplier<R> callback, java.util.function.Consumer<java.lang.Throwable> handler)
enhancedFinally(ThrowsRunnable, Consumer) except that it takes a supplier callback
which returns value and then this helper returns the same value.R - The return type of the supplier callbackcallback - The supplier callback to executehandler - The try/finally handler to executepublic static void trySuppressExceptions(java.lang.Throwable throwable,
ThrowsRunnable callback)
throwable - The throwable to capture the suppressed exception. It can be null (see above).callback - The callback to executepublic static void suppressExceptionsOrFatal(java.lang.Throwable throwable,
ThrowsRunnable callback)
trySuppressExceptions(Throwable, ThrowsRunnable)throwable - The throwable to capture the suppressed exception. It can be null (see above).callback - The callback to executepublic static <T> void tryCloseQuietly(java.lang.Throwable throwable,
T closeable,
ThrowsConsumer<T> closeCallback)
T - The type of the closeable object (typically object that implements Closeable)throwable - The throwable to capture the suppressed exception. It can be null (see above).closeable - The closeable object to be closedcloseCallback - The callback that is responsible for closing the objectpublic static void tryCloseQuietly(java.lang.Throwable throwable,
java.io.Closeable closeable)
tryCloseQuietly(Throwable, Object, ThrowsConsumer) for objects which implement
Closeable interfacethrowable - The throwable to capture the suppressed exception. It can be null (see above).closeable - The closeable object to be closedpublic static <T> void closeQuietlyOrFatal(java.lang.Throwable throwable,
T closeable,
ThrowsConsumer<T> closeCallback)
tryCloseQuietly(Throwable, Object, ThrowsConsumer)
Tries to close quietly a closable object of type T and aborts if the exception thrown by close cannot be suppressed (i.e. throwable is null or it does not support suppression of exceptions)
T - The type of the closeable object (typically object that implements Closeable)throwable - The throwable to capture the suppressed exception. It can be null (see above).closeable - The closeable object to be closedcloseCallback - The callback that is responsible for closing the objectpublic static void closeQuietlyOrException(java.lang.Throwable throwable,
java.io.Closeable closeable)
closeQuietlyOrFatal(Throwable, Object, ThrowsConsumer) for objects which
implement Closeable interfacethrowable - The throwable to capture the suppressed exception. It can be null (see above).closeable - The closeable object to be closed