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

Update Program.cs #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

dar10
Copy link

@dar10 dar10 commented Feb 11, 2020

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?

[ ] Yes
[ ] No

Pull Request Type

What kind of change does this Pull Request introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[ ] Other... Please describe:

How to Test

  • Get the code
git clone [repo-address]
cd [repo-name]
git checkout [branch-name]
npm install
  • Test the code

What to Check

Verify that the following are valid

  • ...

Other Information

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;
@msftclas
Copy link

msftclas commented Feb 11, 2020

CLA assistant check
All CLA requirements met.

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));
Copy link
Member

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

Copy link
Author

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.

Copy link
Member

@ealsur ealsur left a 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.
@dar10 dar10 requested a review from ealsur February 12, 2020 04:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants