getAnyReplica queries throws DocumentUnretrievableError for non-existent keys

Hello,

I’m using “couchbase”: “^4.3.1” in my Node.js application. We use Couchbase as a caching mechanism (2 hours) for some data received from another endpoint.

I observed that once a document expires (or it doesn’t exist), when querying non-existent keys from the primary node, I receive DocumentNotFoundError as expected. However, based on the current code, we’re also querying the replicas 2 times (using getAnyReplica). For the same non-existent keys, I consistently receive DocumentUnretrievableError from the replicas.

No matter if I call the replica with a fresh key or an expired one, the response from getAnyReplica is the same error:

DocumentUnretrievableError: document unretrievable] {
  cause: [Error: document_irretrievable (102)] {
    ctxtype: 'key_value',
    code: 102,
    id: 'super-random-not-found-key',
    opaque: 20,
    cas: Cas<0>,
    status_code: 1,
    enhanced_error_info: undefined,
    retry_attempts: 0,
    retry_reasons: []
  },
  context: KeyValueErrorContext {
    status_code: 1,
    opaque: 20,
    cas: Cas<0>,
    key: undefined,
    bucket: undefined,
    collection: undefined,
    scope: undefined,
    context: '',
    ref: ''
  }
}

I want to modify the code a little bit to make it handle the responses more gracefully and I’m in a dilemma if doing something like this if (error instanceof DocumentNotFoundError || error instanceof DocumentUnretrievableError) { would be a correct approach.

Is throwing DocumentUnretrievableError expected? If not, how would you recommend I handle DocumentUnretrievableError?

Thank you!

Hi @tudor

Yes this is expected. A regular non-replica collection.get() call will fail with DocumentNotFoundError, while if a collection.getAnyReplica() call fails with any reason (including that a document wasn’t present), then DocumentUnretrievableError is raised.

1 Like

Thank you so much for the response!

Just to confirm, is there any scenario where the collection.getAnyReplica() call would respond with DocumentNotFoundError or is always the UnretrievableError?

It should always be the UnretrievableError.

2 Likes

Sorry to bother you, but could you provide me the documentation that covers this? I couldn’t find a specific place where this gets mentioned.

It’s here in the API doc -

And here in the rfc -

1 Like