Skip to content

Commit

Permalink
Handle timeouts in WebClientFilter and ArenaForvalterClient
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
krharum committed Nov 18, 2024
1 parent 40938b9 commit 8096337
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +69,9 @@ public Flux<ClientFuture> 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))));
}

Expand Down Expand Up @@ -94,7 +100,7 @@ private Mono<String> doArenaOpprett(Arenadata arenadata, String ident, List<Stri
arenaDagpengerService.sendDagpenger(arenadata, arenaOperasjoner, ident, miljoe)
.map(dagpengerStatus -> fmtResponse(miljoe, DAGPENGER, dagpengerStatus))
));
} else {
} else {
return Flux.just(fmtResponse(miljoe, BRUKER, NOT_SUPPORTED));
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String> getDagpengerStatus(ArenaNyeDagpengerResponse response, ErrorStatusDecoder errorStatusDecoder) {

Expand All @@ -37,7 +39,7 @@ public static Mono<String> 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()),
Expand Down Expand Up @@ -78,9 +80,16 @@ public static Mono<String> getAapStatus(AapResponse response, ErrorStatusDecoder
}
}

public static String fmtResponse(Collection<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

@Slf4j
@UtilityClass
Expand Down Expand Up @@ -46,21 +47,34 @@ 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();
}
}

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);
}
}
Expand Down

0 comments on commit 8096337

Please sign in to comment.