简体   繁体   中英

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();

使用onErrorResume并最终返回Mono.error是此用例的正确和推荐模式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM