1

I have a functionality to store data to a couchbase array. I am using the reactive collection in couchbase 3.0 sdk.

With the below sample I am able to store the data, but I am getting CasMismatchException printed in the logs. But as it is an expected one, I don't want it to be printed, how can I achieve that ?

private Consumer<? super Throwable> upsert(String key, String value) {
    return e -> {
      if (e instanceof CasMismatchException || e instanceof DocumentExistsException) {

            defaultCollection // couchbase reactive default colection
            .get(key)
            .subscribe(
                s -> {
                  List<String> values = new ArrayList<>();
                  values.addAll(s.contentAs(ArrayList.class));
                    values.add(value);
                    defaultCollection
                        .replace(key, values, ReplaceOptions.replaceOptions().cas(s.cas()))
                        .doOnError(upsert(key,value))
                        .subscribe();
                });
      } else {
        LOGGER.error("Error on couch operation on - {}", e);
      }
    };
  }

This code prints error logs, can I avoid it ?

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
Arun Xavier
  • 763
  • 8
  • 47
  • 3
    you're subscribing as a side effect AND inside a subscribe. this is a huge reactive code smell, as you break the reactive chain so operators cannot properly react to errors or cancellation. try to better compose your chain using error-handling operators like `onErrorResume` for instance. – Simon Baslé Jun 09 '21 at 09:17
  • 1
    @SimonBaslé, I have used onErrorResume, it handles the error gracefully, but when I do a load test on that(around 1000 parallel calls), I am getting a java.util.concurrent.CompletionException and the data is getting lost. – Arun Xavier Jun 09 '21 at 10:45
  • 1
    I am using the Hooks.onErrorDropped as suggested in https://stackoverflow.com/a/60145492/4290096 , looks like I can control the error logs – Arun Xavier Jun 09 '21 at 11:39

1 Answers1

1

I solved the repeated error printing by using Hooks.onErrorDropped.

 Hooks.onErrorDropped(error -> {
            logger.log(Level.WARNING, "Exception happened:", error);
        });

But as suggested in the comments you can also go with onErrorResume. I used the onErrorDropped for my specific case. More detailed explanations can be found in the below links.

Arun Xavier
  • 763
  • 8
  • 47