Skip to content

Commit

Permalink
Unified error messages for failed type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Koelman committed Sep 29, 2021
1 parent 2c60a42 commit f39c18b
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private void AssignStringId(IResourceIdentity identity, IIdentifiable resource,
catch (FormatException exception)
{
using IDisposable _ = state.Position.PushElement("id");
throw new DeserializationException(state.Position, null, exception.Message);
throw new ModelConversionException(state.Position, "Incompatible 'id' value found.", exception.Message);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static void AssertNoInvalidAttribute(object attributeValue, RequestAdapt

string typeName = info.AttributeType.GetFriendlyTypeName();

throw new DeserializationException(state.Position, null,
throw new ModelConversionException(state.Position, "Incompatible attribute value found.",
$"Failed to convert attribute '{info.AttributeName}' with value '{info.JsonValue}' of type '{info.JsonType}' to type '{typeName}'.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ public async Task Cannot_create_resource_with_incompatible_attribute_value()

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.Title.Should().Be("Failed to deserialize request body.");
error.Title.Should().Be("Failed to deserialize request body: Incompatible attribute value found.");
error.Detail.Should().Be("Failed to convert attribute 'bornAt' with value '12345' of type 'Number' to type 'DateTimeOffset'.");
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/attributes/bornAt");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public async Task Cannot_create_resource_for_incompatible_ID()

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.Title.Should().Be("Failed to deserialize request body.");
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
error.Detail.Should().Be($"Failed to convert '{guid}' of type 'String' to type 'Int32'.");
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/id");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public async Task Cannot_delete_resource_for_incompatible_ID()

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.Title.Should().Be("Failed to deserialize request body.");
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
error.Detail.Should().Be($"Failed to convert '{guid}' of type 'String' to type 'Int64'.");
error.Source.Pointer.Should().Be("/atomic:operations[0]/ref/id");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>

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

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.Title.Should().Be("Failed to deserialize request body.");
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
error.Detail.Should().Be("Failed to convert 'invalid-guid' of type 'String' to type 'Guid'.");
error.Source.Pointer.Should().Be("/atomic:operations[0]/data[0]/id");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>

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

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.Title.Should().Be("Failed to deserialize request body.");
error.Title.Should().Be("Failed to deserialize request body: Incompatible 'id' value found.");
error.Detail.Should().Be("Failed to convert 'invalid-guid' of type 'String' to type 'Guid'.");
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/id");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ public async Task Cannot_update_resource_for_incompatible_ID()

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

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.Title.Should().Be("Failed to deserialize request body.");
error.Title.Should().Be("Failed to deserialize request body: Incompatible attribute value found.");
error.Detail.Should().Be("Failed to convert attribute 'bornAt' with value '123.45' of type 'Number' to type 'DateTimeOffset'.");
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/attributes/bornAt");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ public async Task Cannot_create_resource_with_incompatible_attribute_value()

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>

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

Expand Down

0 comments on commit f39c18b

Please sign in to comment.