Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
return more useful exception message for pagination errors
  • Loading branch information
Chris Santero committed Apr 13, 2015
commit 6f2e16b8b82f8932c729ece343aad3773e220fb7
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void ApplyPagination_returns_400_if_page_number_is_negative()
{
GetArray("http://api.example.com/dummies?page[number]=-4&page[size]=4");
};
action.ShouldThrow<HttpResponseException>().And.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
action.ShouldThrow<QueryableTransformException>().And.Message.Should().Be("page.number must be not be negative.");
}

[TestMethod]
Expand All @@ -130,7 +130,7 @@ public void ApplyPagination_returns_400_if_page_size_is_negative()
{
GetArray("http://api.example.com/dummies?page[number]=0&page[size]=-4");
};
action.ShouldThrow<HttpResponseException>().And.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
action.ShouldThrow<QueryableTransformException>().And.Message.Should().Be("page.size must be not be negative.");
}

[TestMethod]
Expand All @@ -140,7 +140,7 @@ public void ApplyPagination_returns_400_if_page_number_specified_but_not_size()
{
GetArray("http://api.example.com/dummies?page[number]=0");
};
action.ShouldThrow<HttpResponseException>().And.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
action.ShouldThrow<QueryableTransformException>().And.Message.Should().Be("In order for paging to work properly, if either page.number or page.size is set, both must be.");
}

[TestMethod]
Expand All @@ -150,7 +150,7 @@ public void ApplyPagination_returns_400_if_page_size_specified_but_not_number()
{
GetArray("http://api.example.com/dummies?page[size]=0");
};
action.ShouldThrow<HttpResponseException>().And.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
action.ShouldThrow<QueryableTransformException>().And.Message.Should().Be("In order for paging to work properly, if either page.number or page.size is set, both must be.");
}

[TestMethod]
Expand Down
22 changes: 17 additions & 5 deletions JSONAPI/ActionFilters/DefaultPaginationTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,35 @@ public IQueryable<T> ApplyPagination<T>(IQueryable<T> query, HttpRequestMessage
if (kvp.Key == _pageNumberQueryParam)
{
hasPageNumberParam = true;
if (!int.TryParse(kvp.Value, out pageNumber)) throw new HttpResponseException(HttpStatusCode.BadRequest);
if (!int.TryParse(kvp.Value, out pageNumber))
throw new QueryableTransformException(
String.Format("{0} must be a positive integer.", _pageNumberQueryParam));

}
else if (kvp.Key == _pageSizeQueryParam)
{
hasPageSizeParam = true;
if (!int.TryParse(kvp.Value, out pageSize)) throw new HttpResponseException(HttpStatusCode.BadRequest);
if (!int.TryParse(kvp.Value, out pageSize))
throw new QueryableTransformException(
String.Format("{0} must be a positive integer.", _pageSizeQueryParam));
}
}

if (!hasPageNumberParam && !hasPageSizeParam)
return query;

if ((hasPageNumberParam && !hasPageSizeParam) || (!hasPageNumberParam && hasPageSizeParam))
throw new HttpResponseException(HttpStatusCode.BadRequest);
throw new QueryableTransformException(
String.Format("In order for paging to work properly, if either {0} or {1} is set, both must be.",
_pageNumberQueryParam, _pageSizeQueryParam));

if (pageNumber < 0)
throw new QueryableTransformException(
String.Format("{0} must be not be negative.", _pageNumberQueryParam));

if (pageNumber < 0 || pageSize < 0)
throw new HttpResponseException(HttpStatusCode.BadRequest);
if (pageSize < 0)
throw new QueryableTransformException(
String.Format("{0} must be not be negative.", _pageSizeQueryParam));

if (_maxPageSize != null && pageSize > _maxPageSize.Value)
pageSize = _maxPageSize.Value;
Expand Down