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 malformed continuation token exception type and message #3917

Merged
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));
ealsur marked this conversation as resolved.
Show resolved Hide resolved
}
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;
akotalwar marked this conversation as resolved.
Show resolved Hide resolved
}

Assert.Fail("Should never reach till here, hence ensuring that an exception is always recieved");
}
}
akotalwar marked this conversation as resolved.
Show resolved Hide resolved

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