couchbasejava-client

How to capture error while using CouchbaseAsyncCluster.openBucket call


I have the following code that creates Couchbase cluster and tried to open a bucket. The name given to the bucket is a wrong name. I want to capture the error since the bucket does not exist. Using java client version 2.7.6 and Java 11.

CouchbaseAsyncCluster cluster = 
CouchbaseAsyncCluster.create("localhost");
cluster.authenticate("Administrator", "password");
rx.Observable<AsyncBucket> bucket;
cluster.openBucket("wrongName")
        .doOnError(e -> System.out.println("error occurred"))
        .doOnNext(openbucket -> System.out.println("bucket opened"))
        .subscribe(openBucket -> System.out.println("subscription 
         complete")); 

The above code does not generate any error. What is the best way to capture the error opening bucket?


Solution

  • The issue is probably that you're subscribing to the Observable, which is starting the async network operation, but before it completes, your application is terminating.

    For testing you can try making the Observable block so that it completes before the application terminates:

        cluster.openBucket("wrongName")
                .doOnError(e -> System.out.println("error occurred"))
                .doOnNext(openbucket -> System.out.println("bucket opened"))
                .toBlocking()
                .subscribe(openBucket -> System.out.println("subscription complete"));
    

    I tested this and it gives an 'Bucket "wrongName" does not exist.' error.