2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.rest.exceptions;
22 import org.apache.commons.lang3.exception.ExceptionUtils;
23 import org.hibernate.exception.ConstraintViolationException;
24 import org.onap.cps.exceptions.CpsException;
25 import org.onap.cps.exceptions.CpsNotFoundException;
26 import org.onap.cps.exceptions.CpsValidationException;
27 import org.onap.cps.rest.controller.CpsRestController;
28 import org.springframework.dao.EmptyResultDataAccessException;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.web.bind.annotation.ExceptionHandler;
32 import org.springframework.web.bind.annotation.RestControllerAdvice;
34 @RestControllerAdvice(assignableTypes = {CpsRestController.class})
35 public class CpsRestExceptionHandler {
38 * Default exception handler.
40 * @param exception the exception to handle
41 * @return response with response code 500.
44 public ResponseEntity<Object> handleInternalErrorException(Exception exception) {
45 return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
49 TODO: Rid off extra dependencies.
51 Generic exception handler and CpsException (incl. children) are the only
52 exceptions to be handled here. All the other exceptions which require a special
53 treatment should be rethrown as CpsException in the place of occurrence ->
54 e.g. persistence exceptions are to be handled in cps-ri module.
57 @ExceptionHandler({ConstraintViolationException.class})
58 public ResponseEntity<Object> handleBadRequestException(Exception exception) {
59 return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
62 @ExceptionHandler({EmptyResultDataAccessException.class})
63 public ResponseEntity<Object> handleNotFoundException(Exception exception) {
64 return buildErrorResponse(HttpStatus.NOT_FOUND, exception);
67 @ExceptionHandler({CpsException.class})
68 public ResponseEntity<Object> handleCpsException(CpsException exception) {
69 return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception));
72 @ExceptionHandler({CpsValidationException.class})
73 public ResponseEntity<Object> handleCpsValidationException(CpsException exception) {
74 return buildErrorResponse(HttpStatus.BAD_REQUEST, exception.getMessage(), extractDetails(exception));
77 @ExceptionHandler({CpsNotFoundException.class})
78 public ResponseEntity<Object> handleCpsNotFoundException(CpsException exception) {
79 return buildErrorResponse(HttpStatus.NOT_FOUND, exception.getMessage(), extractDetails(exception));
82 private static ResponseEntity<Object> buildErrorResponse(HttpStatus status, Exception exception) {
83 return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception));
86 private static ResponseEntity<Object> buildErrorResponse(HttpStatus status, String message, String details) {
87 return new ResponseEntity<>(
88 ErrorMessage.builder().status(status.toString()).message(message).details(details).build(),
93 private static String extractDetails(CpsException exception) {
94 return exception.getCause() == null
95 ? exception.getDetails()
96 : ExceptionUtils.getStackTrace(exception.getCause());