Skip to content

Commit

Permalink
Unify web api exception return value specification to json.
Browse files Browse the repository at this point in the history
  • Loading branch information
hubgeter committed Dec 29, 2024
1 parent d195814 commit b2550f4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand Down Expand Up @@ -65,8 +66,10 @@ protected String getRestApiVersion() {
@ExceptionHandler({RegionNotFoundException.class, ResourceNotFoundException.class})
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handle(final RuntimeException e) {
return convertErrorAsJson(e.getMessage());
public ResponseEntity<String> handle(final RuntimeException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(e.getMessage()));
}

/**
Expand All @@ -80,8 +83,10 @@ public String handle(final RuntimeException e) {
@ExceptionHandler({MalformedJsonException.class})
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handleException(final RuntimeException e) {
return convertErrorAsJson(e.getMessage());
public ResponseEntity<String> handleException(final RuntimeException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(e.getMessage()));
}

/**
Expand All @@ -95,8 +100,10 @@ public String handleException(final RuntimeException e) {
@ExceptionHandler(GemfireRestException.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleException(final GemfireRestException ge) {
return convertErrorAsJson(ge);
public ResponseEntity<String> handleException(final GemfireRestException ge) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(ge));
}

/**
Expand All @@ -111,8 +118,10 @@ public String handleException(final GemfireRestException ge) {
@ExceptionHandler(DataTypeNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
public String handleException(final DataTypeNotSupportedException tns) {
return convertErrorAsJson(tns.getMessage());
public ResponseEntity<String> handleException(final DataTypeNotSupportedException tns) {
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(tns.getMessage()));
}

/**
Expand All @@ -127,8 +136,10 @@ public String handleException(final DataTypeNotSupportedException tns) {
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public String handleException(final HttpRequestMethodNotSupportedException e) {
return convertErrorAsJson(e.getMessage());
public ResponseEntity<String> handleException(final HttpRequestMethodNotSupportedException e) {
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(e.getMessage()));
}

/**
Expand All @@ -142,8 +153,10 @@ public String handleException(final HttpRequestMethodNotSupportedException e) {
@ExceptionHandler(AccessDeniedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.FORBIDDEN)
public String handleException(final AccessDeniedException cause) {
return convertErrorAsJson(cause.getMessage());
public ResponseEntity<String> handleException(final AccessDeniedException cause) {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(cause.getMessage()));
}

/**
Expand All @@ -156,8 +169,10 @@ public String handleException(final AccessDeniedException cause) {
@ExceptionHandler(NotAuthorizedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.FORBIDDEN)
public String handleException(final NotAuthorizedException cause) {
return convertErrorAsJson(cause.getMessage());
public ResponseEntity<String> handleException(final NotAuthorizedException cause) {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(cause.getMessage()));
}

/**
Expand All @@ -170,8 +185,10 @@ public String handleException(final NotAuthorizedException cause) {
@ExceptionHandler(EntityNotFoundException.class)
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleException(final EntityNotFoundException cause) {
return convertErrorAsJson(cause.getMessage());
public ResponseEntity<String> handleException(final EntityNotFoundException cause) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(cause.getMessage()));
}

/**
Expand All @@ -185,7 +202,7 @@ public String handleException(final EntityNotFoundException cause) {
@ExceptionHandler(Throwable.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleException(final Throwable cause) {
public ResponseEntity<String> handleException(final Throwable cause) {
final StringWriter stackTraceWriter = new StringWriter();
cause.printStackTrace(new PrintWriter(stackTraceWriter));
final String stackTrace = stackTraceWriter.toString();
Expand All @@ -194,7 +211,9 @@ public String handleException(final Throwable cause) {
logger.debug(stackTrace);
}

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

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

int mapSize = keys.size();
Expand All @@ -210,11 +212,14 @@ 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 new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
}
}

headers.set(HttpHeaders.CONTENT_LOCATION, toUri(region, keyList).toASCIIString());
headers.setContentType(APPLICATION_JSON_UTF8);
return new ResponseEntity<RegionData<?>>(data, headers, HttpStatus.OK);
}

Expand Down Expand Up @@ -277,7 +282,9 @@ 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 new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
}

final Map<Object, Object> valueObjs = getValues(region, keys);
Expand Down Expand Up @@ -365,17 +372,21 @@ 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 new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
}
if (keys.length > 1) {
updateMultipleKeys(region, keys, json);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(APPLICATION_JSON_UTF8);
headers.setLocation(toUri(region, StringUtils.arrayToCommaDelimitedString(keys)));
return new ResponseEntity<>(headers, HttpStatus.OK);
} else {
// put case
Object existingValue = updateSingleKey(region, keys[0], json, opValue);
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(APPLICATION_JSON_UTF8);
headers.setLocation(toUri(region, keys[0]));
return new ResponseEntity<>(existingValue, headers,
(existingValue == null ? HttpStatus.OK : HttpStatus.CONFLICT));
Expand Down Expand Up @@ -433,7 +444,9 @@ 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 new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
}

if (decodedKeys.length > 1) {
Expand Down

0 comments on commit b2550f4

Please sign in to comment.