Azure / azure-cosmosdb-js-server

The JavaScript SDK for server-side programming in Azure Cosmos DB
MIT License
178 stars 153 forks source link

upsertDocument etag mismatch returns the wrong error code. #31

Open tschultz-msft opened 6 years ago

tschultz-msft commented 6 years ago

I wrote a sproc that contains the following code (calling from C#):

        var upsertOptions = {
            disableAutomaticIdGeneration: true,
            etag: doc._etag
        }

        var isAccepted = collection.upsertDocument(collectionLink, doc, upsertOptions, callback);

The error returned when the etag does not match is the following:

{
  "code": "BadRequest",
  "message": "Message: {\"Errors\":[\"Encountered exception while executing function. Exception = Error: {\\\"Errors\\\":[\\\"One of the specified pre-condition is not met\\\"]}\\r\\nStack trace: Error: {\\\"Errors\\\":[\\\"One of the specified pre-condition is not met\\\"]}\\n   at callback (bulkUpsertV1.js:53:13)\\n   at Anonymous function (bulkUpsertV1.js:751:29)\"]}\r\nActivityId: da2b9e29-da83-4e52-8ac8-cfdbbdaa465e, Request URI: /apps/DocDbApp/services/DocDbServer24/partitions/a4cb4964-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: , SDK: documentdb-dotnet-sdk/2.0.0 Host/64-bit MicrosoftWindowsNT/6.3.9600.0"
}

I would expect that the "code" is PreconditionFailed rather than BadRequest.

I'm currently working around it by checking for the error code BadRequest, and then checkign that the message contains

One of the specified pre-condition is not met

but I seek a more elegant and robust solution.