X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-rest%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fcps%2Frest%2Fexceptions%2FCpsRestExceptionHandler.java;h=0437e70fe52c0fec79cf14061ab105b342d5c006;hb=63be201ed60ca0d0b16ebe5a1a9d3a8e3f7b8482;hp=94226b78c081230de7cc8a49d924bb9d726c1ba0;hpb=b1740000e774ca633309e640f190cdcd74aa8241;p=cps.git 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 old mode 100644 new mode 100755 index 94226b78c..0437e70fe --- 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 @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2020 Pantheon.tech + * Copyright (C) 2021 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,20 +20,33 @@ 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 lombok.extern.slf4j.Slf4j; +import org.onap.cps.rest.controller.AdminRestController; +import org.onap.cps.rest.controller.DataRestController; +import org.onap.cps.rest.controller.QueryRestController; import org.onap.cps.rest.model.ErrorMessage; +import org.onap.cps.spi.exceptions.AlreadyDefinedException; +import org.onap.cps.spi.exceptions.CpsAdminException; +import org.onap.cps.spi.exceptions.CpsException; +import org.onap.cps.spi.exceptions.CpsPathException; +import org.onap.cps.spi.exceptions.DataInUseException; +import org.onap.cps.spi.exceptions.DataNodeNotFoundException; +import org.onap.cps.spi.exceptions.DataValidationException; +import org.onap.cps.spi.exceptions.ModelValidationException; +import org.onap.cps.spi.exceptions.NotFoundInDataspaceException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; -@RestControllerAdvice(assignableTypes = {CpsRestController.class}) +@Slf4j +@RestControllerAdvice(assignableTypes = {AdminRestController.class, DataRestController.class, + QueryRestController.class}) public class CpsRestExceptionHandler { + private CpsRestExceptionHandler() { + } + /** * Default exception handler. * @@ -40,41 +54,40 @@ public class CpsRestExceptionHandler { * @return response with response code 500. */ @ExceptionHandler - public ResponseEntity handleInternalErrorException(final Exception exception) { + public static ResponseEntity handleInternalServerErrorExceptions(final Exception exception) { return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception); } - @ExceptionHandler({CpsException.class}) - public ResponseEntity handleCpsException(final CpsException exception) { - return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception)); + @ExceptionHandler({ModelValidationException.class, DataValidationException.class, CpsAdminException.class, + CpsPathException.class}) + public static ResponseEntity handleBadRequestExceptions(final CpsException exception) { + return buildErrorResponse(HttpStatus.BAD_REQUEST, exception); } - @ExceptionHandler({CpsValidationException.class}) - public ResponseEntity handleCpsValidationException(final CpsException exception) { - return buildErrorResponse(HttpStatus.BAD_REQUEST, exception.getMessage(), extractDetails(exception)); + @ExceptionHandler({NotFoundInDataspaceException.class, DataNodeNotFoundException.class}) + public static ResponseEntity handleNotFoundExceptions(final CpsException exception) { + return buildErrorResponse(HttpStatus.NOT_FOUND, exception); } - @ExceptionHandler({CpsNotFoundException.class}) - public ResponseEntity handleCpsNotFoundException(final CpsException exception) { - return buildErrorResponse(HttpStatus.NOT_FOUND, exception.getMessage(), extractDetails(exception)); + @ExceptionHandler({DataInUseException.class, AlreadyDefinedException.class}) + public static ResponseEntity handleDataInUseException(final CpsException exception) { + return buildErrorResponse(HttpStatus.CONFLICT, exception); } - private static ResponseEntity buildErrorResponse(final HttpStatus status, final Exception exception) { - return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception)); + @ExceptionHandler({CpsException.class}) + public static ResponseEntity handleAnyOtherCpsExceptions(final CpsException exception) { + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception); } - private static ResponseEntity buildErrorResponse(final HttpStatus status, final String message, - final String details) { + private static ResponseEntity buildErrorResponse(final HttpStatus status, final Exception exception) { + if (exception.getCause() != null || !(exception instanceof CpsException)) { + log.error("Exception occurred", exception); + } final ErrorMessage errorMessage = new ErrorMessage(); errorMessage.setStatus(status.toString()); - errorMessage.setMessage(message); - errorMessage.setDetails(details); + errorMessage.setMessage(exception.getMessage()); + errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() : + "Check logs for details."); return new ResponseEntity<>(errorMessage, status); } - - private static String extractDetails(final CpsException exception) { - return exception.getCause() == null - ? exception.getDetails() - : ExceptionUtils.getStackTrace(exception.getCause()); - } }