2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * Modifications 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
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.rest.exceptions;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.validation.ValidationException;
26 import lombok.extern.slf4j.Slf4j;
27 import org.onap.cps.rest.controller.AdminRestController;
28 import org.onap.cps.rest.controller.DataRestController;
29 import org.onap.cps.rest.controller.QueryRestController;
30 import org.onap.cps.rest.model.ErrorMessage;
31 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
32 import org.onap.cps.spi.exceptions.CpsAdminException;
33 import org.onap.cps.spi.exceptions.CpsException;
34 import org.onap.cps.spi.exceptions.CpsPathException;
35 import org.onap.cps.spi.exceptions.DataInUseException;
36 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
37 import org.onap.cps.spi.exceptions.DataValidationException;
38 import org.onap.cps.spi.exceptions.ModelValidationException;
39 import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
40 import org.springframework.http.HttpMethod;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.ExceptionHandler;
44 import org.springframework.web.bind.annotation.RestControllerAdvice;
47 @RestControllerAdvice(assignableTypes = {AdminRestController.class, DataRestController.class,
48 QueryRestController.class})
49 public class CpsRestExceptionHandler {
51 private CpsRestExceptionHandler() {
55 * Default exception handler.
57 * @param exception the exception to handle
58 * @return response with response code 500.
61 public static ResponseEntity<Object> handleInternalServerErrorExceptions(final Exception exception) {
62 return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
65 @ExceptionHandler({ModelValidationException.class, DataValidationException.class, CpsAdminException.class,
66 CpsPathException.class})
67 public static ResponseEntity<Object> handleBadRequestExceptions(final CpsException exception) {
68 return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
71 @ExceptionHandler({ValidationException.class})
72 public static ResponseEntity<Object> handleBadRequestExceptions(final ValidationException validationException) {
73 return buildErrorResponse(HttpStatus.BAD_REQUEST, validationException);
76 @ExceptionHandler({NotFoundInDataspaceException.class, DataNodeNotFoundException.class})
77 public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception,
78 final HttpServletRequest request) {
79 return buildErrorResponse(HttpMethod.GET.matches(request.getMethod())
80 ? HttpStatus.NOT_FOUND : HttpStatus.BAD_REQUEST, exception);
83 @ExceptionHandler({DataInUseException.class, AlreadyDefinedException.class})
84 public static ResponseEntity<Object> handleDataInUseException(final CpsException exception) {
85 return buildErrorResponse(HttpStatus.CONFLICT, exception);
88 @ExceptionHandler({CpsException.class})
89 public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
90 return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
93 private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
94 if (exception.getCause() != null || !(exception instanceof CpsException)) {
95 log.error("Exception occurred", exception);
97 final var errorMessage = new ErrorMessage();
98 errorMessage.setStatus(status.toString());
99 errorMessage.setMessage(exception.getMessage());
100 errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() :
101 "Check logs for details.");
102 return new ResponseEntity<>(errorMessage, status);