Skip to content

Exception-less Streaming API for non-existing NP container scenarios#227

Merged
kirankumarkolli merged 5 commits intomasterfrom
users/kirankk/collection_not_exists
May 10, 2019
Merged

Exception-less Streaming API for non-existing NP container scenarios#227
kirankumarkolli merged 5 commits intomasterfrom
users/kirankk/collection_not_exists

Conversation

@kirankumarkolli
Copy link
Copy Markdown
Member

@kirankumarkolli kirankumarkolli commented May 10, 2019

Pull Request Template

Description

CreateItemStreamAsync targeting a NP collection with NonePartitionKeyValue should not throw exception. Expected to return status code.

Currently for partitioned collection its returning 404 and will return the same.
For GA we should make it 424 (Dependency failure) instead for dis-disambiguate it.

Type of change

  • ExecUtils moved to the path of deprecation. Related changes moved to RequestInvokerHandler

  • RequestInvokerHandler made primary entry contract for client

  • Fixed a bug introduced with FI from master, where cached ContainerSettings are not used.

  • Bug fix (non-breaking change which fixes an issue)

Closing issues

closes #233

Comment thread Microsoft.Azure.Cosmos/src/Resource/CosmosClientContextCore.cs
Few proxy methods from ExecUtils are removed.
@kirankumarkolli kirankumarkolli changed the title Stream API for non-existing NP container should return status code as result Exception-less Streaming API for non-existing NP container scenarios May 10, 2019
@kirankumarkolli kirankumarkolli merged commit 4f5d872 into master May 10, 2019
@kirankumarkolli kirankumarkolli deleted the users/kirankk/collection_not_exists branch May 10, 2019 14:50
kirankumarkolli added a commit that referenced this pull request May 10, 2019
…227)

* First cut of fix

* Refreshing syntax as per C#7

