public class ErrorHandler
extends java.lang.Object
Action, that are found to match the error.| Modifier and Type | Method and Description |
|---|---|
ErrorHandler |
always(Action action)
Register
action to be executed on all errors. |
<T> ErrorHandler |
bind(T errorCode,
MatcherFactory<? super T> matcherFactory)
Bind an
errorCode to a Matcher, using a MatcherFactory. |
<T> ErrorHandler |
bindClass(java.lang.Class<T> errorCodeClass,
MatcherFactory<? super T> matcherFactory)
Bind an
errorCode Class to a Matcher, using a MatcherFactory. |
void |
clear()
Clear ErrorHandler instance from all its registered Actions and Matchers.
|
static ErrorHandler |
create()
Create a new @{link ErrorHandler}, that delegates to the default one.
|
static ErrorHandler |
createIsolated()
Create a new @{link ErrorHandler}, isolated from the default one.
|
static ErrorHandler |
defaultErrorHandler()
Get the default @{link ErrorHandler}, a singleton object
to which all other instances by default delegate to.
|
protected <T> MatcherFactory<? super T> |
getMatcherFactoryForErrorCode(T errorCode) |
void |
handle(java.lang.Throwable error)
Handle
error by executing all matching actions. |
protected void |
handle(java.lang.Throwable error,
java.lang.ThreadLocal<com.workable.errorhandler.ErrorHandler.Context> context) |
ErrorHandler |
on(java.lang.Class<? extends java.lang.Exception> exceptionClass,
Action action)
Register
action to be executed by handle(Throwable),
if the thrown error is an instance of exceptionClass. |
ErrorHandler |
on(Matcher matcher,
Action action)
|
<T> ErrorHandler |
on(T errorCode,
Action action)
Register
action to be executed by handle(Throwable),
if the thrown error is bound (associated) to errorCode. |
ErrorHandler |
otherwise(Action action)
Register
action to be executed in case no other conditional
action gets executed. |
void |
run(BlockExecutor blockExecutor)
Run a custom code block and assign current ErrorHandler instance
to handle a possible exception throw in 'catch'.
|
ErrorHandler |
skipAlways()
Skip all actions registered via
always(Action) |
ErrorHandler |
skipDefaults()
Skip the default matching actions if any
|
ErrorHandler |
skipFollowing()
Skip all following actions registered via an
on method |
public static ErrorHandler createIsolated()
In other words, designed to handle all errors by itself without delegating to the default error handler.
ErrorHandler instancepublic static ErrorHandler create()
Any default actions, are always executed after the ones registered on this one.
ErrorHandler instancepublic static ErrorHandler defaultErrorHandler()
public ErrorHandler on(Matcher matcher, Action action)
matcher - a matcher to match the thrown erroraction - the associated actionErrorHandler instance - to use in command chainspublic ErrorHandler on(java.lang.Class<? extends java.lang.Exception> exceptionClass, Action action)
action to be executed by handle(Throwable),
if the thrown error is an instance of exceptionClass.exceptionClass - the class of the erroraction - the associated actionErrorHandler instance - to use in command chainspublic <T> ErrorHandler on(T errorCode, Action action)
action to be executed by handle(Throwable),
if the thrown error is bound (associated) to errorCode.
See bindClass(Class, MatcherFactory) and bind(Object, MatcherFactory)
on how to associate arbitrary error codes with actual Throwables via Matcher.
T - the error code typeerrorCode - the error codeaction - the associated actionErrorHandler instance - to use in command chainspublic ErrorHandler otherwise(Action action)
action to be executed in case no other conditional
action gets executed.action - the actionErrorHandler instance - to use in command chainspublic ErrorHandler always(Action action)
action to be executed on all errors.action - the actionErrorHandler instance - to use in command chainspublic ErrorHandler skipFollowing()
on methodErrorHandler instance - to use in command chainspublic ErrorHandler skipAlways()
always(Action)ErrorHandler instance - to use in command chainspublic ErrorHandler skipDefaults()
ErrorHandler instance - to use in command chainsprotected void handle(java.lang.Throwable error,
java.lang.ThreadLocal<com.workable.errorhandler.ErrorHandler.Context> context)
public void run(BlockExecutor blockExecutor)
blockExecutor - functional interface containing Exception prone codepublic void handle(java.lang.Throwable error)
error by executing all matching actions.error - the error as a Throwablepublic <T> ErrorHandler bind(T errorCode, MatcherFactory<? super T> matcherFactory)
errorCode to a Matcher, using a MatcherFactory.
For example, when we need to catch a network timeout it's better to just write "timeout" instead of a train-wreck expression. So we need to bind this "timeout" error code to an actual condition that will check the actual error when it occurs to see if its a network timeout or not.
ErrorHandler
.defaultErrorHandler()
.bind("timeout", errorCode -> throwable -> {
return (throwable instanceof SocketTimeoutException) && throwable.getMessage().contains("Read timed out");
});
// ...
ErrorHandler
.build()
.on("timeout", (throwable, handler) -> {
showOfflineScreen();
})
T - the error code typeerrorCode - the errorCode value, can use a primitive for clarity and let it be autoboxedmatcherFactory - a factory that given an error code, provides a matcher to match the error against itErrorHandler instance - to use in command chainspublic <T> ErrorHandler bindClass(java.lang.Class<T> errorCodeClass, MatcherFactory<? super T> matcherFactory)
errorCode Class to a Matcher, using a MatcherFactory.
For example, when we prefer using plain integers to refer to HTTP errors instead of checking the HTTPException status code every time.
ErrorHandler
.defaultErrorHandler()
.bindClass(Integer.class, errorCode -> throwable -> {
return throwable instanceof HTTPException && ((HTTPException)throwable).getStatusCode() == errorCode;
});
// ...
ErrorHandler
.build()
.on(404, (throwable, handler) -> {
showResourceNotFoundError();
})
.on(500, (throwable, handler) -> {
showServerError();
})
T - the error code typeerrorCodeClass - the errorCode classmatcherFactory - a factory that given an error code, provides a matcher to match the error against itErrorHandler instance - to use in command chainsprotected <T> MatcherFactory<? super T> getMatcherFactoryForErrorCode(T errorCode)
public void clear()