public abstract class Errors extends Object implements Consumer<Throwable>
Consumer<Throwable>
error handler,
see ErrorsExample.Modifier and Type | Class and Description |
---|---|
static class |
Errors.Handling
An
Errors which is free to rethrow the exception, but it might not. |
static interface |
Errors.Plugins
Namespace for the plugins which Errors supports.
|
static class |
Errors.Rethrowing
An
Errors which is guaranteed to always throw a RuntimeException. |
static class |
Errors.WrappedAsRuntimeException
A RuntimeException specifically for the purpose of wrapping non-runtime Throwables as RuntimeExceptions.
|
Modifier | Constructor and Description |
---|---|
protected |
Errors(Consumer<Throwable> error) |
Modifier and Type | Method and Description |
---|---|
void |
accept(Throwable error)
Passes the given error to this Errors.
|
static RuntimeException |
asRuntime(Throwable e)
Casts or wraps the given exception to be a RuntimeException.
|
Consumer<Optional<Throwable>> |
asTerminal()
Converts this
Consumer<Throwable> to a Consumer<Optional<Throwable>> . |
static Errors.Handling |
createHandling(Consumer<Throwable> handler)
Creates an Errors.Handling which passes any exceptions it receives
to the given handler.
|
static Errors.Rethrowing |
createRethrowing(Function<Throwable,RuntimeException> transform)
Creates an Errors.Rethrowing which transforms any exceptions it receives into a RuntimeException
as specified by the given function, and then throws that RuntimeException.
|
static Errors.Handling |
dialog()
Opens a dialog to notify the user of any exceptions.
|
static Errors.Handling |
log()
Logs any exceptions.
|
static Errors.Rethrowing |
rethrow()
Rethrows any exceptions as runtime exceptions.
|
void |
run(Throwing.Runnable runnable)
Attempts to run the given runnable.
|
static Errors.Handling |
suppress()
Suppresses errors entirely.
|
<T> Consumer<T> |
wrap(Throwing.Consumer<T> consumer)
Returns a Consumer whose exceptions are handled by this Errors.
|
Runnable |
wrap(Throwing.Runnable runnable)
Returns a Runnable whose exceptions are handled by this Errors.
|
public static Errors.Handling createHandling(Consumer<Throwable> handler)
The handler is free to throw a RuntimeException if it wants to. If it always
throws a RuntimeException, then you should instead create an Errors.Rethrowing
using createRethrowing(java.util.function.Function<java.lang.Throwable, java.lang.RuntimeException>)
.
public static Errors.Rethrowing createRethrowing(Function<Throwable,RuntimeException> transform)
If that function happens to throw an unchecked error itself, that'll work just fine too.
public static Errors.Handling suppress()
public static Errors.Rethrowing rethrow()
public static Errors.Handling log()
By default, log() calls Throwable.printStackTrace(). To modify this behavior in your application, call DurianPlugins.set(Errors.Plugins.Log.class, error -> myCustomLog(error));
public static Errors.Handling dialog()
By default, dialog() opens a JOptionPane. To modify this behavior in your application, call DurianPlugins.set(Errors.Plugins.Dialog.class, error -> openMyDialog(error));
For a non-interactive console application, a good implementation of would probably print the error and call System.exit().
public void accept(Throwable error)
public Consumer<Optional<Throwable>> asTerminal()
Consumer<Throwable>
to a Consumer<Optional<Throwable>>
.public void run(Throwing.Runnable runnable)
public Runnable wrap(Throwing.Runnable runnable)
public <T> Consumer<T> wrap(Throwing.Consumer<T> consumer)
public static RuntimeException asRuntime(Throwable e)