Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify web api exception return value specification to json. #7888

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Unify the contentType setting method.
hubgeter committed Jan 2, 2025
commit 91310470c314598e8585b421fbc62239beedd987
Original file line number Diff line number Diff line change
@@ -765,6 +765,18 @@ private <T> T introspectAndConvert(final T value) {
return value;
}

public ResponseEntity<String> convertErrorAsJson(HttpStatus status, String errorMessage) {
return ResponseEntity.status(status)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
}

public ResponseEntity<String> convertErrorAsJson(HttpStatus status, Throwable t) {
return ResponseEntity.status(status)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(t));
}

String convertErrorAsJson(String errorMessage) {
return ("{" + "\"cause\"" + ":" + "\"" + errorMessage + "\"" + "}");
}
Original file line number Diff line number Diff line change
@@ -67,9 +67,7 @@ protected String getRestApiVersion() {
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<String> handle(final RuntimeException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(e.getMessage()));
return convertErrorAsJson(HttpStatus.NOT_FOUND, e.getMessage());
}

/**
@@ -84,9 +82,7 @@ public ResponseEntity<String> handle(final RuntimeException e) {
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<String> handleException(final RuntimeException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(e.getMessage()));
return convertErrorAsJson(HttpStatus.BAD_REQUEST, e.getMessage());
}

/**
@@ -101,9 +97,7 @@ public ResponseEntity<String> handleException(final RuntimeException e) {
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<String> handleException(final GemfireRestException ge) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(ge));
return convertErrorAsJson(HttpStatus.INTERNAL_SERVER_ERROR, ge);
}

/**
@@ -119,9 +113,7 @@ public ResponseEntity<String> handleException(final GemfireRestException ge) {
@ResponseBody
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
public ResponseEntity<String> handleException(final DataTypeNotSupportedException tns) {
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(tns.getMessage()));
return convertErrorAsJson(HttpStatus.NOT_ACCEPTABLE, tns.getMessage());
}

/**
@@ -137,9 +129,7 @@ public ResponseEntity<String> handleException(final DataTypeNotSupportedExceptio
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ResponseEntity<String> handleException(final HttpRequestMethodNotSupportedException e) {
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(e.getMessage()));
return convertErrorAsJson(HttpStatus.METHOD_NOT_ALLOWED, e.getMessage());
}

/**
@@ -154,9 +144,7 @@ public ResponseEntity<String> handleException(final HttpRequestMethodNotSupporte
@ResponseBody
@ResponseStatus(HttpStatus.FORBIDDEN)
public ResponseEntity<String> handleException(final AccessDeniedException cause) {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(cause.getMessage()));
return convertErrorAsJson(HttpStatus.FORBIDDEN, cause.getMessage());
}

/**
@@ -170,9 +158,7 @@ public ResponseEntity<String> handleException(final AccessDeniedException cause)
@ResponseBody
@ResponseStatus(HttpStatus.FORBIDDEN)
public ResponseEntity<String> handleException(final NotAuthorizedException cause) {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(cause.getMessage()));
return convertErrorAsJson(HttpStatus.FORBIDDEN, cause.getMessage());
}

/**
@@ -186,9 +172,7 @@ public ResponseEntity<String> handleException(final NotAuthorizedException cause
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<String> handleException(final EntityNotFoundException cause) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(cause.getMessage()));
return convertErrorAsJson(HttpStatus.NOT_FOUND, cause.getMessage());
}

/**
@@ -211,9 +195,7 @@ public ResponseEntity<String> handleException(final Throwable cause) {
logger.debug(stackTrace);
}

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(cause.getMessage()));
return convertErrorAsJson(HttpStatus.INTERNAL_SERVER_ERROR, cause.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -195,9 +195,7 @@ private ResponseEntity<?> getAllRegionData(String region, String limit) {
if (maxLimit < 0) {
String errorMessage =
String.format("Negative limit param (%1$s) is not valid!", maxLimit);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
}

int mapSize = keys.size();
@@ -212,9 +210,7 @@ private ResponseEntity<?> getAllRegionData(String region, String limit) {
// limit param is not specified in proper format. set the HTTPHeader
// for BAD_REQUEST
String errorMessage = String.format("limit param (%1$s) is not valid!", limit);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
}
}

@@ -253,6 +249,7 @@ private ResponseEntity<?> getRegionKeys(String region, String ignoreMissingKey,
logger.debug("Reading data for keys ({}) in Region ({})", ArrayUtils.toString(keys), region);
securityService.authorize("READ", region, keys);
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(APPLICATION_JSON_UTF8);
if (keys.length == 1) {
/* GET op on single key */
Object value = getValue(region, keys[0]);
@@ -282,9 +279,7 @@ private ResponseEntity<?> getRegionKeys(String region, String ignoreMissingKey,
String errorMessage = String.format(
"ignoreMissingKey param (%1$s) is not valid. valid usage is ignoreMissingKey=true!",
ignoreMissingKey);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
}

final Map<Object, Object> valueObjs = getValues(region, keys);
@@ -372,9 +367,7 @@ public ResponseEntity<?> update(@PathVariable("region") String region,
String errorMessage = String.format(
"The op parameter (%1$s) is not valid. Valid values are PUT, REPLACE, or CAS.",
opValue);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
}
if (keys.length > 1) {
updateMultipleKeys(region, keys, json);
@@ -444,9 +437,7 @@ public ResponseEntity<?> updateKeys(@PathVariable("region") final String encoded
String errorMessage = String.format(
"The op parameter (%1$s) is not valid. Valid values are PUT, CREATE, REPLACE, or CAS.",
opValue);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
}

if (decodedKeys.length > 1) {