Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Add error key to exception response body message
Browse files Browse the repository at this point in the history
  • Loading branch information
jillurquddus committed Aug 17, 2022
1 parent 30de316 commit 7bbc22b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ public abstract class OntoPopException extends RuntimeException {
"ErrorMessages";
private static final ResourceBundle DEFAULT_BUNDLE = ResourceBundle
.getBundle(RESOURCE_BUNDLE_FILE_NAME_PREFIX, Locale.ROOT);
private static final String DEFAULT_I18N_KEY = "Default";
private final String i18nKey;
private static final String DEFAULT_ERROR_KEY_SUFFIX = "Default";
private final String errorKey;

protected OntoPopException(String className) {
super(DEFAULT_BUNDLE.getString(className + DEFAULT_I18N_KEY));
this.i18nKey = className + DEFAULT_I18N_KEY;
super(DEFAULT_BUNDLE.getString(className + DEFAULT_ERROR_KEY_SUFFIX));
this.errorKey = className + DEFAULT_ERROR_KEY_SUFFIX;
}

protected OntoPopException(String className, String i18nKey) {
super(DEFAULT_BUNDLE.getString(className + i18nKey));
this.i18nKey = className + i18nKey;
protected OntoPopException(String className, String errorKey) {
super(DEFAULT_BUNDLE.getString(className + errorKey));
this.errorKey = className + errorKey;
}

public String getI18nKey() {
return i18nKey;
public String getErrorKey() {
return errorKey;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ private ResponseEntity<Object> handleException(OntoPopException exception,
WebRequest request, HttpStatus httpStatus) {

// Construct the response body
String localisedErrorMessage = getI18nErrorMessage(
request, exception.getI18nKey());
String localisedErrorMessage = getErrorMessage(
request, exception.getErrorKey());
OntoPopExceptionResponseBody ontoPopExceptionResponseBody =
new OntoPopExceptionResponseBody(localisedErrorMessage);
new OntoPopExceptionResponseBody(exception.getErrorKey(),
localisedErrorMessage);
HttpHeaders headers = new HttpHeaders();
String responseBody = null;
try {
Expand All @@ -96,18 +97,18 @@ private ResponseEntity<Object> handleException(OntoPopException exception,
/**
* Get the localised error message for this exception
* @param request
* @param i18nKey
* @param errorKey
* @return
*/

private String getI18nErrorMessage(WebRequest request, String i18nKey) {
private String getErrorMessage(WebRequest request, String errorKey) {
String lang = request.getParameter(LANG_REQUEST_PARAMETER_NAME);
ResourceBundle bundle = StringUtils.hasText(lang) ?
ResourceBundle.getBundle(RESOURCE_BUNDLE_FILE_NAME_PREFIX,
new Locale(lang)) :
ResourceBundle.getBundle(RESOURCE_BUNDLE_FILE_NAME_PREFIX,
Locale.ROOT);
return bundle.getString(i18nKey);
return bundle.getString(errorKey);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
public class OntoPopExceptionResponseBody implements Serializable {

private static final long serialVersionUID = -6791529013463816400L;
private String errorKey;
private String errorDisplayMessage;

public OntoPopExceptionResponseBody(String errorDisplayMessage) {
public OntoPopExceptionResponseBody(String errorKey,
String errorDisplayMessage) {
this.errorKey = errorKey;
this.errorDisplayMessage = errorDisplayMessage;
}

public String getErrorKey() {
return errorKey;
}

public String getErrorDisplayMessage() {
return errorDisplayMessage;
Expand All @@ -25,6 +32,7 @@ public String getErrorDisplayMessage() {
@Override
public String toString() {
return "OntoPopExceptionResponseBody ["
+ "errorKey=" + errorKey + ", "
+ "errorDisplayMessage=" + errorDisplayMessage
+ "]";
}
Expand Down

0 comments on commit 7bbc22b

Please sign in to comment.