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

unable to access the StatusCode property when the Status of operation is “Unauthorized”. It seems to be not accessible via CosmosException unlike other exception types. #784

Closed
arunmannengal-zz opened this issue Sep 9, 2019 · 4 comments · Fixed by #785
Labels
bug Something isn't working has-repro

Comments

@arunmannengal-zz
Copy link

arunmannengal-zz commented Sep 9, 2019

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

Describe the bug
Unable to access the StatusCode property when the Status of operation is “Unauthorized”.
It seems to be not accessible via CosmosException unlike other exception types.

To Reproduce

try
{
var cosmosClient = new CosmosClient(
connectionProperties.EndpointUri,
connectionProperties.PrimaryAuthorizationKey,
new CosmosClientOptions()
{
ConnectionMode = ConnectionMode.Direct,
});
var accountProperties = cosmosClient.ReadAccountProperties( ...parameter values...) ... missed adding an operation earlier.
}
catch (CosmosException cosmosException)
{
if (cosmosException.StatusCode.Equals(HttpStatusCode.Unauthorized))
{
// This condition is never met. Instead base “Exception” is thrown
}

}
catch (Exception ex)
{
// Fails on following condition when "statusCode" is "Unauthorized"
var code = ((Microsoft.Azure.Documents.DocumentClientException)ex).StatusCode;
}

Expected behavior
CosmosException type should inlcude the Status code even for "Unauthorized" operation

Actual behavior
Base Exception is thrown without access to the "Status Code"
Environment summary
SDK Version: 3.1.1
OS Version : Windows

Additional context
Add any other context about the problem here (for example, complete stack traces or logs).

@ealsur
Copy link
Member

ealsur commented Sep 9, 2019

Is your invalid key a valid base64 value or just a random string?

@arunmannengal-zz
Copy link
Author

arunmannengal-zz commented Sep 9, 2019

It is a Valid base 64 string. Essentially I took a valid SecretKey value and replaced a character to induce this error.

@ealsur
Copy link
Member

ealsur commented Sep 9, 2019

Which character? Because I'm using your code with the Emulator credentials, and changing one of the middle letters, and it doesn't throw, because it's a valid base64 still.

Now, if I change the last ones for example, I get a FormatException, because the string is no longer a valid base64.

Check the typeof of the Exception you get.

@ealsur ealsur added bug Something isn't working has-repro labels Sep 9, 2019
@ealsur
Copy link
Member

ealsur commented Sep 9, 2019

We seem to be throwing a DCE on Unauthorized:

try
{
    var cosmosClient = new CosmosClient(
        "https://localhost:8081",
        "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIr/Jw==",
        new CosmosClientOptions()
        {
            ConnectionMode = ConnectionMode.Direct,
        });

    var document = await cosmosClient.GetContainer("test", "test").ReadItemAsync<dynamic>("test", new PartitionKey("test"));
}
catch (Exception ex)
{
    // This is a DCE
}

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