Handling Errors in Gocb

Hello,

I was trying out the Couchbase’s official go lang lib. I have been using NodeJS SDK for a long time on couchbase.

I have just 1 question: How do I handle errors in gocb? In NodeJS counterpart there was an Error object associated so I could easily capture 404, Conflicts etc.

But in GoCB as per the documentation it returns the default error type on return. On checking that documentation it’s just a string… Like for eg:

bucket.Get() returns “Key not found.” in error for a 404.

It may seem very dirty to do something like

if error == “Key not found.”

Is there any better way to detect errors?

Just for reference this is how I handle errors in NodeJS SDK.

err is the error coming from Couchbase NodeJS, and I use the err.code value (number) to determine the type of error.

var KEY_EXISTS_ALREADY = 12;

if (err.code === KEY_EXISTS_ALREADY) {
}

Hey @vasu,
The gocb library exports a number of errors that can be directly compared. In your case, you can simply do:

if err && err == gocb.ErrKeyAlreadyExists) {
  // ...
}

Cheers, Brett

Hi Brett,

Thanks for the reply! I will give it a try. :slight_smile: