Skip to content
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
25 changes: 23 additions & 2 deletions Microsoft.Azure.Cosmos/src/ReadFeed/ReadFeedIteratorCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ namespace Microsoft.Azure.Cosmos.ReadFeed
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.CosmosElements;
using Microsoft.Azure.Cosmos.Diagnostics;
using Microsoft.Azure.Cosmos.Json;
using Microsoft.Azure.Cosmos.Pagination;
using Microsoft.Azure.Cosmos.Query.Core;
using Microsoft.Azure.Cosmos.Query.Core.Exceptions;
using Microsoft.Azure.Cosmos.Query.Core.Monads;
using Microsoft.Azure.Cosmos.ReadFeed.Pagination;
using Microsoft.Azure.Cosmos.Resource.CosmosExceptions;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Documents;

/// <summary>
/// Cosmos feed stream iterator. This is used to get the query responses with a Stream content
Expand Down Expand Up @@ -113,7 +116,25 @@ public ReadFeedIteratorCore(
else
{
CosmosString tokenAsString = (CosmosString)token;
state = ReadFeedState.Continuation(CosmosElement.Parse(tokenAsString.Value));
try
{
state = ReadFeedState.Continuation(CosmosElement.Parse(tokenAsString.Value));
}
catch (Exception exception) when (exception.InnerException is JsonParseException)
{
MalformedContinuationTokenException malformedContinuationTokenException = new MalformedContinuationTokenException(exception.Message);
throw CosmosExceptionFactory.CreateBadRequestException(
message: $"Malformed Continuation Token: {tokenAsString}.",
headers: CosmosQueryResponseMessageHeaders.ConvertToQueryHeaders(
new Headers(),
default,
default,
(int)SubStatusCodes.MalformedContinuationToken,
default),
stackTrace: exception.StackTrace,
innerException: malformedContinuationTokenException,
trace: null);
}
}

FeedRangeState<ReadFeedState> feedRangeState = new FeedRangeState<ReadFeedState>(feedRange, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ public async Task ItemLINQQueryTest()
Assert.AreEqual(itemList[1].id, queriable.ToList()[0].id);
}

[TestMethod]
public void ItemLINQQueryWithInvalidContinuationTokenTest()
{
string malformedString = "Malformed String";
FeedIterator<ToDoActivity> feedIterator = this.Container.GetItemLinqQueryable<ToDoActivity>().ToFeedIterator();

while (feedIterator.HasMoreResults)
{
IOrderedQueryable<ToDoActivity> querable = this.Container.GetItemLinqQueryable<ToDoActivity>(
continuationToken: malformedString);
try
{
FeedIterator<ToDoActivity> iterator = querable.ToFeedIterator();
}
catch (CosmosException exception)
{
Assert.IsTrue(exception.StatusCode == System.Net.HttpStatusCode.BadRequest);
Assert.IsTrue(exception.SubStatusCode == (int)Documents.SubStatusCodes.MalformedContinuationToken);
Assert.IsTrue(exception.Message.Contains(malformedString));
return;
}

Assert.Fail("Should never reach till here, hence ensuring that an exception is always recieved");
}
}

[TestMethod]
public async Task ItemLINQQueryWithContinuationTokenTest()
{
Expand Down