Skip to content

Commit c1ba95c

Browse files
authored
Unify web api exception return value specification to json. (#7888)
1 parent 4a83ba4 commit c1ba95c

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/AbstractBaseController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,18 @@ private <T> T introspectAndConvert(final T value) {
765765
return value;
766766
}
767767

768+
public ResponseEntity<String> convertErrorAsJson(HttpStatus status, String errorMessage) {
769+
return ResponseEntity.status(status)
770+
.contentType(APPLICATION_JSON_UTF8)
771+
.body(convertErrorAsJson(errorMessage));
772+
}
773+
774+
public ResponseEntity<String> convertErrorAsJson(HttpStatus status, Throwable t) {
775+
return ResponseEntity.status(status)
776+
.contentType(APPLICATION_JSON_UTF8)
777+
.body(convertErrorAsJson(t));
778+
}
779+
768780
String convertErrorAsJson(String errorMessage) {
769781
return ("{" + "\"cause\"" + ":" + "\"" + errorMessage + "\"" + "}");
770782
}

geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/BaseControllerAdvice.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.logging.log4j.Logger;
2121
import org.springframework.http.HttpStatus;
22+
import org.springframework.http.ResponseEntity;
2223
import org.springframework.security.access.AccessDeniedException;
2324
import org.springframework.web.HttpRequestMethodNotSupportedException;
2425
import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -65,8 +66,8 @@ protected String getRestApiVersion() {
6566
@ExceptionHandler({RegionNotFoundException.class, ResourceNotFoundException.class})
6667
@ResponseBody
6768
@ResponseStatus(HttpStatus.NOT_FOUND)
68-
public String handle(final RuntimeException e) {
69-
return convertErrorAsJson(e.getMessage());
69+
public ResponseEntity<String> handle(final RuntimeException e) {
70+
return convertErrorAsJson(HttpStatus.NOT_FOUND, e.getMessage());
7071
}
7172

7273
/**
@@ -80,8 +81,8 @@ public String handle(final RuntimeException e) {
8081
@ExceptionHandler({MalformedJsonException.class})
8182
@ResponseBody
8283
@ResponseStatus(HttpStatus.BAD_REQUEST)
83-
public String handleException(final RuntimeException e) {
84-
return convertErrorAsJson(e.getMessage());
84+
public ResponseEntity<String> handleException(final RuntimeException e) {
85+
return convertErrorAsJson(HttpStatus.BAD_REQUEST, e.getMessage());
8586
}
8687

8788
/**
@@ -95,8 +96,8 @@ public String handleException(final RuntimeException e) {
9596
@ExceptionHandler(GemfireRestException.class)
9697
@ResponseBody
9798
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
98-
public String handleException(final GemfireRestException ge) {
99-
return convertErrorAsJson(ge);
99+
public ResponseEntity<String> handleException(final GemfireRestException ge) {
100+
return convertErrorAsJson(HttpStatus.INTERNAL_SERVER_ERROR, ge);
100101
}
101102

102103
/**
@@ -111,8 +112,8 @@ public String handleException(final GemfireRestException ge) {
111112
@ExceptionHandler(DataTypeNotSupportedException.class)
112113
@ResponseBody
113114
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
114-
public String handleException(final DataTypeNotSupportedException tns) {
115-
return convertErrorAsJson(tns.getMessage());
115+
public ResponseEntity<String> handleException(final DataTypeNotSupportedException tns) {
116+
return convertErrorAsJson(HttpStatus.NOT_ACCEPTABLE, tns.getMessage());
116117
}
117118

118119
/**
@@ -127,8 +128,8 @@ public String handleException(final DataTypeNotSupportedException tns) {
127128
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
128129
@ResponseBody
129130
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
130-
public String handleException(final HttpRequestMethodNotSupportedException e) {
131-
return convertErrorAsJson(e.getMessage());
131+
public ResponseEntity<String> handleException(final HttpRequestMethodNotSupportedException e) {
132+
return convertErrorAsJson(HttpStatus.METHOD_NOT_ALLOWED, e.getMessage());
132133
}
133134

134135
/**
@@ -142,8 +143,8 @@ public String handleException(final HttpRequestMethodNotSupportedException e) {
142143
@ExceptionHandler(AccessDeniedException.class)
143144
@ResponseBody
144145
@ResponseStatus(HttpStatus.FORBIDDEN)
145-
public String handleException(final AccessDeniedException cause) {
146-
return convertErrorAsJson(cause.getMessage());
146+
public ResponseEntity<String> handleException(final AccessDeniedException cause) {
147+
return convertErrorAsJson(HttpStatus.FORBIDDEN, cause.getMessage());
147148
}
148149

149150
/**
@@ -156,8 +157,8 @@ public String handleException(final AccessDeniedException cause) {
156157
@ExceptionHandler(NotAuthorizedException.class)
157158
@ResponseBody
158159
@ResponseStatus(HttpStatus.FORBIDDEN)
159-
public String handleException(final NotAuthorizedException cause) {
160-
return convertErrorAsJson(cause.getMessage());
160+
public ResponseEntity<String> handleException(final NotAuthorizedException cause) {
161+
return convertErrorAsJson(HttpStatus.FORBIDDEN, cause.getMessage());
161162
}
162163

163164
/**
@@ -170,8 +171,8 @@ public String handleException(final NotAuthorizedException cause) {
170171
@ExceptionHandler(EntityNotFoundException.class)
171172
@ResponseBody
172173
@ResponseStatus(HttpStatus.NOT_FOUND)
173-
public String handleException(final EntityNotFoundException cause) {
174-
return convertErrorAsJson(cause.getMessage());
174+
public ResponseEntity<String> handleException(final EntityNotFoundException cause) {
175+
return convertErrorAsJson(HttpStatus.NOT_FOUND, cause.getMessage());
175176
}
176177

177178
/**
@@ -185,7 +186,7 @@ public String handleException(final EntityNotFoundException cause) {
185186
@ExceptionHandler(Throwable.class)
186187
@ResponseBody
187188
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
188-
public String handleException(final Throwable cause) {
189+
public ResponseEntity<String> handleException(final Throwable cause) {
189190
final StringWriter stackTraceWriter = new StringWriter();
190191
cause.printStackTrace(new PrintWriter(stackTraceWriter));
191192
final String stackTrace = stackTraceWriter.toString();
@@ -194,7 +195,7 @@ public String handleException(final Throwable cause) {
194195
logger.debug(stackTrace);
195196
}
196197

197-
return convertErrorAsJson(cause.getMessage());
198+
return convertErrorAsJson(HttpStatus.INTERNAL_SERVER_ERROR, cause.getMessage());
198199
}
199200

200201
}

geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/PdxBasedCrudController.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private ResponseEntity<?> getAllRegionData(String region, String limit) {
195195
if (maxLimit < 0) {
196196
String errorMessage =
197197
String.format("Negative limit param (%1$s) is not valid!", maxLimit);
198-
return new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
198+
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
199199
}
200200

201201
int mapSize = keys.size();
@@ -210,11 +210,12 @@ private ResponseEntity<?> getAllRegionData(String region, String limit) {
210210
// limit param is not specified in proper format. set the HTTPHeader
211211
// for BAD_REQUEST
212212
String errorMessage = String.format("limit param (%1$s) is not valid!", limit);
213-
return new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
213+
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
214214
}
215215
}
216216

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

@@ -248,6 +249,7 @@ private ResponseEntity<?> getRegionKeys(String region, String ignoreMissingKey,
248249
logger.debug("Reading data for keys ({}) in Region ({})", ArrayUtils.toString(keys), region);
249250
securityService.authorize("READ", region, keys);
250251
final HttpHeaders headers = new HttpHeaders();
252+
headers.setContentType(APPLICATION_JSON_UTF8);
251253
if (keys.length == 1) {
252254
/* GET op on single key */
253255
Object value = getValue(region, keys[0]);
@@ -277,7 +279,7 @@ private ResponseEntity<?> getRegionKeys(String region, String ignoreMissingKey,
277279
String errorMessage = String.format(
278280
"ignoreMissingKey param (%1$s) is not valid. valid usage is ignoreMissingKey=true!",
279281
ignoreMissingKey);
280-
return new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
282+
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
281283
}
282284

283285
final Map<Object, Object> valueObjs = getValues(region, keys);
@@ -365,17 +367,19 @@ public ResponseEntity<?> update(@PathVariable("region") String region,
365367
String errorMessage = String.format(
366368
"The op parameter (%1$s) is not valid. Valid values are PUT, REPLACE, or CAS.",
367369
opValue);
368-
return new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
370+
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
369371
}
370372
if (keys.length > 1) {
371373
updateMultipleKeys(region, keys, json);
372374
HttpHeaders headers = new HttpHeaders();
375+
headers.setContentType(APPLICATION_JSON_UTF8);
373376
headers.setLocation(toUri(region, StringUtils.arrayToCommaDelimitedString(keys)));
374377
return new ResponseEntity<>(headers, HttpStatus.OK);
375378
} else {
376379
// put case
377380
Object existingValue = updateSingleKey(region, keys[0], json, opValue);
378381
final HttpHeaders headers = new HttpHeaders();
382+
headers.setContentType(APPLICATION_JSON_UTF8);
379383
headers.setLocation(toUri(region, keys[0]));
380384
return new ResponseEntity<>(existingValue, headers,
381385
(existingValue == null ? HttpStatus.OK : HttpStatus.CONFLICT));
@@ -433,7 +437,7 @@ public ResponseEntity<?> updateKeys(@PathVariable("region") final String encoded
433437
String errorMessage = String.format(
434438
"The op parameter (%1$s) is not valid. Valid values are PUT, CREATE, REPLACE, or CAS.",
435439
opValue);
436-
return new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
440+
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
437441
}
438442

439443
if (decodedKeys.length > 1) {

0 commit comments

Comments
 (0)