2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * Copyright (C) 2021 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.rest.exceptions;
23 import lombok.extern.slf4j.Slf4j;
24 import org.onap.cps.rest.controller.AdminRestController;
25 import org.onap.cps.rest.controller.DataRestController;
26 import org.onap.cps.rest.controller.QueryRestController;
27 import org.onap.cps.rest.model.ErrorMessage;
28 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
29 import org.onap.cps.spi.exceptions.CpsAdminException;
30 import org.onap.cps.spi.exceptions.CpsException;
31 import org.onap.cps.spi.exceptions.CpsPathException;
32 import org.onap.cps.spi.exceptions.DataInUseException;
33 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
34 import org.onap.cps.spi.exceptions.DataValidationException;
35 import org.onap.cps.spi.exceptions.ModelValidationException;
36 import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.bind.annotation.ExceptionHandler;
40 import org.springframework.web.bind.annotation.RestControllerAdvice;
43 @RestControllerAdvice(assignableTypes = {AdminRestController.class, DataRestController.class,
44 QueryRestController.class})
45 public class CpsRestExceptionHandler {
47 private CpsRestExceptionHandler() {
51 * Default exception handler.
53 * @param exception the exception to handle
54 * @return response with response code 500.
57 public static ResponseEntity<Object> handleInternalServerErrorExceptions(final Exception exception) {
58 return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
61 @ExceptionHandler({ModelValidationException.class, DataValidationException.class, CpsAdminException.class,
62 CpsPathException.class})
63 public static ResponseEntity<Object> handleBadRequestExceptions(final CpsException exception) {
64 return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
67 @ExceptionHandler({NotFoundInDataspaceException.class, DataNodeNotFoundException.class})
68 public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception) {
69 return buildErrorResponse(HttpStatus.NOT_FOUND, exception);
72 @ExceptionHandler({DataInUseException.class, AlreadyDefinedException.class})
73 public static ResponseEntity<Object> handleDataInUseException(final CpsException exception) {
74 return buildErrorResponse(HttpStatus.CONFLICT, exception);
77 @ExceptionHandler({CpsException.class})
78 public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
79 return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
82 private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
83 if (exception.getCause() != null || !(exception instanceof CpsException)) {
84 log.error("Exception occurred", exception);
86 final ErrorMessage errorMessage = new ErrorMessage();
87 errorMessage.setStatus(status.toString());
88 errorMessage.setMessage(exception.getMessage());
89 errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() :
90 "Check logs for details.");
91 return new ResponseEntity<>(errorMessage, status);