Skip to content

Conflict 409 errors not returning a reason when failing on trigger #681

Closed

Description

We are continuously addressing and improving the SDK, if possible, make sure the problem persist in the latest SDK version.

Describe the bug
In the case where an issue occurs while executing a pre-trigger insert, the API appears to correctly throw a Conflict 409 response, however, the response contains no reason to indicate what has gone wrong.

To Reproduce
From #667

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Cosmos.Scripts;
using Newtonsoft.Json;

namespace Cosmos409
{
    public class Program
    {
        public const string LocalConnectionString =
            "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;";

        public const string DatabaseName = "test";
        public const string ContainerName = "jobs";
        public const string SetJobNumberTrigger = "SetJobNumber";

        public static async Task Main(string[] args)
        {
            var cosmosClient = new CosmosClientBuilder(LocalConnectionString).Build();

            try
            {
                await cosmosClient.GetDatabase(DatabaseName).DeleteAsync();
            } catch (CosmosException)
            {
                // data doesn't exist
            }

            var databaseResponse = await cosmosClient.CreateDatabaseAsync(DatabaseName, 400);
            var containerResponse = await databaseResponse.Database.DefineContainer(ContainerName, "/investigationKey")
                .WithUniqueKey().Path("/investigationKey").Attach()
                .WithUniqueKey().Path("/jobNumber").Attach()
                .CreateIfNotExistsAsync();

            await containerResponse.Container.Scripts.CreateTriggerAsync(new TriggerProperties
            {
                Id = SetJobNumberTrigger,
                TriggerType = TriggerType.Pre,
                TriggerOperation = TriggerOperation.Create,
                Body = @"function setJobNumber() {
                    var context = getContext();
                    var request = context.getRequest();      
                    var containerManager = context.getCollection();
                    var containerLink = containerManager.getSelfLink()

                    var documentToCreate = request.getBody();

                    var jobNumberQuery = ""SELECT VALUE MAX(r.jobNumber) from root r WHERE r.investigationKey = '"" + documentToCreate['investigationKey'] + ""'"";
                    containerManager.queryDocuments(containerLink,
                        jobNumberQuery,
                        function(err, countValue) {
                            if (err) throw new Error(err.message);
                            documentToCreate['jobNumber'] = (countValue.length > 0 ? countValue[0] : 0) + 1;
                        });

                    // update the document that will be created
                    request.setBody(documentToCreate);
                    }",
            });

            var job = new Job {Id = Guid.NewGuid(), InvestigationKey = "test"};

            await containerResponse.Container.CreateItemAsync(job, null,
                new ItemRequestOptions {PreTriggers = new List<string> {SetJobNumberTrigger}});

            job.Id = Guid.NewGuid();

            try
            {
                // this will currently fail due to the ill defined unique key, however, the 409 response doesn't indicate any reason at all
                await containerResponse.Container.CreateItemAsync(job, null,
                    new ItemRequestOptions {PreTriggers = new List<string> {SetJobNumberTrigger}});
            }
            catch (CosmosException e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("<3");
            Console.ReadLine();
        }
    }

    public class Job
    {
        [JsonProperty("id")]
        public Guid Id { get; set; }
        [JsonProperty("investigationKey")]
        public string InvestigationKey { get; set; }
        [JsonProperty("jobNumber")]
        public int JobNumber { get; set; }
    }
}

Expected behavior
Conflict 409 response with a reason to indicate to the user what has gone wrong.

Actual behavior
Conflict 409 response with an empty reason field

Environment summary
SDK Version: 3.1.1
OS Version (e.g. Windows, Linux, MacOSX) Windows

Additional context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions