Chet
Chet

Reputation: 1225

Reactor Core - Mono - onErrorFlatmap

Is there a way in Mono to return a flatMap while there is an error(onErrorFlatMap)

My scenario is, i would need the SubscriberContext when there is an error after processing i need the same error propagated down the chain

    String test = "test";
    Mono.just(test)
            .map(Integer::valueOf)
            .onErrorMap(error -> Mono.subscriberContext()
                    .map(context -> {
                        System.out.println(error + " --   " + context.getOrDefault("APPID", null));
                        return error;
                    }))
            .subscriberContext(of("APPID", "APP-101"))
            .block();

This is the way, i found to fix it, but is there a better way?

String test = "test";
Mono.just(test)
        .map(Integer::valueOf)
        .onErrorResume(error -> Mono.subscriberContext()
                .flatMap(context -> {
                    System.out.println(error + " --   " + context.getOrDefault("APPID", null));
                    return Mono.error(error);
                }))
        .subscriberContext(of("APPID", "APP-101"))
        .block();

Upvotes: 2

Views: 479

Answers (1)

Simon Baslé
Simon Baslé

Reputation: 28301

Using onErrorResume and ultimately returning a Mono.error is the correct and recommended pattern for this use case.

Upvotes: 2

Related Questions