Search code examples
javacouchbaseproject-reactorspring-data-couchbasereactive-streams

Why does Mono.doOnError prints error logs


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 ?


Solution

  • 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.