Skip to content

Commit ddd2498

Browse files
authored
Merge branch 'master' into ernestoc/stringCompareTo
2 parents e155b8f + 51c9d9c commit ddd2498

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

Microsoft.Azure.Cosmos/src/ReadFeed/ReadFeedIteratorCore.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ namespace Microsoft.Azure.Cosmos.ReadFeed
99
using System.Threading;
1010
using System.Threading.Tasks;
1111
using Microsoft.Azure.Cosmos.CosmosElements;
12-
using Microsoft.Azure.Cosmos.Diagnostics;
12+
using Microsoft.Azure.Cosmos.Json;
1313
using Microsoft.Azure.Cosmos.Pagination;
1414
using Microsoft.Azure.Cosmos.Query.Core;
15+
using Microsoft.Azure.Cosmos.Query.Core.Exceptions;
1516
using Microsoft.Azure.Cosmos.Query.Core.Monads;
1617
using Microsoft.Azure.Cosmos.ReadFeed.Pagination;
18+
using Microsoft.Azure.Cosmos.Resource.CosmosExceptions;
1719
using Microsoft.Azure.Cosmos.Routing;
1820
using Microsoft.Azure.Cosmos.Tracing;
21+
using Microsoft.Azure.Documents;
1922

2023
/// <summary>
2124
/// Cosmos feed stream iterator. This is used to get the query responses with a Stream content
@@ -113,7 +116,25 @@ public ReadFeedIteratorCore(
113116
else
114117
{
115118
CosmosString tokenAsString = (CosmosString)token;
116-
state = ReadFeedState.Continuation(CosmosElement.Parse(tokenAsString.Value));
119+
try
120+
{
121+
state = ReadFeedState.Continuation(CosmosElement.Parse(tokenAsString.Value));
122+
}
123+
catch (Exception exception) when (exception.InnerException is JsonParseException)
124+
{
125+
MalformedContinuationTokenException malformedContinuationTokenException = new MalformedContinuationTokenException(exception.Message);
126+
throw CosmosExceptionFactory.CreateBadRequestException(
127+
message: $"Malformed Continuation Token: {tokenAsString}.",
128+
headers: CosmosQueryResponseMessageHeaders.ConvertToQueryHeaders(
129+
new Headers(),
130+
default,
131+
default,
132+
(int)SubStatusCodes.MalformedContinuationToken,
133+
default),
134+
stackTrace: exception.StackTrace,
135+
innerException: malformedContinuationTokenException,
136+
trace: null);
137+
}
117138
}
118139

119140
FeedRangeState<ReadFeedState> feedRangeState = new FeedRangeState<ReadFeedState>(feedRange, state);

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemLinqTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,32 @@ public async Task ItemLINQQueryTest()
211211
Assert.AreEqual(itemList[1].id, queriable.ToList()[0].id);
212212
}
213213

214+
[TestMethod]
215+
public void ItemLINQQueryWithInvalidContinuationTokenTest()
216+
{
217+
string malformedString = "Malformed String";
218+
FeedIterator<ToDoActivity> feedIterator = this.Container.GetItemLinqQueryable<ToDoActivity>().ToFeedIterator();
219+
220+
while (feedIterator.HasMoreResults)
221+
{
222+
IOrderedQueryable<ToDoActivity> querable = this.Container.GetItemLinqQueryable<ToDoActivity>(
223+
continuationToken: malformedString);
224+
try
225+
{
226+
FeedIterator<ToDoActivity> iterator = querable.ToFeedIterator();
227+
}
228+
catch (CosmosException exception)
229+
{
230+
Assert.IsTrue(exception.StatusCode == System.Net.HttpStatusCode.BadRequest);
231+
Assert.IsTrue(exception.SubStatusCode == (int)Documents.SubStatusCodes.MalformedContinuationToken);
232+
Assert.IsTrue(exception.Message.Contains(malformedString));
233+
return;
234+
}
235+
236+
Assert.Fail("Should never reach till here, hence ensuring that an exception is always recieved");
237+
}
238+
}
239+
214240
[TestMethod]
215241
public async Task ItemLINQQueryWithContinuationTokenTest()
216242
{

0 commit comments

Comments
 (0)