* ComosContextCore depenency on ExecUtils removed.
Few proxy methods from ExecUtils are removed.
kirankumarkolli added a commit that referenced this pull request May 12, 2019
* added new system strings (#210)

* Ignoring TestLazyIndexAllTerms (#211)

* PartitionedCRUDTest ignored (#217)

* Exception-less Streaming API for non-existing NP container scenarios (#227)

* First cut of fix

* Refreshing syntax as per C#7

* ComosContextCore depenency on ExecUtils removed.
Few proxy methods from ExecUtils are removed.

* SDK version to 3.0.0.11-preview and Direct dependency to 3.0.0.27-preview

* Cherry pick 15a9e26 into 3.0.0.11_preview (#235)

* Query routing with partition key definition (#213)

* Fix breaks for the release

* Remove unused usings

* Adding a unit test to verify setting PartitionKeyDefinition works. (#226)

* More

* Fixes for query pipeline

* Some more fixes

* Np tests and quarantine open-partition tests

* small correction

* Direct contract test pruning

* Few test fixes

* Bug fix for paramaterised queries

* Including NP query tests

* One more test fix

* Fix for MixedModeTypoes unit test

* MixedMode multi-targeting tests clean.

* Enabling Distinct and TopOrderBy for NP.

* Quarantine TestQueryCrossPartitionAggregateFunctionsWithMixedTypes
kirankumarkolli added a commit that referenced this pull request May 12, 2019
* Raising version to 22-preview

* Merge master into release (CF Pull internal API) (#115)

* Assemblyinfo clean-up (#104)

* Signed build assembly info fix

* Assembly info clean-up

* Including test projects internal visible to (#107)

* Stand By Feed and ChangeFeed pull model (#105)

* Adding initial files

* Using Etag for continuation

* Removing unused

* Refactoring to reduce variables

* Refactoring to use CompositeToken

* Adding feed test

* Refactor through Options

* Adding public methods and comments

* Routing through the point transport handler

* Moving to outer if

* Adding split logic

* Adding unit tests

* Adding logic to detect invalid continuation tokens

* Adding JSON validation

* Routing based on PKRangeId

* Renaming and adding more tests

* Moving logic into the token

* Forcing refresh on split

* Addressing final coments

* Addressing feedback

* Added test to cover CT passing

* Refactoring and adding pkrangedelegate

* Argument checks

* Moving contract to CosmosRequestMessage

* Refactoring make EnsureInitialized async

* Moving tests to a new file

* Adding PKrange assert

* Refactored back to parameters outside Options

* UT split

* Adding Start* checks

* Adding new tests and renames

* Addressing comments

* Refactoring for cache tests

* Adding comments to tests

* Adding factory method

* Addressing comments

* Refactoring PKRange outside options

* Addressing comments

* Removing StartFromBeginning

* Removing extra lines

* Removing unnecessary ToList

* Raising version to 22-preview (#113)

* fix bugs

* Fixing signing issue

* Hot fixes for CosmosSerializationOptions and query partition key issues (#207)

* Hot fixes for CosmosSerializationOptions and query partition key.

* initial commit (#193)

* Fixed formatting

* added new system strings (#210)

* Ignoring TestLazyIndexAllTerms (#211)

* PartitionedCRUDTest ignored (#217)

* Exception-less Streaming API for non-existing NP container scenarios (#227)

* First cut of fix

* Refreshing syntax as per C#7

* ComosContextCore depenency on ExecUtils removed.
Few proxy methods from ExecUtils are removed.

* SDK version to 3.0.0.11-preview and Direct dependency to 3.0.0.27-preview

* Cherry pick 15a9e26 into 3.0.0.11_preview (#235)

* Query routing with partition key definition (#213)

* Fix breaks for the release

* Remove unused usings

* Adding a unit test to verify setting PartitionKeyDefinition works. (#226)

* More

* Revert "Fixing samples build break (#236)"

This reverts commit cda8903.

* Force samples to Nuget.org only

* Fixes for query pipeline

* Some more fixes

* Np tests and quarantine open-partition tests

* small correction

* Direct contract test pruning

* Few test fixes

* Bug fix for paramaterised queries

* Including NP query tests

* One more test fix

* Fix for MixedModeTypoes unit test

* MixedMode multi-targeting tests clean.

* Enabling Distinct and TopOrderBy for NP.

* Quarantine TestQueryCrossPartitionAggregateFunctionsWithMixedTypes

* Test fixes.

* BadRequestException fixes

* Owner attribute removed
kirankumarkolli added a commit that referenced this pull request Nov 19, 2025
…ontainer doesn't exist (Direct mode) (#5500)

## Description

When operations fail with 404, it's ambiguous whether the item doesn't
exist or the parent container/database doesn't exist. This PR fixes the
SDK to properly set substatus code 1003 (OwnerResourceNotFound) when
collection resolution fails during item operations, enabling developers
to distinguish between these two fundamentally different error
conditions.

### Changes

-**SDK Fix**: Modified
`ClientCollectionCache.ResolveCollectionWithSessionContainerCleanupAsync`
to set substatus 1003 when collection resolution fails with 404 for
non-container operations
- Item/document operations now correctly get 404/1003 when container
doesn't exist (Direct mode)
- Container metadata operations continue to get 404/0 when container
doesn't exist (the container itself is the missing resource)
- Query and stored procedure operations get 404/1003 when container
doesn't exist

- **Emulator Tests**: Added parameterized tests validating substatus
code behavior in both TCP (Direct) and Gateway connection modes
  - 404/0 → item not found in existing container
  - 404/1003 → container or database doesn't exist (Direct mode)
  - Tests run with `[DataRow]` attributes for both connection modes
- Test names clarified with `_StreamingAPI` and `_TypedAPI` suffixes to
indicate which API type is being tested
- `ValidateSubStatusCodeForItemNotFoundVsContainerNotFound_StreamingAPI`
tests streaming APIs (ReadItemStreamAsync)
- `ValidateCosmosExceptionSubStatusCodeForNotFound_TypedAPI` tests
typed/non-streaming APIs (ReadItemAsync)
- `ValidateContainerMetadataRequestsHaveSubstatus0` validates that
container operations correctly return substatus 0


- **Unit Tests**: Added tests for CosmosException and ResponseMessage
substatus code handling

- **Documentation**: Added examples in `Exceptions.md` showing how to
distinguish these cases

### Direct Mode vs Gateway Mode

__Direct Mode (TCP)__:

- Fully supports substatus code 1003 for container-not-found scenarios
- Can distinguish between item-not-found (404/0) and container-not-found
(404/1003)

__Gateway Mode__:

- Known limitation: Cannot always distinguish these scenarios due to
Gateway architecture
- Returns 404/0 for both item-not-found and container-not-found
- Database-not-found scenarios still return 404/1003 in both modes

### Usage

Check `SubStatusCode` to differentiate 404 scenarios:

```csharp
try {
    var response = await container.ReadItemAsync(id, partitionKey);
} catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound) {
    if (ex.SubStatusCode == 1003) {
        // Container/database doesn't exist - may need to create it
        // (Direct mode only for container-not-found)
    } else {
        // Item doesn't exist - normal case
    }
}
```

Same pattern works with `ResponseMessage.Headers.SubStatusCode` for
stream APIs.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## Closing issues

Resolves #227
Fixes #229

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in
our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: kirankumarkolli <6880899+kirankumarkolli@users.noreply.github.com>
Co-authored-by: Yash Trivedi <yash2710@users.noreply.github.com>
Co-authored-by: yash2710 <8458233+yash2710@users.noreply.github.com>
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 this pull request may close these issues.

Exception-less Streaming API for non-existing NP container scenarios

3 participants