public static class Exceptions.ErrorHandler extends Object
The intention is to use a call like:
Exceptions.handler()
.error(anException) // Sets the exception to handle
.to(aLogger) // Sets the logger to use for logging
.withNLSKey("nls.key") // Sets the i18n key to create the error message
.set("param",value) // Sets a named parameter which occurs in the message
.handle(); // logs an creates the HandledException
Since none of the methods must be called (except handle() of course), this provides a lot of flexibility and permits to handle several different error situations without having methods with long parameter lists and lots of null values.
The set(String, Object)
method can be called several times to set different parameters. The reason
why named parameters are used is because the resulting messages in the .properties files are easier to
translate and also the order of the parameters can be different in different languages.
Modifier | Constructor and Description |
---|---|
protected |
ErrorHandler(boolean processError)
Use
Exceptions.handle() to create an ErrorHandler |
Modifier and Type | Method and Description |
---|---|
Exceptions.ErrorHandler |
error(Throwable e)
Specifies which exception leaded to the error being handled.
|
HandledException |
handle()
Generates and logs the resulting HandledException.
|
Exceptions.ErrorHandler |
set(String parameter,
Object value)
Specifies a parameter which is replaced in the generated error message.
|
Exceptions.ErrorHandler |
to(Log log)
Specifies the logger which is used to log the generated exception.
|
String |
toString() |
Exceptions.ErrorHandler |
withNLSKey(String key)
Specifies the i18n key which is passed to
NLS.fmtr(String) to create the internal formatter
used to generate the translated error message. |
Exceptions.ErrorHandler |
withSystemErrorMessage(String englishMessagePattern,
Object... params)
Sets an untranslated error message, used by rare system errors.
|
protected ErrorHandler(boolean processError)
Exceptions.handle()
to create an ErrorHandlerprocessError
- determines if the error should be processed
(logged and sent to all ExceptionHandler
) or if just a
HandledException
is to be createdpublic Exceptions.ErrorHandler error(Throwable e)
e
- the exception which needs to be attached to this error handlerpublic Exceptions.ErrorHandler to(Log log)
log
- the logger used to log the generated HandledExceptionpublic Exceptions.ErrorHandler withNLSKey(String key)
NLS.fmtr(String)
to create the internal formatter
used to generate the translated error message.
This message may contain two parameters which don't need to be passed in: errorMessage and errorClass which contain the message of the exception being handled as well as the type name of it.
key
- the translation key used to fetch the translated error messagepublic Exceptions.ErrorHandler withSystemErrorMessage(String englishMessagePattern, Object... params)
Still a translated message will be created, which notifies the user about the system error and provides the untranslated error message, generated by this method. These messages should be in english.
englishMessagePattern
- contains a pattern used to generate the error message. May contain
placeholders as understood by Strings.apply(String, Object...)
.params
- parameters used to format the resulting error message based on the given
patternpublic Exceptions.ErrorHandler set(String parameter, Object value)
parameter
- the name of the parameter which should be replaced. This must occur as
${parameter}
in the translated message to be replacedvalue
- the value to be used as replacement for the parameter. The given value will be converted
to a string using NLS.toUserString(Object)
public HandledException handle()
The generated exception can be either thrown (it subclasses RuntimeException and therefore needs no throws clause). Alternatively it may be passed along or even be just discarded.
Copyright © 2018. All rights reserved.