0

I am wondering if adding functionality to the Mono that I return from an ExchangeFilterFunction will be applied to the final Mono that gets returned. Basically I am wanting to clean up some of my client code by move some of the concerns out of the client class and into ExchangeFilterFunctions that I can add to the client, but my case is a bit different than a single call.

Basically I have something similar to the below code:

    Flux.fromIterable(uris)
        .parallel()
        .runOn(Schedulers.parallel())
        .flatMap(uri -> webClient.get()
            .uri(uri)
            .headers(headers -> headers.addAll(httpHeaders))
            .retrieve()
            .onStatus(
                status -> status.is4xxClientError() || status.is5xxServerError(),
                response -> response.bodyToMono(String.class)
                    .map(body -> new MyCustomException(body, response.statusCode())))
            .bodyToMono(MyResponse.class)
            .map(x -> Tuple.of(x, null))                
            .onErrorMap(ReadTimeoutException.class, th ->
                new MyCustomException(ErrorResponseBuilder.buildError(th), HttpStatus.INTERNAL_SERVER_ERROR)))
        .doOnEach(this::log)
        .sequential();

My question is can I move out some of this logic for example the error handling and probably retry logic and additional logging to ExchangeFilterFunctions instead and if I do that will they still get applied when put into the Flux?

Also in the ExchangeFilterFunctions how would I be able to access the SubscriberContext because there is data in the context we need when logging like the unique id of the request that gets used for tracing.

Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
twreid
  • 1,453
  • 2
  • 22
  • 42

1 Answers1

0

The error handling logic could definitely be separated out, this Baeldung article is very useful to help with this

On a further note I would recommend refactoring your code so as to not have any nested maps/flatmaps. This will make your code more readable and will probably lead you to understand which parts should be extracted out. This article covers this well

Joseph Berry
  • 168
  • 1
  • 10