java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
io.github.mmm.base.exception.ApplicationException
- All Implemented Interfaces:
Localizable,LocalizableObject,Serializable
- Direct Known Subclasses:
DuplicateObjectException,ObjectMismatchException,ObjectNotFoundException,ReadOnlyException,RuntimeIoException
Extends
Exceptions should only occur in unexpected or undesired situations. Never use exceptions for control flows.
RuntimeException with the following features:
- a
UUIDunique per exception instance automatically generated once per exceptionchain. - an
error codethat should be unique per exception type. - distinction between
technicalexceptions and exceptionsintended for end-users.
Exceptions should only occur in unexpected or undesired situations. Never use exceptions for control flows.
- See Also:
-
Field Summary
Fields inherited from interface io.github.mmm.base.i18n.Localizable
BUNDLE_PREFIX -
Constructor Summary
ConstructorsModifierConstructorDescriptionApplicationException(Localizable message) The constructor.ApplicationException(Localizable message, Throwable cause) The constructor.protectedApplicationException(Localizable message, Throwable cause, UUID uuid) The constructor.ApplicationException(String message) The constructor.ApplicationException(String message, Throwable cause) The constructor.protectedApplicationException(String message, Throwable cause, UUID uuid) The constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected UUIDgetCode()This method gets the code that identifies the detailed type of this object.getLocalizedMessage(Locale locale) This method gets the resolved and localized message.voidgetLocalizedMessage(Locale locale, Appendable appendable) This method writes thelocalized messageto the givenAppendable.ATTENTION: Logging frameworks like logback do not follow Java exception standards and writetoString()but onlygetMessage()of theExceptiontogether with the stacktrace into the log.static LocalizablegetNlsMessage(Throwable error) final UUIDgetUuid()This method gets theUUIDof thisApplicationException.booleanNote: Please consider usingnet.sf.mmm.nls.exception.NlsExceptionwith i18n support in case you are creating exceptions for end-users.booleanDetermines if this is a technical exception.voidprintStackTrace(Locale locale, Appendable buffer) This method prints the stack trace with localized exception message(s).toString()LiketoString()but using the specifiedLocale.toString(Locale locale, Appendable appendable) appendsthe localized string representation of this exception.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTraceMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface io.github.mmm.base.i18n.Localizable
getArgument, toLocalizable
-
Constructor Details
-
ApplicationException
The constructor.- Parameters:
message- themessagedescribing the problem briefly.
-
ApplicationException
The constructor.- Parameters:
message- themessagedescribing the problem briefly.
-
ApplicationException
The constructor. -
ApplicationException
The constructor.- Parameters:
message- theNLS messagedescribing the problem briefly.cause- thecauseof this exception.
-
ApplicationException
The constructor. -
ApplicationException
The constructor.- Parameters:
message- theNLS messagedescribing the problem briefly.cause- thecauseof this exception. May benull.uuid- the explicitUUIDornullto initialize by default (from givenThrowableor as newUUID).
-
-
Method Details
-
createUuid
-
getUuid
This method gets theUUIDof thisApplicationException. When a newApplicationExceptionis created, aUUIDis assigned. In case theApplicationExceptionis created from anotherApplicationExceptionas cause, the existingUUIDwill be used from that cause. Otherwise a newUUIDis generated.
TheUUIDwill appear in thestacktracebut NOT in themessage. It will therefore be written to log-files if this exception is logged. If you supply theUUIDto the end-user, he can provide it with the problem report so an administrator or software developer can easily find the stacktrace in the log-files.- Returns:
- the
UUIDof this object.
-
getMessage
ATTENTION: Logging frameworks like logback do not follow Java exception standards and writetoString()but onlygetMessage()of theExceptiontogether with the stacktrace into the log. Since anApplicationExceptioncontains additional information likeUUIDandcodethat needs to be logged we are forced to return the message from this method in the following form:[
Please note that the«code»: ]«message»«uuid»codeis omitted if thegetCode()method is not overridden and returning a custom code.
In case you want to get the plain exception message, you therefore need to either callgetLocalizedMessage()or usegetNlsMessage().- Specified by:
getMessagein interfaceLocalizable- Overrides:
getMessagein classThrowable- Returns:
- the untranslated message in the form specified above.
- See Also:
-
getNlsMessage
- Returns:
- the
Localizablemessage describing the problem. - See Also:
-
getLocalizedMessage
Description copied from interface:LocalizableThis method tries to get thelocalized messageasStringusing thedefault locale. ATTENTION:
If possible try to avoid using this method and useLocalizable.getLocalizedMessage(Locale)instead (e.g. using springLocaleContextHolderto get the users locale).- Specified by:
getLocalizedMessagein interfaceLocalizable- Overrides:
getLocalizedMessagein classThrowable- Returns:
- the localized message.
-
getLocalizedMessage
Description copied from interface:LocalizableThis method gets the resolved and localized message.- Specified by:
getLocalizedMessagein interfaceLocalizable- Parameters:
locale- is the locale to translate to.- Returns:
- the localized message.
-
getLocalizedMessage
Description copied from interface:Localizable- Specified by:
getLocalizedMessagein interfaceLocalizable- Parameters:
locale- theLocaleto translate to.appendable- theAppendablewhere towritethe message to.- See Also:
-
printStackTrace
This method prints the stack trace with localized exception message(s).- Parameters:
locale- is the locale to translate to.buffer- is where to write the stack trace to.- Throws:
IllegalStateException- if the givenbufferproduced anIOException.
-
isTechnical
public boolean isTechnical()Determines if this is a technical exception.- A technical exception is an unexpected situation that is to be logged on error level and should be analyzed by
the operators or software developers of the system. Further in such case the end-user can typically do nothing
about the problem (except to retry his operation) and will typically not understand the problem. Therefore a
generic message should be
displayed to the end-userin such case. - A non technical exception is called user failure. It is an undesired but NOT abnormal situation (e.g.
a mandatory field was not filled). It should be logged on a level less than error (typically info). The
messageis typicallyintended for to end-usersand has to be easy to understand.
- Returns:
trueif this is a technical exception,falseif this is a user error.- See Also:
- A technical exception is an unexpected situation that is to be logged on error level and should be analyzed by
the operators or software developers of the system. Further in such case the end-user can typically do nothing
about the problem (except to retry his operation) and will typically not understand the problem. Therefore a
generic message should be
-
isForUser
public boolean isForUser()Note: Please consider usingnet.sf.mmm.nls.exception.NlsExceptionwith i18n support in case you are creating exceptions for end-users. -
getCode
This method gets the code that identifies the detailed type of this object. WhileUUIDis unique per instance of aApplicationExceptionthis code is a short and readable identifier representing theApplicationExceptionClass. The default implementation returns thesimple name. However, the code should remain stable after refactoring (so at least after the rename the previous code should be returned asStringliteral). This code may be used as a compact identifier to reference the related problem or information as well as for automatic tests of error situations that should remain stable even if the message text gets improved or the locale is unknown.- Returns:
- the error code.
-
toString
-
toString
LiketoString()but using the specifiedLocale.- Parameters:
locale- is theLocaleused forgetLocalizedMessage(Locale).- Returns:
- the localized string representation of this exception as described in
toString(Locale, Appendable)
-
toString
appendsthe localized string representation of this exception. It is defined as following:«
classname»: [«custom-code»: ]«message»- Parameters:
locale- is theLocaleused forgetLocalizedMessage(Locale).appendable- is the buffer toappendto. Will be created asStringBuilderifnullis provided.- Returns:
- the provided
Appendableor the created one ifnullwas given.
-
getNlsMessage
- Parameters:
error- theThrowable.- Returns:
- the
NLS message.
-