3db04fcfe808f3a7a5a4e1cf9a4790faa0182641
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-service / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / rest / exceptions / NsLcmControllerExceptionHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
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  *
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 package org.onap.so.etsi.nfvo.ns.lcm.rest.exceptions;
21
22 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.exceptions.NsRequestProcessingException;
23 import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
24 import org.onap.so.etsi.nfvo.ns.lcm.rest.NsLifecycleManagementController;
25 import org.springframework.http.HttpStatus;
26 import org.springframework.http.ResponseEntity;
27 import org.springframework.web.bind.annotation.ControllerAdvice;
28 import org.springframework.web.bind.annotation.ExceptionHandler;
29
30 /**
31  * @author Waqas Ikram (waqas.ikram@est.tech)
32  *
33  */
34 @ControllerAdvice(assignableTypes = NsLifecycleManagementController.class)
35 public class NsLcmControllerExceptionHandler {
36
37     @ExceptionHandler(NsRequestProcessingException.class)
38     public ResponseEntity<InlineResponse400> handleNsRequestProcessingException(
39             final NsRequestProcessingException nsRequestProcessingException) {
40         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
41                 .body(getInlineResponse400(nsRequestProcessingException));
42     }
43
44     @ExceptionHandler(Exception.class)
45     public ResponseEntity<InlineResponse400> handleNsRequestProcessingException(final Exception exception) {
46         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new InlineResponse400()
47                 .status(HttpStatus.INTERNAL_SERVER_ERROR.value()).detail(exception.getMessage()));
48     }
49
50     private InlineResponse400 getInlineResponse400(final NsRequestProcessingException nsRequestProcessingException) {
51         if (nsRequestProcessingException.getProblemDetails() != null) {
52             return nsRequestProcessingException.getProblemDetails();
53         }
54         return new InlineResponse400().status(HttpStatus.INTERNAL_SERVER_ERROR.value())
55                 .detail(nsRequestProcessingException.getMessage());
56     }
57
58 }