693ae513dda4d2ec716d7119adb6954a96551653
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.service.v3;
22
23 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicyController;
24 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
25 import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.ProblemDetails;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.http.*;
29 import org.springframework.stereotype.Service;
30 import org.springframework.web.reactive.function.client.WebClientException;
31 import org.springframework.web.reactive.function.client.WebClientResponseException;
32 import reactor.core.publisher.Mono;
33
34 import java.lang.invoke.MethodHandles;
35 import java.math.BigDecimal;
36
37 @Service
38 public class ErrorHandlingService {
39
40     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
41     public Mono<ResponseEntity<ProblemDetails>> handleError(Throwable throwable) {
42         if (throwable instanceof WebClientResponseException) {
43             WebClientResponseException e = (WebClientResponseException) throwable;
44             return createErrorResponse(e.getResponseBodyAsString(), e.getStatusCode());
45         } else if (throwable instanceof WebClientException) {
46             WebClientException e = (WebClientException) throwable;
47             return createErrorResponse(e.getMessage(), HttpStatus.BAD_GATEWAY);
48         } else if (throwable instanceof ServiceException) {
49             ServiceException e = (ServiceException) throwable;
50             return createErrorResponse(e.getMessage(), e.getHttpStatus());
51         } else {
52             return createErrorResponse(throwable.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
53         }
54     }
55
56     public Mono<ResponseEntity<ProblemDetails>> createErrorResponse(String errorBody, HttpStatusCode statusCode) {
57         logger.debug("Error content: {}, with status code {}", errorBody, statusCode);
58         ProblemDetails problemDetail = new ProblemDetails().type("about:blank");
59         problemDetail.setDetail(errorBody);
60         problemDetail.setStatus(new BigDecimal(statusCode.value()));
61         HttpHeaders headers = new HttpHeaders();
62         headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
63         return Mono.just(new ResponseEntity<>(problemDetail, headers, statusCode));
64     }
65 }