Skip to content

Commit fd406f7

Browse files
author
Chris Santero
committed
throw more helpful errors from filtering transformer
1 parent 4e4a4ba commit fd406f7

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

JSONAPI.Tests/ActionFilters/DefaultFilteringTransformerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using FluentAssertions;
1010
using JSONAPI.ActionFilters;
1111
using JSONAPI.Core;
12+
using JSONAPI.Documents.Builders;
1213
using JSONAPI.QueryableTransformers;
1314
using Microsoft.VisualStudio.TestTools.UnitTesting;
1415

@@ -1147,7 +1148,7 @@ public void Filters_by_missing_nullable_double_property()
11471148
public void Does_not_filter_unknown_type()
11481149
{
11491150
Action action = () => GetArray("http://api.example.com/dummies?filter[unknownTypeField]=asdfasd");
1150-
action.ShouldThrow<HttpResponseException>().Which.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
1151+
action.ShouldThrow<JsonApiException>().Which.Error.Status.Should().Be(HttpStatusCode.BadRequest);
11511152
}
11521153

11531154
#endregion

JSONAPI/QueryableTransformers/DefaultFilteringTransformer.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Web.Http;
99
using JSONAPI.ActionFilters;
1010
using JSONAPI.Core;
11+
using JSONAPI.Documents.Builders;
1112

1213
namespace JSONAPI.QueryableTransformers
1314
{
@@ -79,11 +80,14 @@ private Expression GetPredicateBody(HttpRequestMessage request, ParameterExpress
7980
}
8081
catch (TypeRegistrationNotFoundException)
8182
{
82-
throw new HttpResponseException(HttpStatusCode.BadRequest);
83+
throw JsonApiException.CreateForBadRequest("No registration exists for the specified type");
8384
}
8485

8586
var resourceTypeField = registration.GetFieldByName(filterField);
86-
87+
if (resourceTypeField == null)
88+
throw JsonApiException.CreateForBadRequest(
89+
string.Format("No attribute {0} exists on the specified type.", filterField));
90+
8791
var queryValue = queryPair.Value;
8892
if (string.IsNullOrWhiteSpace(queryValue))
8993
queryValue = null;
@@ -100,7 +104,8 @@ private Expression GetPredicateBody(HttpRequestMessage request, ParameterExpress
100104
if (relationshipModelProperty != null)
101105
expr = GetPredicateBodyForRelationship(relationshipModelProperty, queryValue, param);
102106

103-
if (expr == null) throw new HttpResponseException(HttpStatusCode.BadRequest);
107+
if (expr == null) throw JsonApiException.CreateForBadRequest(
108+
string.Format("The attribute {0} is unsupported for filtering.", filterField));
104109

105110
workingExpr = workingExpr == null ? expr : Expression.AndAlso(workingExpr, expr);
106111
}
@@ -344,7 +349,7 @@ private Expression GetPredicateBodyForRelationship(ResourceTypeRelationship reso
344349
}
345350
catch (TypeRegistrationNotFoundException)
346351
{
347-
throw new HttpResponseException(HttpStatusCode.BadRequest);
352+
throw JsonApiException.CreateForBadRequest("No registration exists for the specified type");
348353
}
349354

350355
var prop = resourceTypeProperty.Property;

0 commit comments

Comments
 (0)