From 80963373d832aee23423b8a4ebc48ad61eebca2c Mon Sep 17 00:00:00 2001 From: "Kristen.Herum" Date: Mon, 18 Nov 2024 15:34:12 +0100 Subject: [PATCH] Handle timeouts in WebClientFilter and ArenaForvalterClient Add TimeoutException handling in WebClientFilter for improved error messaging and status codes. Update ArenaForvalterClient to include timeout on WebClient calls with a 30-second limit, and provide detailed error messages on timeout exceptions. --- .../arenaforvalter/ArenaForvalterClient.java | 8 ++++++- .../arenaforvalter/utils/ArenaStatusUtil.java | 13 +++++++++-- .../reactivecore/utils/WebClientFilter.java | 22 +++++++++++++++---- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/apps/dolly-backend/src/main/java/no/nav/dolly/bestilling/arenaforvalter/ArenaForvalterClient.java b/apps/dolly-backend/src/main/java/no/nav/dolly/bestilling/arenaforvalter/ArenaForvalterClient.java index c3323e5fa25..27322e0edb1 100644 --- a/apps/dolly-backend/src/main/java/no/nav/dolly/bestilling/arenaforvalter/ArenaForvalterClient.java +++ b/apps/dolly-backend/src/main/java/no/nav/dolly/bestilling/arenaforvalter/ArenaForvalterClient.java @@ -16,17 +16,20 @@ import no.nav.dolly.domain.resultset.dolly.DollyPerson; import no.nav.dolly.util.IdentTypeUtil; import no.nav.dolly.util.TransactionHelperService; +import no.nav.testnav.libs.reactivecore.utils.WebClientFilter; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.time.Duration; import java.util.List; import java.util.stream.Collectors; import static java.util.Objects.nonNull; import static no.nav.dolly.bestilling.arenaforvalter.utils.ArenaStatusUtil.AAP; import static no.nav.dolly.bestilling.arenaforvalter.utils.ArenaStatusUtil.AAP115; +import static no.nav.dolly.bestilling.arenaforvalter.utils.ArenaStatusUtil.ANDREFEIL; import static no.nav.dolly.bestilling.arenaforvalter.utils.ArenaStatusUtil.BRUKER; import static no.nav.dolly.bestilling.arenaforvalter.utils.ArenaStatusUtil.DAGPENGER; import static no.nav.dolly.bestilling.arenaforvalter.utils.ArenaStatusUtil.fmtResponse; @@ -66,6 +69,9 @@ public Flux gjenopprett(RsDollyUtvidetBestilling bestilling, Dolly BestillingProgress::setArenaforvalterStatus, initStatus); }) .flatMap(miljoer -> doArenaOpprett(ordre, dollyPerson.getIdent(), miljoer) + .timeout(Duration.ofSeconds(30)) + .onErrorResume(error -> + Mono.just(fmtResponse(miljoer, ANDREFEIL, WebClientFilter.getMessage(error)))) .map(status -> futurePersist(progress, status)))); } @@ -94,7 +100,7 @@ private Mono doArenaOpprett(Arenadata arenadata, String ident, List fmtResponse(miljoe, DAGPENGER, dagpengerStatus)) )); - } else { + } else { return Flux.just(fmtResponse(miljoe, BRUKER, NOT_SUPPORTED)); } }) diff --git a/apps/dolly-backend/src/main/java/no/nav/dolly/bestilling/arenaforvalter/utils/ArenaStatusUtil.java b/apps/dolly-backend/src/main/java/no/nav/dolly/bestilling/arenaforvalter/utils/ArenaStatusUtil.java index e3fa29f5e61..7df12b55b4c 100644 --- a/apps/dolly-backend/src/main/java/no/nav/dolly/bestilling/arenaforvalter/utils/ArenaStatusUtil.java +++ b/apps/dolly-backend/src/main/java/no/nav/dolly/bestilling/arenaforvalter/utils/ArenaStatusUtil.java @@ -11,6 +11,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.util.Collection; import java.util.Map; import java.util.stream.Collectors; @@ -29,6 +30,7 @@ public class ArenaStatusUtil { public static final String AAP115 = "AAP115"; public static final String AAP = "AAP"; public static final String DAGPENGER = "DAGP"; + public static final String ANDREFEIL = "ARENA Oppretting Feil="; public static Mono getDagpengerStatus(ArenaNyeDagpengerResponse response, ErrorStatusDecoder errorStatusDecoder) { @@ -37,7 +39,7 @@ public static Mono getDagpengerStatus(ArenaNyeDagpengerResponse response .map(status -> errorStatusDecoder.getErrorText(response.getStatus(), getMessage(response.getFeilmelding()))), Flux.fromIterable(response.getNyeDagp()) .filter(nyDagP -> nonNull(nyDagP.getNyeDagpResponse())) - .map(nyDagP -> "JA".equals(nyDagP.getNyeDagpResponse().getUtfall()) ? + .map(nyDagP -> "JA".equals(nyDagP.getNyeDagpResponse().getUtfall()) ? "OK" : encodeStatus(ArenaUtils.AVSLAG + nyDagP.getNyeDagpResponse().getBegrunnelse())) .collect(Collectors.joining()), @@ -78,9 +80,16 @@ public static Mono getAapStatus(AapResponse response, ErrorStatusDecoder } } + public static String fmtResponse(Collection miljoer, String system, String status) { + + return miljoer.stream() + .map(miljo -> fmtResponse(miljo, system, status)) + .collect(Collectors.joining(",")); + } + public static String fmtResponse(String miljoe, String system, String status) { - return String.format(MILJOE_FMT, miljoe, system, encodeStatus(status)); + return MILJOE_FMT.formatted(miljoe, system, encodeStatus(status)); } public static String getMessage(String jsonFeilmelding) { diff --git a/libs/reactive-core/src/main/java/no/nav/testnav/libs/reactivecore/utils/WebClientFilter.java b/libs/reactive-core/src/main/java/no/nav/testnav/libs/reactivecore/utils/WebClientFilter.java index 83eac6dc0ec..17d1a078364 100644 --- a/libs/reactive-core/src/main/java/no/nav/testnav/libs/reactivecore/utils/WebClientFilter.java +++ b/libs/reactive-core/src/main/java/no/nav/testnav/libs/reactivecore/utils/WebClientFilter.java @@ -11,6 +11,7 @@ import java.net.SocketException; import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeoutException; @Slf4j @UtilityClass @@ -46,6 +47,10 @@ public static String getMessage(Throwable throwable) { return requestException.getCause().toString(); } + } else if (throwable instanceof TimeoutException) { + + return "Mottaker svarer ikke, eller har for lang svartid."; + } else { return throwable.getMessage(); } @@ -53,14 +58,23 @@ public static String getMessage(Throwable throwable) { public static HttpStatus getStatus(Throwable throwable) { - return throwable instanceof WebClientResponseException webClientResponseException ? - HttpStatus.valueOf(webClientResponseException.getStatusCode().value()) : - HttpStatus.INTERNAL_SERVER_ERROR; + if (throwable instanceof WebClientResponseException webClientResponseException) { + return HttpStatus.valueOf(webClientResponseException.getStatusCode().value()); + + } else if (throwable instanceof TimeoutException) { + return HttpStatus.REQUEST_TIMEOUT; + + } else { + return HttpStatus.INTERNAL_SERVER_ERROR; + } } public static void logErrorMessage(Throwable throwable) { - if (!(throwable instanceof WebClientResponseException)) { + if ((throwable instanceof WebClientResponseException webClientResponseException)) { + log.error("%s, %s".formatted(throwable.getMessage(), + webClientResponseException.getResponseBodyAsString()), throwable); + } else { log.error(throwable.getMessage(), throwable); } }