Handling CAS errors

The error returned for a CAS mismatch is the same that is returned when trying to insert an already-existing document. The error code is not ambiguous since the CAS option is only accepted (and only makes sense) for documents which already exist.

Is the above statemat always true? We have KeyExistsError even in delete operation?

Reply please!!! :pensive:

@socketman2016 that’s interesting, we do have an open bug for this behavior in the Java client, but I think you’re using the JS client correct?

On Java at least, this only happens when using the subdoc API to insert a document: with non-subdoc you should get a KeyExistsError instead.

As a workaround for now, you can regard a CAS mismatch error as a KeyExistsError, when inserting a doc using subdoc.

but I think you’re using the JS client correct?

Yes , I use NodeJS client

Can you (or any body) verify that In couchnode client it is safe to assume CAS mismatch error as a KeyExistsError and the statement is true regardless of operation,insert,upsert,delete,subdoc

No as mentioned, only when you’re inserting a doc using subdoc is that a safe assumption.

How can I handle CAS mismatch In CRUD ? What error code we have?

How we can inserting a doc using subdoc?? there is no option in nodejs

https://github.com/couchbase/couchnode/blob/master/lib/bucket.js#L3307
https://github.com/couchbase/couchnode/blob/master/lib/bucket.js#L3576-L3596

Well the JS client is outside my wheelhouse tbh (I work on the Java SDK)… paging @brett19

1 Like

@brett19 can you please reply

Reply please , it is very important question

Hey @socketman2016,

Sorry for the delay in answering this question!

Can you (or any body) verify that In couchnode client it is safe to assume CAS mismatch error as a KeyExistsError and the statement is true regardless of operation,insert,upsert,delete,subdoc

This is nearly true. KeyExistsError will appear for insert when they key exists, regardless of any kind of CAS usage, since CAS is ignored on an insert. In the other cases, the KeyExistsError will appear when a CAS mismatch occurs.

Cheers, Brett