Skip to content

Commit 6244ef7

Browse files
author
Bart Koelman
committed
Unified error messages for failed type conversion
1 parent 5ea961f commit 6244ef7

File tree

10 files changed

+13
-13
lines changed

10 files changed

+13
-13
lines changed

src/JsonApiDotNetCore/Serialization/RequestAdapters/ResourceIdentityAdapter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private void AssignStringId(IResourceIdentity identity, IIdentifiable resource,
189189
catch (FormatException exception)
190190
{
191191
using IDisposable _ = state.Position.PushElement("id");
192-
throw new DeserializationException(state.Position, null, exception.Message);
192+
throw new ModelConversionException(state.Position, "Incompatible 'id' value found.", exception.Message);
193193
}
194194
}
195195
}

src/JsonApiDotNetCore/Serialization/RequestAdapters/ResourceObjectAdapter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private static void AssertNoInvalidAttribute(object attributeValue, RequestAdapt
9696

9797
string typeName = info.AttributeType.GetFriendlyTypeName();
9898

99-
throw new DeserializationException(state.Position, null,
99+
throw new ModelConversionException(state.Position, "Incompatible attribute value found.",
100100
$"Failed to convert attribute '{info.AttributeName}' with value '{info.JsonValue}' of type '{info.JsonType}' to type '{typeName}'.");
101101
}
102102
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Creating/AtomicCreateResourceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ public async Task Cannot_create_resource_with_incompatible_attribute_value()
804804

805805
ErrorObject error = responseDocument.Errors[0];
806806
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
807-
error.Title.Should().Be("Failed to deserialize request body.");
807+
error.Title.Should().Be("Failed to deserialize request body: Incompatible attribute value found.");
808808
error.Detail.Should().Be("Failed to convert attribute 'bornAt' with value '12345' of type 'Number' to type 'DateTimeOffset'.");
809809
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/attributes/bornAt");
810810
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Creating/AtomicCreateResourceWithClientGeneratedIdTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public async Task Cannot_create_resource_for_incompatible_ID()
225225

226226
ErrorObject error = responseDocument.Errors[0];
227227
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
228-
error.Title.Should().Be("Failed to deserialize request body.");
228+
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
229229
error.Detail.Should().Be($"Failed to convert '{guid}' of type 'String' to type 'Int32'.");
230230
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/id");
231231
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Deleting/AtomicDeleteResourceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ public async Task Cannot_delete_resource_for_incompatible_ID()
575575

576576
ErrorObject error = responseDocument.Errors[0];
577577
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
578-
error.Title.Should().Be("Failed to deserialize request body.");
578+
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
579579
error.Detail.Should().Be($"Failed to convert '{guid}' of type 'String' to type 'Int64'.");
580580
error.Source.Pointer.Should().Be("/atomic:operations[0]/ref/id");
581581
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Updating/Relationships/AtomicReplaceToManyRelationshipTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
523523

524524
ErrorObject error = responseDocument.Errors[0];
525525
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
526-
error.Title.Should().Be("Failed to deserialize request body.");
526+
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
527527
error.Detail.Should().Be($"Failed to convert '{guid}' of type 'String' to type 'Int16'.");
528528
error.Source.Pointer.Should().Be("/atomic:operations[0]/ref/id");
529529
}
@@ -955,7 +955,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
955955

956956
ErrorObject error = responseDocument.Errors[0];
957957
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
958-
error.Title.Should().Be("Failed to deserialize request body.");
958+
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
959959
error.Detail.Should().Be("Failed to convert 'invalid-guid' of type 'String' to type 'Guid'.");
960960
error.Source.Pointer.Should().Be("/atomic:operations[0]/data[0]/id");
961961
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Updating/Relationships/AtomicUpdateToOneRelationshipTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
762762

763763
ErrorObject error = responseDocument.Errors[0];
764764
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
765-
error.Title.Should().Be("Failed to deserialize request body.");
765+
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
766766
error.Detail.Should().Be("Failed to convert 'invalid-guid' of type 'String' to type 'Guid'.");
767767
error.Source.Pointer.Should().Be("/atomic:operations[0]/ref/id");
768768
}
@@ -1168,7 +1168,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
11681168

11691169
ErrorObject error = responseDocument.Errors[0];
11701170
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
1171-
error.Title.Should().Be("Failed to deserialize request body.");
1171+
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
11721172
error.Detail.Should().Be("Failed to convert 'invalid-guid' of type 'String' to type 'Guid'.");
11731173
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/id");
11741174
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Updating/Resources/AtomicUpdateResourceTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ public async Task Cannot_update_resource_for_incompatible_ID()
14251425

14261426
ErrorObject error = responseDocument.Errors[0];
14271427
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
1428-
error.Title.Should().Be("Failed to deserialize request body.");
1428+
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
14291429
error.Detail.Should().Be($"Failed to convert '{guid}' of type 'String' to type 'Int32'.");
14301430
error.Source.Pointer.Should().Be("/atomic:operations[0]/ref/id");
14311431
}
@@ -1621,7 +1621,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
16211621

16221622
ErrorObject error = responseDocument.Errors[0];
16231623
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
1624-
error.Title.Should().Be("Failed to deserialize request body.");
1624+
error.Title.Should().Be("Failed to deserialize request body: Incompatible attribute value found.");
16251625
error.Detail.Should().Be("Failed to convert attribute 'bornAt' with value '123.45' of type 'Number' to type 'DateTimeOffset'.");
16261626
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/attributes/bornAt");
16271627
}

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ public async Task Cannot_create_resource_with_incompatible_attribute_value()
797797

798798
ErrorObject error = responseDocument.Errors[0];
799799
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
800-
error.Title.Should().Be("Failed to deserialize request body.");
800+
error.Title.Should().Be("Failed to deserialize request body: Incompatible attribute value found.");
801801
error.Detail.Should().Be("Failed to convert attribute 'dueAt' with value 'not-a-valid-time' of type 'String' to type 'Nullable<DateTimeOffset>'.");
802802
error.Source.Pointer.Should().Be("/data/attributes/dueAt");
803803

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/UpdateResourceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
12521252

12531253
ErrorObject error = responseDocument.Errors[0];
12541254
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
1255-
error.Title.Should().Be("Failed to deserialize request body.");
1255+
error.Title.Should().Be("Failed to deserialize request body: Incompatible attribute value found.");
12561256
error.Detail.Should().Match("Failed to convert attribute 'dueAt' with value '*start*end*' of type 'Object' to type 'Nullable<DateTimeOffset>'.");
12571257
error.Source.Pointer.Should().Be("/data/attributes/dueAt");
12581258

0 commit comments

Comments
 (0)