96a1cb267d7716a0e2da1eff0ac7c21a7ef5d84e
[cps.git] / cps-nf-proxy-rest / src / main / java / org / onap / cps / nfproxy / rest / exceptions / NfProxyRestExceptionHandler.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.nfproxy.rest.exceptions;
21
22 import lombok.AccessLevel;
23 import lombok.NoArgsConstructor;
24 import lombok.extern.slf4j.Slf4j;
25 import org.apache.commons.lang3.exception.ExceptionUtils;
26 import org.onap.cps.nfproxy.rest.controller.NfProxyController;
27 import org.onap.cps.nfproxy.rest.model.ErrorMessage;
28 import org.onap.cps.spi.exceptions.CpsException;
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;
33
34 /**
35  * Exception handler with error message return.
36  */
37 @Slf4j
38 @NoArgsConstructor(access = AccessLevel.PRIVATE)
39 @RestControllerAdvice(assignableTypes = {NfProxyController.class})
40 public class NfProxyRestExceptionHandler {
41
42     /**
43      * Default exception handler.
44      *
45      * @param exception the exception to handle
46      * @return response with response code 500.
47      */
48     @ExceptionHandler
49     public static ResponseEntity<Object> handleInternalServerErrorExceptions(
50         final Exception exception) {
51         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
52     }
53
54     @ExceptionHandler({CpsException.class})
55     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
56         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception));
57     }
58
59     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
60         return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception));
61     }
62
63     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final String message,
64         final String details) {
65         log.error("An error has occurred : {} Status: {} Details: {}", message, status, details);
66         final ErrorMessage errorMessage = new ErrorMessage();
67         errorMessage.setStatus(status.toString());
68         errorMessage.setMessage(message);
69         errorMessage.setDetails(details);
70         return new ResponseEntity<>(errorMessage, status);
71     }
72
73     private static String extractDetails(final CpsException exception) {
74         return exception.getCause() == null
75             ? exception.getDetails()
76             : ExceptionUtils.getStackTrace(exception.getCause());
77     }
78 }