6729329779f4d8a7609158c25831cfdeb962e97d
[cps.git] / cps-ncmp-rest / src / main / java / org / onap / cps / ncmp / rest / exceptions / NetworkCmProxyRestExceptionHandler.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.rest.exceptions;
22
23 import lombok.AccessLevel;
24 import lombok.NoArgsConstructor;
25 import lombok.extern.slf4j.Slf4j;
26 import org.onap.cps.ncmp.api.impl.exception.NcmpException;
27 import org.onap.cps.ncmp.rest.controller.NetworkCmProxyController;
28 import org.onap.cps.ncmp.rest.model.ErrorMessage;
29 import org.onap.cps.spi.exceptions.CpsException;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.web.bind.annotation.ExceptionHandler;
33 import org.springframework.web.bind.annotation.RestControllerAdvice;
34
35 /**
36  * Exception handler with error message return.
37  */
38 @Slf4j
39 @NoArgsConstructor(access = AccessLevel.PRIVATE)
40 @RestControllerAdvice(assignableTypes = {NetworkCmProxyController.class})
41 public class NetworkCmProxyRestExceptionHandler {
42
43     private static final String CHECK_LOGS_FOR_DETAILS = "Check logs for details.";
44
45     /**
46      * Default exception handler.
47      *
48      * @param exception the exception to handle
49      * @return response with response code 500.
50      */
51     @ExceptionHandler
52     public static ResponseEntity<Object> handleInternalServerErrorExceptions(
53         final Exception exception) {
54         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
55     }
56
57     @ExceptionHandler({CpsException.class})
58     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
59         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
60     }
61
62     @ExceptionHandler({NcmpException.class})
63     public static ResponseEntity<Object> handleNcmpExceptions(final NcmpException exception) {
64         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
65     }
66
67     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
68         if (exception.getCause() != null || !(exception instanceof CpsException)) {
69             log.error("Exception occurred", exception);
70         }
71         final var errorMessage = new ErrorMessage();
72         errorMessage.setStatus(status.toString());
73         errorMessage.setMessage(exception.getMessage());
74         if (exception instanceof CpsException) {
75             errorMessage.setDetails(((CpsException) exception).getDetails());
76         } else if (exception instanceof NcmpException) {
77             errorMessage.setDetails(((NcmpException) exception).getDetails());
78         } else {
79             errorMessage.setDetails(CHECK_LOGS_FOR_DETAILS);
80         }
81         errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() :
82             CHECK_LOGS_FOR_DETAILS);
83         return new ResponseEntity<>(errorMessage, status);
84     }
85 }