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.onap.cps.exceptions.CpsException;
24 import org.onap.cps.exceptions.CpsNotFoundException;
25 import org.onap.cps.exceptions.CpsValidationException;
26 import org.onap.cps.rest.controller.CpsRestController;
27 import org.onap.cps.rest.model.ErrorMessage;
28 import org.springframework.http.HttpStatus;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.web.bind.annotation.ExceptionHandler;
31 import org.springframework.web.bind.annotation.RestControllerAdvice;
33 @RestControllerAdvice(assignableTypes = {CpsRestController.class})
34 public class CpsRestExceptionHandler {
37 * Default exception handler.
39 * @param exception the exception to handle
40 * @return response with response code 500.
43 public ResponseEntity<Object> handleInternalErrorException(final Exception exception) {
44 return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
47 @ExceptionHandler({CpsException.class})
48 public ResponseEntity<Object> handleCpsException(final CpsException exception) {
49 return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception));
52 @ExceptionHandler({CpsValidationException.class})
53 public ResponseEntity<Object> handleCpsValidationException(final CpsException exception) {
54 return buildErrorResponse(HttpStatus.BAD_REQUEST, exception.getMessage(), extractDetails(exception));
57 @ExceptionHandler({CpsNotFoundException.class})
58 public ResponseEntity<Object> handleCpsNotFoundException(final CpsException exception) {
59 return buildErrorResponse(HttpStatus.NOT_FOUND, exception.getMessage(), extractDetails(exception));
62 private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
63 return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception));
66 private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final String message,
67 final String details) {
68 final ErrorMessage errorMessage = new ErrorMessage();
69 errorMessage.setStatus(status.toString());
70 errorMessage.setMessage(message);
71 errorMessage.setDetails(details);
72 return new ResponseEntity<>(errorMessage, status);
75 private static String extractDetails(final CpsException exception) {
76 return exception.getCause() == null
77 ? exception.getDetails()
78 : ExceptionUtils.getStackTrace(exception.getCause());