diff options
author | ToineSiebelink <toine.siebelink@est.tech> | 2020-12-08 12:08:31 +0000 |
---|---|---|
committer | ToineSiebelink <toine.siebelink@est.tech> | 2020-12-10 10:11:53 +0000 |
commit | 098a93c84aae8d8d74e7589518a576b84c38ca27 (patch) | |
tree | 97ef75d4be28415dbc71d093db1d8d27d6ac3f4c /cps-rest/src/main/java | |
parent | 9647834359b168ed8091cecaecd363cb87abe874 (diff) |
Improve CPS Exception handling
- Removing unnecessary level of CPS(Validation)Exception
- Moved exception inside spi package
- Rename exceptions are per agreed new exception hierarchy
- Use new exceptions
- Added SchemaSetAlreadDefined Exception
- Added AnchorAlreadyDefined Exception
- Ensure unique serialVersionUIDs
- Fixed CpsAdmin(Persistence)ServiceImplSpec tests (there was a mix-up about what this class was testing)
- Replaced exception builder with dedicated exception (constructors)
Issue-ID: CPS-8
Change-Id: I9cb80af4951e69ee7b0e99303853955de9d4399f
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Diffstat (limited to 'cps-rest/src/main/java')
-rwxr-xr-x | cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java | 14 | ||||
-rw-r--r-- | cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java | 34 |
2 files changed, 26 insertions, 22 deletions
diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java index 0821dca2b6..30d3e24bb9 100755 --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java @@ -31,9 +31,9 @@ import org.modelmapper.ModelMapper; import org.onap.cps.api.CpService; import org.onap.cps.api.CpsAdminService; import org.onap.cps.api.CpsModuleService; -import org.onap.cps.exceptions.CpsException; -import org.onap.cps.exceptions.CpsValidationException; import org.onap.cps.rest.api.CpsRestApi; +import org.onap.cps.spi.exceptions.CpsException; +import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.spi.model.Anchor; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.springframework.beans.factory.annotation.Autowired; @@ -75,7 +75,7 @@ public class CpsRestController implements CpsRestApi { final Anchor anchorDetails = modelMapper.map(anchor, Anchor.class); anchorDetails.setDataspaceName(dataspaceName); final String anchorName = cpsAdminService.createAnchor(anchorDetails); - return new ResponseEntity<String>(anchorName, HttpStatus.CREATED); + return new ResponseEntity<>(anchorName, HttpStatus.CREATED); } @Override @@ -154,7 +154,7 @@ public class CpsRestController implements CpsRestApi { @GetMapping("/json-object/{id}") public final ResponseEntity<String> getJsonObjectById( @PathVariable("id") final int jsonObjectId) { - return new ResponseEntity<String>(cpService.getJsonById(jsonObjectId), HttpStatus.OK); + return new ResponseEntity<>(cpService.getJsonById(jsonObjectId), HttpStatus.OK); } /** @@ -175,7 +175,7 @@ public class CpsRestController implements CpsRestApi { final Gson gson = new Gson(); gson.fromJson(getJsonString(multipartFile), Object.class); } catch (final JsonSyntaxException e) { - throw new CpsValidationException("Not a valid JSON file.", e); + throw new DataValidationException("Not a valid JSON file.", e.getMessage(), e); } } @@ -183,7 +183,7 @@ public class CpsRestController implements CpsRestApi { try { final File file = File.createTempFile("tempFile", ".yang"); file.deleteOnExit(); - try (OutputStream outputStream = new FileOutputStream(file)) { + try (final OutputStream outputStream = new FileOutputStream(file)) { outputStream.write(multipartFile.getBytes()); } return file; @@ -200,4 +200,4 @@ public class CpsRestController implements CpsRestApi { throw new CpsException(e); } } -}
\ No newline at end of file +} diff --git a/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java b/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java index 94226b78c0..fc0164f5e0 100644 --- a/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java @@ -20,11 +20,14 @@ package org.onap.cps.rest.exceptions; import org.apache.commons.lang3.exception.ExceptionUtils; -import org.onap.cps.exceptions.CpsException; -import org.onap.cps.exceptions.CpsNotFoundException; -import org.onap.cps.exceptions.CpsValidationException; import org.onap.cps.rest.controller.CpsRestController; import org.onap.cps.rest.model.ErrorMessage; +import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException; +import org.onap.cps.spi.exceptions.CpsException; +import org.onap.cps.spi.exceptions.DataValidationException; +import org.onap.cps.spi.exceptions.ModelValidationException; +import org.onap.cps.spi.exceptions.NotFoundInDataspaceException; +import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -39,32 +42,33 @@ public class CpsRestExceptionHandler { * @param exception the exception to handle * @return response with response code 500. */ - @ExceptionHandler - public ResponseEntity<Object> handleInternalErrorException(final Exception exception) { + @ExceptionHandler public static ResponseEntity<Object> handleInternalServerErrorExceptions( + final Exception exception) { return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception); } - @ExceptionHandler({CpsException.class}) - public ResponseEntity<Object> handleCpsException(final CpsException exception) { - return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception)); - } - - @ExceptionHandler({CpsValidationException.class}) - public ResponseEntity<Object> handleCpsValidationException(final CpsException exception) { + @ExceptionHandler({ModelValidationException.class, DataValidationException.class, + SchemaSetAlreadyDefinedException.class, AnchorAlreadyDefinedException.class}) + public static ResponseEntity<Object> handleBadRequestExceptions(final CpsException exception) { return buildErrorResponse(HttpStatus.BAD_REQUEST, exception.getMessage(), extractDetails(exception)); } - @ExceptionHandler({CpsNotFoundException.class}) - public ResponseEntity<Object> handleCpsNotFoundException(final CpsException exception) { + @ExceptionHandler({NotFoundInDataspaceException.class}) + public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception) { return buildErrorResponse(HttpStatus.NOT_FOUND, exception.getMessage(), extractDetails(exception)); } + @ExceptionHandler({CpsException.class}) + public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) { + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception)); + } + private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) { return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception)); } private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final String message, - final String details) { + final String details) { final ErrorMessage errorMessage = new ErrorMessage(); errorMessage.setStatus(status.toString()); errorMessage.setMessage(message); |