7dc6284fccf6b86d90e7facf3f04ec52566a0247
[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-2022 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.DmiRequestException;
27 import org.onap.cps.ncmp.api.impl.exception.NcmpException;
28 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException;
29 import org.onap.cps.ncmp.rest.controller.NetworkCmProxyController;
30 import org.onap.cps.ncmp.rest.controller.NetworkCmProxyInventoryController;
31 import org.onap.cps.ncmp.rest.model.ErrorMessage;
32 import org.onap.cps.spi.exceptions.CpsException;
33 import org.onap.cps.spi.exceptions.DataValidationException;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.ExceptionHandler;
37 import org.springframework.web.bind.annotation.RestControllerAdvice;
38
39 /**
40  * Exception handler with error message return.
41  */
42 @Slf4j
43 @RestControllerAdvice(assignableTypes = {NetworkCmProxyController.class, NetworkCmProxyInventoryController.class})
44 @NoArgsConstructor(access = AccessLevel.PACKAGE)
45 public class NetworkCmProxyRestExceptionHandler {
46
47     private static final String CHECK_LOGS_FOR_DETAILS = "Check logs for details.";
48
49     /**
50      * Default exception handler.
51      *
52      * @param exception the exception to handle
53      * @return response with response code 500.
54      */
55     @ExceptionHandler
56     public static ResponseEntity<Object> handleInternalServerErrorExceptions(
57         final Exception exception) {
58         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
59     }
60
61     @ExceptionHandler({CpsException.class})
62     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
63         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
64     }
65
66     @ExceptionHandler({ServerNcmpException.class})
67     public static ResponseEntity<Object> handleServerNcmpExceptions(final ServerNcmpException exception) {
68         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
69     }
70
71     @ExceptionHandler({DmiRequestException.class})
72     public static ResponseEntity<Object> handleDmiRequestExceptions(final DmiRequestException exception) {
73         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
74     }
75
76     @ExceptionHandler({DataValidationException.class})
77     public static ResponseEntity<Object> handleDataValidatedExceptions(final DataValidationException exception) {
78         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
79     }
80
81     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
82         if (exception.getCause() != null || !(exception instanceof CpsException)) {
83             log.error("Exception occurred", exception);
84         }
85         final var errorMessage = new ErrorMessage();
86         errorMessage.setStatus(status.toString());
87         errorMessage.setMessage(exception.getMessage());
88         if (exception instanceof CpsException) {
89             errorMessage.setDetails(((CpsException) exception).getDetails());
90         } else if (exception instanceof NcmpException) {
91             errorMessage.setDetails(((NcmpException) exception).getDetails());
92         } else {
93             errorMessage.setDetails(CHECK_LOGS_FOR_DETAILS);
94         }
95         errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() :
96             CHECK_LOGS_FOR_DETAILS);
97         return new ResponseEntity<>(errorMessage, status);
98     }
99 }