Skip to content

Commit c38346d

Browse files
authored
Query: Fixes default to BadRequestException in case of internal errors in ServiceInterop (#3399)
* Don't default to BadRequestException in case of errors in ServiceInterop * Incorporate code review feedback * Fix build error * fix up failing test
1 parent 9c2015b commit c38346d

File tree

3 files changed

+56
-31
lines changed

3 files changed

+56
-31
lines changed

Microsoft.Azure.Cosmos/src/Query/Core/ExceptionToCosmosException.cs

+14-18
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,28 @@ private static bool TryCreateFromExceptionWithStackTrace(
7676
ITrace trace,
7777
out CosmosException cosmosException)
7878
{
79-
// Use the original stack trace from the inner exception.
80-
if (exceptionWithStackTrace.InnerException is Microsoft.Azure.Documents.DocumentClientException
81-
|| exceptionWithStackTrace.InnerException is CosmosException)
82-
{
83-
return ExceptionToCosmosException.TryCreateFromException(
84-
exceptionWithStackTrace.InnerException,
85-
trace,
86-
out cosmosException);
87-
}
79+
Exception innerException = ExceptionWithStackTraceException.UnWrapMonadExcepion(exceptionWithStackTrace, trace);
8880

8981
if (!ExceptionToCosmosException.TryCreateFromException(
90-
exceptionWithStackTrace.InnerException,
82+
innerException,
9183
trace,
9284
out cosmosException))
9385
{
9486
return false;
9587
}
9688

97-
cosmosException = CosmosExceptionFactory.Create(
98-
cosmosException.StatusCode,
99-
cosmosException.Message,
100-
exceptionWithStackTrace.StackTrace,
101-
headers: cosmosException.Headers,
102-
cosmosException.Trace,
103-
cosmosException.Error,
104-
cosmosException.InnerException);
89+
if (innerException is not CosmosException && innerException is not Documents.DocumentClientException)
90+
{
91+
cosmosException = CosmosExceptionFactory.Create(
92+
cosmosException.StatusCode,
93+
cosmosException.Message,
94+
exceptionWithStackTrace.StackTrace,
95+
headers: cosmosException.Headers,
96+
cosmosException.Trace,
97+
cosmosException.Error,
98+
cosmosException.InnerException);
99+
}
100+
105101
return true;
106102
}
107103

Microsoft.Azure.Cosmos/src/Query/Core/QueryPlan/QueryPlanRetriever.cs

+6-10
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,14 @@ public static async Task<PartitionedQueryExecutionInfo> GetQueryPlanWithServiceI
7676

7777
if (!tryGetQueryPlan.Succeeded)
7878
{
79-
Exception originalException = ExceptionWithStackTraceException.UnWrapMonadExcepion(tryGetQueryPlan.Exception, serviceInteropTrace);
80-
if (originalException is CosmosException)
79+
if (ExceptionToCosmosException.TryCreateFromException(tryGetQueryPlan.Exception, serviceInteropTrace, out CosmosException cosmosException))
8180
{
82-
throw originalException;
81+
throw cosmosException;
82+
}
83+
else
84+
{
85+
throw ExceptionWithStackTraceException.UnWrapMonadExcepion(tryGetQueryPlan.Exception, serviceInteropTrace);
8386
}
84-
85-
throw CosmosExceptionFactory.CreateBadRequestException(
86-
message: originalException.Message,
87-
headers: new Headers(),
88-
stackTrace: tryGetQueryPlan.Exception.StackTrace,
89-
innerException: originalException,
90-
trace: trace);
9187
}
9288

9389
return tryGetQueryPlan.Result;

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Query/QueryPlanRetrieverTests.cs

+36-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public async Task ServiceInterop_BadRequestContainsInnerException()
4141
It.IsAny<Cosmos.GeospatialType>(),
4242
It.IsAny<CancellationToken>())).ReturnsAsync(TryCatch<PartitionedQueryExecutionInfo>.FromException(innerException));
4343

44-
Mock<ITrace> trace = new Mock<ITrace>();
4544
CosmosException cosmosException = await Assert.ThrowsExceptionAsync<CosmosException>(() => QueryPlanRetriever.GetQueryPlanWithServiceInteropAsync(
4645
queryClient.Object,
4746
new SqlQuerySpec("selectttttt * from c"),
@@ -50,8 +49,7 @@ public async Task ServiceInterop_BadRequestContainsInnerException()
5049
hasLogicalPartitionKey: false,
5150
geospatialType: Cosmos.GeospatialType.Geography,
5251
useSystemPrefix: false,
53-
trace.Object,
54-
default));
52+
NoOpTrace.Singleton));
5553

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

9391
Assert.AreEqual(expectedException, cosmosException);
9492
}
93+
94+
[TestMethod]
95+
public async Task ServiceInterop_E_UNEXPECTED()
96+
{
97+
UnexpectedQueryPartitionProviderException innerException = new UnexpectedQueryPartitionProviderException("E_UNEXPECTED");
98+
Mock<CosmosQueryClient> queryClient = new Mock<CosmosQueryClient>();
99+
100+
queryClient.Setup(c => c.TryGetPartitionedQueryExecutionInfoAsync(
101+
It.IsAny<SqlQuerySpec>(),
102+
It.IsAny<ResourceType>(),
103+
It.IsAny<Documents.PartitionKeyDefinition>(),
104+
It.IsAny<bool>(),
105+
It.IsAny<bool>(),
106+
It.IsAny<bool>(),
107+
It.IsAny<bool>(),
108+
It.IsAny<bool>(),
109+
It.IsAny<bool>(),
110+
It.IsAny<Cosmos.GeospatialType>(),
111+
It.IsAny<CancellationToken>())).ReturnsAsync(TryCatch<PartitionedQueryExecutionInfo>.FromException(innerException));
112+
113+
CosmosException cosmosException = await Assert.ThrowsExceptionAsync<CosmosException>(() => QueryPlanRetriever.GetQueryPlanWithServiceInteropAsync(
114+
queryClient.Object,
115+
new SqlQuerySpec("Super secret query that triggers bug"),
116+
ResourceType.Document,
117+
new Documents.PartitionKeyDefinition() { Paths = new Collection<string>() { "/id" } },
118+
hasLogicalPartitionKey: false,
119+
geospatialType: Cosmos.GeospatialType.Geography,
120+
useSystemPrefix: false,
121+
NoOpTrace.Singleton));
122+
123+
Assert.AreEqual(HttpStatusCode.InternalServerError, cosmosException.StatusCode);
124+
Assert.AreEqual(innerException, cosmosException.InnerException);
125+
Assert.IsNotNull(cosmosException.Trace);
126+
Assert.IsNotNull(cosmosException.Diagnostics);
127+
}
95128
}
96129
}

0 commit comments

Comments
 (0)