Exceptions returned in the connection to couchbase server

Hello everyone.

I have a doubt related with the java sdk
I’m doing connection tests and when it fails the exception returned is always the same com.couchbase.client.core.config.ConfigurationException with the message could not open bucket.
In this test i’m trying to connect to a non existing server and i’m not able to get the real exception cause without parsing the throwable.getCause().getCause() that method returns
java.net.ConnectException: Conexión rehusada (In my machine, configured with an es_ES locale)

A solution could be do something like this throwable.getCause().getCause().getClass().equals(java.net.ConnectException.class) but this is not fancy code

Is there any reason for not specialize that exception (ConfigurationException) and create more concret exceptions?

Thanks to all.

Hi Bernal,

It’s unclear why the exception is not specialized, but here is a general-purpose utility method that might help.

/**
 * Returns true if the given throwable or any throwable
 * in its causal chain is an instance of the given type.
 */
public static boolean isCausedBy(Throwable throwable,
                                 Class<? extends Throwable> type) {
  if (throwable == null) {
    throw new NullPointerException();
  }

  for (Throwable t = throwable; t != null; t = t.getCause()) {
    if (type.isAssignableFrom(t.getClass())) {
      return true;
    }
  }

  return false;
}

With that method in your toolbox, you can write isCausedBy(throwable, ConnectException.class) which is slightly fancier :slight_smile:

Thanks,
David

1 Like

Thanks for the response.

Clearly is a much better way to check the exceptions but anyway the exceptions checking in the java sdk is a point to be improved.

Best regards and thanks again for the response.