Errors (durian 2.0 API) (original) (raw)
java.lang.Object
- com.diffplug.common.base.Errors
Direct Known Subclasses:
Errors.Handling, Errors.Rethrowing
public abstract class Errors
extends java.lang.Object
Errors makes it easy to create implementations of the standard functional interfaces (which don't allow checked exceptions). Even for cases where you aren't required to stuff some code into a functional interface, Errors is useful as a concise way to specify how errors will be handled.
Nested Class Summary
Nested Classes
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. Field Summary
Fields
Modifier and Type Field and Description protected java.util.function.Consumer<java.lang.Throwable> handler Constructor Summary
Constructors
Modifier Constructor and Description protected Errors(java.util.function.Consumer<java.lang.Throwable> error) Method Summary
All Methods Static Methods Instance Methods Concrete Methods
Modifier and Type Method and Description static java.lang.RuntimeException asRuntime(java.lang.Throwable e) Converts the given exception to a RuntimeException, with a minimum of new exceptions to obscure the cause. static Errors.Handling createHandling(java.util.function.Consumer<java.lang.Throwable> handler) Creates an Errors.Handling which passes any exceptions it receives to the given handler. static Errors.Rethrowing createRethrowing(java.util.function.Function<java.lang.Throwable,java.lang.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. void handle(java.lang.Throwable error) Passes the given error to be handled by the Errors. 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. java.util.function.Consumer wrap(Throwing.Consumer consumer) Returns a Consumer whose exceptions are handled by this Errors. java.lang.Runnable wrap(Throwing.Runnable runnable) Returns a Runnable whose exceptions are handled by this Errors. * ### Methods inherited from class java.lang.Object `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`Field Detail
* #### handler protected final java.util.function.Consumer<java.lang.Throwable> handlerConstructor Detail
* #### Errors protected Errors(java.util.function.Consumer<java.lang.Throwable> error)Method Detail
* #### createRethrowing public static [Errors.Rethrowing](../../../../com/diffplug/common/base/Errors.Rethrowing.html "class in com.diffplug.common.base") createRethrowing(java.util.function.Function<java.lang.Throwable,java.lang.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. If that function happens to throw an unchecked error itself, that'll work just fine. * #### createHandling public static [Errors.Handling](../../../../com/diffplug/common/base/Errors.Handling.html "class in com.diffplug.common.base") createHandling(java.util.function.Consumer<java.lang.Throwable> handler) Creates an Errors.Handling which passes any exceptions it receives to the given 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 creeateRethrowAs(). * #### suppress public static [Errors.Handling](../../../../com/diffplug/common/base/Errors.Handling.html "class in com.diffplug.common.base") suppress() Suppresses errors entirely. * #### rethrow public static [Errors.Rethrowing](../../../../com/diffplug/common/base/Errors.Rethrowing.html "class in com.diffplug.common.base") rethrow() Rethrows any exceptions as runtime exceptions. * #### log public static [Errors.Handling](../../../../com/diffplug/common/base/Errors.Handling.html "class in com.diffplug.common.base") log() Logs any exceptions. By default, log() calls Throwable.printStackTrace(). To modify this behavior in your application, call DurianPlugins.set(Errors.Plugins.Log.class, error -> myCustomLog(error)); * #### dialog public static [Errors.Handling](../../../../com/diffplug/common/base/Errors.Handling.html "class in com.diffplug.common.base") dialog() Opens a dialog to notify the user of any exceptions. It should be used in cases where an error is too severe to be silently logged. 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(). * #### handle public void handle(java.lang.Throwable error) Passes the given error to be handled by the Errors. * #### run public void run([Throwing.Runnable](../../../../com/diffplug/common/base/Throwing.Runnable.html "interface in com.diffplug.common.base") runnable) Attempts to run the given runnable. * #### wrap public java.lang.Runnable wrap([Throwing.Runnable](../../../../com/diffplug/common/base/Throwing.Runnable.html "interface in com.diffplug.common.base") runnable) Returns a Runnable whose exceptions are handled by this Errors. * #### wrap public <T> java.util.function.Consumer<T> wrap([Throwing.Consumer](../../../../com/diffplug/common/base/Throwing.Consumer.html "interface in com.diffplug.common.base")<T> consumer) Returns a Consumer whose exceptions are handled by this Errors. * #### asRuntime public static java.lang.RuntimeException asRuntime(java.lang.Throwable e) Converts the given exception to a RuntimeException, with a minimum of new exceptions to obscure the cause.