Class CommonRTE
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
io.github.venkateshamurthy.enums.examples.CommonRTE
- All Implemented Interfaces:
Serializable
CommonRTE is a unified runtime exception class that can be used across the
project to encapsulate error information in a structured and consistent format.
This class extends RuntimeException and adds contextual fields such as:
code- Application or domain-specific error codedetailedMessage- Detailed, formatted explanation of the errortimeStamp- UTC timestamp when the error was createdhttpStatus- TheHttpStatusrepresenting the error category
The class supports message formatting with templates, placeholder substitution, and structured logging to simplify debugging and monitoring.
Example usage:
throw new CommonRTE(FaultCode.CREDENTIAL_MISSING,
"Missing or invalid credential ID: {}", request.getCredentialId())
.setHttpStatus(HttpStatus.BAD_REQUEST)
.logInfo();
Another example: *
* throw FaultCodes.CREDENTIAL_MISSING.toCommonRTE(
* "Missing or invalid credential ID: {}", request.getCredentialId())
* .setHttpStatus(HttpStatus.BAD_REQUEST)
* .logInfo();
* - Since:
- 1.0
- Author:
- venkateshamurthy
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Supplier<ZonedDateTime>A supplier of current instant. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondetailedMessage(String template, Object... values) Sets the detailed message by substituting positional placeholders ("{}") in the given template.formatDetailedMessage(MessageFormat template, Object... values) Sets the detailed message using aMessageFormatinstance for advanced message formatting.logDebug()Logs this exception at DEBUG level (though implemented as INFO here for visibility), including message, code, timestamp, details, and HTTP status.logInfo()Logs this exception’s summary at INFO level, including error code, timestamp, and details.setDetailedMessage(String template, Map<String, Object> values) Sets the detailed message by replacing named placeholders in the form of{key}.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Field Details
-
NOW
A supplier of current instant.
-
-
Constructor Details
-
CommonRTE
Constructs a newCommonRTEwith the specified message. Automatically sets the timestamp to current UTC time.- Parameters:
message- the exception message
-
CommonRTE
Constructs a newCommonRTEwith the specified message and cause. Automatically sets the timestamp to current UTC time.- Parameters:
message- the exception messagecause- the underlying cause of this exception
-
-
Method Details
-
detailedMessage
Sets the detailed message by substituting positional placeholders ("{}") in the given template.- Parameters:
template- the message templatevalues- values to replace in template- Returns:
- this instance for fluent chaining
-
setDetailedMessage
Sets the detailed message by replacing named placeholders in the form of{key}.Example:
exception.setDetailedMessage("Error in {module} at {time}", Map.of("module", "Auth", "time", "12:00 UTC"));- Parameters:
template- the message template with named placeholdersvalues- the key-value pairs for substitution- Returns:
- this instance for fluent chaining
-
formatDetailedMessage
Sets the detailed message using aMessageFormatinstance for advanced message formatting.- Parameters:
template- theMessageFormatinstancevalues- the argument values- Returns:
- this instance for fluent chaining
-
logInfo
Logs this exception’s summary at INFO level, including error code, timestamp, and details.- Returns:
- this instance for fluent chaining
-
logDebug
Logs this exception at DEBUG level (though implemented as INFO here for visibility), including message, code, timestamp, details, and HTTP status.- Returns:
- this instance for fluent chaining
-