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

Query: Fixes default to BadRequestException in case of internal errors in ServiceInterop #3399

Merged
merged 5 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,28 @@ private static bool TryCreateFromExceptionWithStackTrace(
ITrace trace,
out CosmosException cosmosException)
{
// Use the original stack trace from the inner exception.
if (exceptionWithStackTrace.InnerException is Microsoft.Azure.Documents.DocumentClientException
|| exceptionWithStackTrace.InnerException is CosmosException)
{
return ExceptionToCosmosException.TryCreateFromException(
exceptionWithStackTrace.InnerException,
trace,
out cosmosException);
}
Exception innerException = ExceptionWithStackTraceException.UnWrapMonadExcepion(exceptionWithStackTrace, trace);

if (!ExceptionToCosmosException.TryCreateFromException(
exceptionWithStackTrace.InnerException,
innerException,
trace,
out cosmosException))
{
return false;
}

cosmosException = CosmosExceptionFactory.Create(
cosmosException.StatusCode,
cosmosException.Message,
exceptionWithStackTrace.StackTrace,
headers: cosmosException.Headers,
cosmosException.Trace,
cosmosException.Error,
cosmosException.InnerException);
if (innerException is not CosmosException && innerException is not Documents.DocumentClientException)
{
cosmosException = CosmosExceptionFactory.Create(
cosmosException.StatusCode,
cosmosException.Message,
exceptionWithStackTrace.StackTrace,
headers: cosmosException.Headers,
cosmosException.Trace,
cosmosException.Error,
cosmosException.InnerException);
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,14 @@ public static async Task<PartitionedQueryExecutionInfo> GetQueryPlanWithServiceI

if (!tryGetQueryPlan.Succeeded)
{
Exception originalException = ExceptionWithStackTraceException.UnWrapMonadExcepion(tryGetQueryPlan.Exception, serviceInteropTrace);
if (originalException is CosmosException)
if (ExceptionToCosmosException.TryCreateFromException(tryGetQueryPlan.Exception, serviceInteropTrace, out CosmosException cosmosException))
{
throw originalException;
throw cosmosException;
}
else
{
throw ExceptionWithStackTraceException.UnWrapMonadExcepion(tryGetQueryPlan.Exception, serviceInteropTrace);
}

throw CosmosExceptionFactory.CreateBadRequestException(
message: originalException.Message,
headers: new Headers(),
stackTrace: tryGetQueryPlan.Exception.StackTrace,
innerException: originalException,
trace: trace);
}

return tryGetQueryPlan.Result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public async Task ServiceInterop_BadRequestContainsInnerException()
It.IsAny<Cosmos.GeospatialType>(),
It.IsAny<CancellationToken>())).ReturnsAsync(TryCatch<PartitionedQueryExecutionInfo>.FromException(innerException));

Mock<ITrace> trace = new Mock<ITrace>();
CosmosException cosmosException = await Assert.ThrowsExceptionAsync<CosmosException>(() => QueryPlanRetriever.GetQueryPlanWithServiceInteropAsync(
queryClient.Object,
new SqlQuerySpec("selectttttt * from c"),
Expand All @@ -50,8 +49,7 @@ public async Task ServiceInterop_BadRequestContainsInnerException()
hasLogicalPartitionKey: false,
geospatialType: Cosmos.GeospatialType.Geography,
useSystemPrefix: false,
trace.Object,
default));
NoOpTrace.Singleton));

Assert.AreEqual(HttpStatusCode.BadRequest, cosmosException.StatusCode);
Assert.AreEqual(innerException, cosmosException.InnerException);
Expand Down Expand Up @@ -92,5 +90,40 @@ public async Task ServiceInterop_BadRequestContainsOriginalCosmosException()

Assert.AreEqual(expectedException, cosmosException);
}

[TestMethod]
public async Task ServiceInterop_E_UNEXPECTED()
{
UnexpectedQueryPartitionProviderException innerException = new UnexpectedQueryPartitionProviderException("E_UNEXPECTED");
Mock<CosmosQueryClient> queryClient = new Mock<CosmosQueryClient>();

queryClient.Setup(c => c.TryGetPartitionedQueryExecutionInfoAsync(
It.IsAny<SqlQuerySpec>(),
It.IsAny<ResourceType>(),
It.IsAny<Documents.PartitionKeyDefinition>(),
It.IsAny<bool>(),
It.IsAny<bool>(),
It.IsAny<bool>(),
It.IsAny<bool>(),
It.IsAny<bool>(),
It.IsAny<bool>(),
It.IsAny<Cosmos.GeospatialType>(),
It.IsAny<CancellationToken>())).ReturnsAsync(TryCatch<PartitionedQueryExecutionInfo>.FromException(innerException));

CosmosException cosmosException = await Assert.ThrowsExceptionAsync<CosmosException>(() => QueryPlanRetriever.GetQueryPlanWithServiceInteropAsync(
queryClient.Object,
new SqlQuerySpec("Super secret query that triggers bug"),
ResourceType.Document,
new Documents.PartitionKeyDefinition() { Paths = new Collection<string>() { "/id" } },
hasLogicalPartitionKey: false,
geospatialType: Cosmos.GeospatialType.Geography,
useSystemPrefix: false,
NoOpTrace.Singleton));

Assert.AreEqual(HttpStatusCode.InternalServerError, cosmosException.StatusCode);
Assert.AreEqual(innerException, cosmosException.InnerException);
Assert.IsNotNull(cosmosException.Trace);
Assert.IsNotNull(cosmosException.Diagnostics);
}
}
}