Skip to content

Commit

Permalink
Simplify error handling in organization services
Browse files Browse the repository at this point in the history
#deploy-dolly-backend

Added onErrorResume to handle exceptions gracefully and avoid blocking. Streamlined status processing by checking for null values and refining status detail mapping logic. These changes enhance code robustness and maintainability.
  • Loading branch information
krharum committed Sep 16, 2024
1 parent 617955f commit e51cfd3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;
Expand Down Expand Up @@ -85,8 +86,9 @@ public OrganisasjonDeployStatus hentOrganisasjonStatus(List<String> orgnumre) {
.header(HEADER_NAV_CONSUMER_ID, CONSUMER)
.retrieve()
.bodyToMono(OrganisasjonDeployStatus.class)
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
.filter(WebClientFilter::is5xxException)))
.doOnError(WebClientFilter::logErrorMessage)
.onErrorResume(throwable -> Mono.empty())
)
.block();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public Flux<OrganisasjonDetaljer> call() {
.bodyToFlux(OrganisasjonDetaljer.class)
.doOnError(WebClientFilter::logErrorMessage)
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
.filter(WebClientFilter::is5xxException));
.filter(WebClientFilter::is5xxException))
.onErrorResume(throwable -> Flux.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static no.nav.dolly.domain.resultset.SystemTyper.ORGANISASJON_FORVALTER;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
Expand All @@ -29,8 +30,6 @@ public static List<RsOrganisasjonStatusRapport> buildOrganisasjonStatusMap(Organ

List.of(progress.getOrganisasjonsforvalterStatus()
.replace(",q", "$q")
.replace(",t", "$t")
.replace(",u", "$u")
.split("\\$")).forEach(status -> {
String[] environMsg = status.split(":", 2);
if (environMsg.length < 2) {
Expand Down Expand Up @@ -65,9 +64,11 @@ public static List<RsOrganisasjonStatusRapport> buildOrganisasjonStatusMap(Organ
}

private static String getOrgStatusDetailForMiljo(List<OrganisasjonDeployStatus.OrgStatus> orgStatuser, String miljo) {
return orgStatuser.stream()

return nonNull(orgStatuser) ? orgStatuser.stream()
.filter(orgStatus -> miljo.equals(orgStatus.getEnvironment()))
.findFirst().orElseGet(OrganisasjonDeployStatus.OrgStatus::new)
.getDetails();
.map(status -> "%s %s".formatted(status.getDetails(),
status.getDetails().contains("feil") ? status.getError() : ""))
.findFirst().orElse("") : "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static no.nav.dolly.bestilling.organisasjonforvalter.domain.OrganisasjonStatusDTO.Status.ERROR;
import static no.nav.dolly.bestilling.organisasjonforvalter.domain.OrganisasjonStatusDTO.Status.FAILED;
import static no.nav.dolly.util.CurrentAuthentication.getUserId;
import static org.apache.commons.lang3.BooleanUtils.isNotTrue;
import static org.apache.commons.lang3.BooleanUtils.isTrue;
import static org.apache.logging.log4j.util.Strings.isBlank;
import static org.apache.logging.log4j.util.Strings.isNotBlank;
Expand Down Expand Up @@ -75,13 +76,15 @@ public RsOrganisasjonBestillingStatus fetchBestillingStatusById(Long bestillingI
.orElseThrow(() -> new NotFoundException("Fant ikke bestilling med id " + bestillingId));

OrganisasjonBestillingProgress bestillingProgress;
List<OrgStatus> orgStatusList;
List<OrgStatus> orgStatusList = null;

try {
bestillingProgress = progressService.fetchOrganisasjonBestillingProgressByBestillingsId(bestillingId)
.stream().findFirst().orElseThrow(() -> new NotFoundException("Status ikke funnet for bestillingId " + bestillingId));

orgStatusList = getOrgforvalterStatus(bestilling, bestillingProgress);
if (isNotTrue(bestilling.getFerdig())) {
orgStatusList = getOrgforvalterStatus(bestilling, bestillingProgress);
}

} catch (WebClientResponseException e) {
log.info("Status ennå ikke opprettet for bestilling");
Expand Down Expand Up @@ -272,17 +275,21 @@ private List<OrgStatus> getOrgforvalterStatus(OrganisasjonBestilling bestilling,

log.info("Status for org deploy på org: {} - {}", bestillingProgress.getOrganisasjonsnummer(), organisasjonDeployStatus);

var orgStatus = organisasjonDeployStatus.getOrgStatus()
.getOrDefault(bestillingProgress.getOrganisasjonsnummer(), emptyList());
if (nonNull(organisasjonDeployStatus)) {
var orgStatus = organisasjonDeployStatus.getOrgStatus()
.getOrDefault(bestillingProgress.getOrganisasjonsnummer(), emptyList());

updateBestilling(bestilling, orgStatus);
updateBestilling(bestilling, orgStatus);

var forvalterStatus = orgStatus.stream()
.map(org -> org.getEnvironment() + ":" + forvalterStatusDetails(org))
.collect(Collectors.joining(","));
bestillingProgress.setOrganisasjonsforvalterStatus(forvalterStatus);
var forvalterStatus = orgStatus.stream()
.map(org -> org.getEnvironment() + ":" + forvalterStatusDetails(org))
.collect(Collectors.joining(","));
bestillingProgress.setOrganisasjonsforvalterStatus(forvalterStatus);
return orgStatus;

return orgStatus;
} else {
return emptyList();
}
}

private String toJson(Object object) {
Expand Down

0 comments on commit e51cfd3

Please sign in to comment.