Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wahyuen opened this issue Aug 14, 2019 · 2 comments · Fixed by #753
Closed

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

wahyuen opened this issue Aug 14, 2019 · 2 comments · Fixed by #753
Assignees
Labels
bug Something isn't working

Comments

@wahyuen
Copy link
Contributor

wahyuen commented Aug 14, 2019

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

@kirankumarkolli
Copy link
Member

@ealsur can you please take a look?

@ealsur
Copy link
Member

ealsur commented Aug 30, 2019

There seems to be 2 problems:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants