-
Notifications
You must be signed in to change notification settings - Fork 16
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
Update Program.cs #2
base: master
Are you sure you want to change the base?
Conversation
The original program.cs returned the following error: NotFound error occurred: Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: 404 Substatus: 0 Reason: ({ "Errors": [ "Resource Not Found" ] }). StatusCode = NotFound; SubStatusCode = 0;
Console.WriteLine("Item in database with id: {0} already exists\n", andersenFamilyResponse.Resource.Id); | ||
} | ||
// Upsert the item, i.e. if it exists it gets updated, otherwise it gets created | ||
ItemResponse<Family> andersenFamilyResponse = await this.container.UpsertItemAsync<Family>(andersenFamily, new PartitionKey(andersenFamily.LastName)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this beats the purpose of the original code, while I agree that Upsert solves the exception, the code was showing how to handle a scenario where an item might Not exist.
Possible try/catch CosmosException when StatusCode == NotFound
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ealsur Thanks for the feedback. I added try catch semantics to the code. Keep in mind that the method Container.ReadItemAsync returns an exception when the code runs. Therefore, since the Cosmos Database and its container are empty the catch block will always execute and the items will get created in each catch clause.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any code fix should not break the intent of the original code, rather fix it by adding the explicit catch
Since the Method Container.ReadItemAsync<T> will throw an exception "Resource Not Found (404)" I added try-catch semantics to catch it and act accordingly.
The original program.cs returned the following error:
NotFound error occurred: Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: 404 Substatus: 0 Reason: ({
"Errors": [
"Resource Not Found"
]
}).
StatusCode = NotFound;
SubStatusCode = 0;
Purpose
Does this introduce a breaking change?
Pull Request Type
What kind of change does this Pull Request introduce?
How to Test
What to Check
Verify that the following are valid
Other Information