From 928fbbeaeb1fd6a85179b49934ecd451a3fd2f8f Mon Sep 17 00:00:00 2001 From: Maximilian Wesener Date: Tue, 19 Mar 2024 11:54:17 +0100 Subject: [PATCH] chore(exception): 606 - Added discovery finder exception to make sure it will per saved as error. --- .../common/config/ErrorHandlingConfig.java | 8 +++++ .../domain/service/DiscoveryServiceImpl.java | 30 +++++++++++-------- .../exception/DiscoveryFinderException.java | 25 ++++++++++++++++ .../service/EdcNotificationServiceImpl.java | 3 +- 4 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 tx-backend/src/main/java/org/eclipse/tractusx/traceability/discovery/infrastructure/exception/DiscoveryFinderException.java diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/common/config/ErrorHandlingConfig.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/common/config/ErrorHandlingConfig.java index d2140a4ca5..124aa58ee8 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/common/config/ErrorHandlingConfig.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/common/config/ErrorHandlingConfig.java @@ -41,6 +41,7 @@ import org.eclipse.tractusx.traceability.common.request.exception.InvalidSortException; import org.eclipse.tractusx.traceability.common.security.TechnicalUserAuthorizationException; import org.eclipse.tractusx.traceability.contracts.domain.exception.ContractException; +import org.eclipse.tractusx.traceability.discovery.infrastructure.exception.DiscoveryFinderException; import org.eclipse.tractusx.traceability.qualitynotification.application.contract.model.CreateNotificationContractException; import org.eclipse.tractusx.traceability.qualitynotification.application.validation.UpdateQualityNotificationValidationException; import org.eclipse.tractusx.traceability.qualitynotification.domain.alert.model.exception.AlertIllegalUpdate; @@ -116,6 +117,13 @@ ResponseEntity handleAssetNotFoundException(AssetNotFoundExceptio .body(new ErrorResponse(exception.getMessage())); } + @ExceptionHandler(DiscoveryFinderException.class) + ResponseEntity handleDiscoveryFinderException(DiscoveryFinderException exception) { + log.warn("handleDiscoveryFinderException", exception); + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE) + .body(new ErrorResponse(exception.getMessage())); + } + @ExceptionHandler(PublishAssetException.class) ResponseEntity handlePublishAssetException(PublishAssetException exception) { log.warn("handlePublishAssetException", exception); diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/discovery/domain/service/DiscoveryServiceImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/discovery/domain/service/DiscoveryServiceImpl.java index 2a83692fb9..e36ca98051 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/discovery/domain/service/DiscoveryServiceImpl.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/discovery/domain/service/DiscoveryServiceImpl.java @@ -22,9 +22,10 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.eclipse.tractusx.traceability.bpn.domain.service.BpnRepository; +import org.eclipse.tractusx.traceability.common.properties.EdcProperties; import org.eclipse.tractusx.traceability.discovery.domain.model.Discovery; import org.eclipse.tractusx.traceability.discovery.domain.repository.DiscoveryRepository; -import org.eclipse.tractusx.traceability.common.properties.EdcProperties; +import org.eclipse.tractusx.traceability.discovery.infrastructure.exception.DiscoveryFinderException; import org.jetbrains.annotations.NotNull; import org.springframework.stereotype.Component; @@ -50,17 +51,22 @@ public class DiscoveryServiceImpl implements DiscoveryService { @Override public Discovery getDiscoveryByBPN(String bpn) { List discoveryList = new ArrayList<>(); - Optional optionalDiscoveryFromDiscoveryService = getOptionalDiscoveryByBpnFromDiscoveryService(bpn); - optionalDiscoveryFromDiscoveryService.ifPresent(discovery -> { - discovery.setReceiverUrls( - discovery.getReceiverUrls().stream().map( - DiscoveryServiceImpl::removeTrailingSlash - ).toList() - ); - log.info("Retrieved discovery by bpn from edcDiscoveryService receiverUrls: {}, senderUrls: {}", discovery.getReceiverUrls().toString(), discovery.getSenderUrl()); - discoveryList.add(discovery); - }); - optionalDiscoveryFromDiscoveryService.ifPresent(discoveryList::add); + try { + Optional optionalDiscoveryFromDiscoveryService = getOptionalDiscoveryByBpnFromDiscoveryService(bpn); + optionalDiscoveryFromDiscoveryService.ifPresent(discovery -> { + discovery.setReceiverUrls( + discovery.getReceiverUrls().stream().map( + DiscoveryServiceImpl::removeTrailingSlash + ).toList() + ); + log.info("Retrieved discovery by bpn from edcDiscoveryService receiverUrls: {}, senderUrls: {}", discovery.getReceiverUrls().toString(), discovery.getSenderUrl()); + discoveryList.add(discovery); + }); + optionalDiscoveryFromDiscoveryService.ifPresent(discoveryList::add); + } catch (Exception e) { + throw new DiscoveryFinderException("DiscoverFinder not reachable."); + } + Optional optionalDiscoveryFromBpnDatabase = getOptionalDiscoveryFromBpnDatabase(bpn); optionalDiscoveryFromBpnDatabase.ifPresent(discovery -> log.info("Retrieved discovery by bpn from BPN Mapping Table receiverUrls: {}, senderUrls: {}", discovery.getReceiverUrls().toString(), discovery.getSenderUrl())); optionalDiscoveryFromBpnDatabase.ifPresent(discoveryList::add); diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/discovery/infrastructure/exception/DiscoveryFinderException.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/discovery/infrastructure/exception/DiscoveryFinderException.java new file mode 100644 index 0000000000..7d7978e6e6 --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/discovery/infrastructure/exception/DiscoveryFinderException.java @@ -0,0 +1,25 @@ +/******************************************************************************** + * Copyright (c) 2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.discovery.infrastructure.exception; + +public class DiscoveryFinderException extends RuntimeException { + public DiscoveryFinderException(String message) { + super(message); + } +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/qualitynotification/domain/base/service/EdcNotificationServiceImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/qualitynotification/domain/base/service/EdcNotificationServiceImpl.java index ca464bc219..f7c98a05d5 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/qualitynotification/domain/base/service/EdcNotificationServiceImpl.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/qualitynotification/domain/base/service/EdcNotificationServiceImpl.java @@ -27,6 +27,7 @@ import org.eclipse.tractusx.traceability.common.config.AssetsAsyncConfig; import org.eclipse.tractusx.traceability.discovery.domain.model.Discovery; import org.eclipse.tractusx.traceability.discovery.domain.service.DiscoveryService; +import org.eclipse.tractusx.traceability.discovery.infrastructure.exception.DiscoveryFinderException; import org.eclipse.tractusx.traceability.qualitynotification.domain.base.InvestigationRepository; import org.eclipse.tractusx.traceability.qualitynotification.domain.base.exception.ContractNegotiationException; import org.eclipse.tractusx.traceability.qualitynotification.domain.base.exception.NoCatalogItemException; @@ -49,7 +50,7 @@ @Slf4j @RequiredArgsConstructor @Service -@Transactional +@Transactional(dontRollbackOn = DiscoveryFinderException.class) @Profile(NOT_INTEGRATION_TESTS) public class EdcNotificationServiceImpl implements EdcNotificationService {