Fix code smells
[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.onap.cps.nfproxy.rest.controller.NfProxyController;
26 import org.onap.cps.nfproxy.rest.model.ErrorMessage;
27 import org.onap.cps.spi.exceptions.CpsException;
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;
32
33 /**
34  * Exception handler with error message return.
35  */
36 @Slf4j
37 @NoArgsConstructor(access = AccessLevel.PRIVATE)
38 @RestControllerAdvice(assignableTypes = {NfProxyController.class})
39 public class NfProxyRestExceptionHandler {
40
41     private static final String CHECK_LOGS_FOR_DETAILS = "Check logs for details.";
42
43     /**
44      * Default exception handler.
45      *
46      * @param exception the exception to handle
47      * @return response with response code 500.
48      */
49     @ExceptionHandler
50     public static ResponseEntity<Object> handleInternalServerErrorExceptions(
51         final Exception exception) {
52         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
53     }
54
55     @ExceptionHandler({CpsException.class})
56     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
57         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
58     }
59
60     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
61         if (exception.getCause() != null || !(exception instanceof CpsException)) {
62             log.error("Exception occurred", exception);
63         }
64         final ErrorMessage errorMessage = new ErrorMessage();
65         errorMessage.setStatus(status.toString());
66         errorMessage.setMessage(exception.getMessage());
67         errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() :
68             CHECK_LOGS_FOR_DETAILS);
69         return new ResponseEntity<>(errorMessage, status);
70     }
71 }