Skip to content

Commit 1ea2f53

Browse files
committed
Improved error message
1 parent b7e31a1 commit 1ea2f53

File tree

8 files changed

+13
-13
lines changed

8 files changed

+13
-13
lines changed

src/Thinktecture.Runtime.Extensions.AspNetCore/AspNetCore/ModelBinding/ThinktectureModelBinderBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private static void CheckKey(ModelBindingContext bindingContext, ValueProviderRe
127127
{
128128
if (_disallowDefaultValues && key.Equals(_keyDefaultValue))
129129
{
130-
bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, $"Cannot convert null to type \"{typeof(T).Name}\" because it doesn't allow default values.");
130+
bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, $"Cannot convert the value {_keyDefaultValue} to type \"{typeof(T).Name}\" because it doesn't allow default values.");
131131
return;
132132
}
133133

src/Thinktecture.Runtime.Extensions.Json/Text/Json/Serialization/ThinktectureJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public ThinktectureJsonConverter(JsonSerializerOptions options)
8989
}
9090

9191
if (_disallowDefaultValues && key.Equals(_keyDefaultValue))
92-
throw new JsonException($"Cannot convert null to type \"{typeof(T).Name}\" because it doesn't allow default values.");
92+
throw new JsonException($"Cannot convert the value {_keyDefaultValue} to type \"{typeof(T).Name}\" because it doesn't allow default values.");
9393

9494
var validationError = T.Validate(key, null, out var obj);
9595

src/Thinktecture.Runtime.Extensions.MessagePack/Formatters/ThinktectureStructMessagePackFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions
6868
}
6969

7070
if (_disallowDefaultValues && key.Equals(_keyDefaultValue))
71-
throw new MessagePackSerializationException($"Cannot convert null to type \"{typeof(T).Name}\" because it doesn't allow default values.");
71+
throw new MessagePackSerializationException($"Cannot convert the value {_keyDefaultValue} to type \"{typeof(T).Name}\" because it doesn't allow default values.");
7272

7373
return Deserialize(key);
7474
}

src/Thinktecture.Runtime.Extensions.Newtonsoft.Json/Json/ThinktectureNewtonsoftJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
7878
}
7979

8080
if (_disallowDefaultValues && key.Equals(_keyDefaultValue))
81-
throw new JsonException($"Cannot convert null to type \"{typeof(T).Name}\" because it doesn't allow default values.");
81+
throw new JsonException($"Cannot convert the value {_keyDefaultValue} to type \"{typeof(T).Name}\" because it doesn't allow default values.");
8282

8383
var validationError = T.Validate(key, null, out var obj);
8484

test/Thinktecture.Runtime.Extensions.AspNetCore.Tests/AspNetCore/ModelBinding/ThinktectureModelBinderTests/BindModelAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void Should_try_bind_keyed_value_object_when_value_is_null_or_default()
8181
.Should().Throw<Exception>().WithMessage("Cannot convert null to type \"IntBasedStructValueObject\".");
8282

8383
FluentActions.Invoking(() => Bind<IntBasedStructValueObjectDoesNotAllowDefaultStructs>("0")) // AllowDefaultStructs = true
84-
.Should().Throw<Exception>().WithMessage("Cannot convert null to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
84+
.Should().Throw<Exception>().WithMessage("Cannot convert the value 0 to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
8585

8686
// nullable struct - string
8787
Bind<StringBasedStructValueObject?>(null).Should().Be(null);

test/Thinktecture.Runtime.Extensions.Json.Tests/Text/Json/Serialization/ThinktectureJsonConverterFactoryTests/ReadJson.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void Should_try_deserialize_keyed_value_object_when_value_is_null_or_defa
8484
.Should().Throw<JsonException>().WithMessage("The JSON value could not be converted to Thinktecture.Runtime.Tests.TestValueObjects.IntBasedStructValueObject. Path: $ | LineNumber: 0 | BytePositionInLine: 4.");
8585

8686
FluentActions.Invoking(() => Deserialize<IntBasedStructValueObjectDoesNotAllowDefaultStructs>("0")) // AllowDefaultStructs = true
87-
.Should().Throw<JsonException>().WithMessage("Cannot convert null to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
87+
.Should().Throw<JsonException>().WithMessage("Cannot convert the value 0 to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
8888

8989
// nullable struct - string
9090
Deserialize<StringBasedStructValueObject?>("null").Should().Be(null);
@@ -102,7 +102,7 @@ public void Should_try_deserialize_keyed_value_object_when_value_is_null_or_defa
102102
public void Should_throw_if_AllowDefaultStructs_is_disabled_on_keyed_value_object_and_value_is_null_and_default()
103103
{
104104
FluentActions.Invoking(() => Deserialize<GenericClass<IntBasedStructValueObjectDoesNotAllowDefaultStructs>>("{\"Property\": 0 }"))
105-
.Should().Throw<JsonException>().WithMessage("Cannot convert null to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
105+
.Should().Throw<JsonException>().WithMessage("Cannot convert the value 0 to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
106106

107107
FluentActions.Invoking(() => Deserialize<GenericClass<StringBasedStructValueObject>>("{\"Property\": null }"))
108108
.Should().Throw<JsonException>().WithMessage("Cannot convert null to type \"StringBasedStructValueObject\" because it doesn't allow default values.");
@@ -315,7 +315,7 @@ public void Should_throw_if_complex_value_object_property_has_AllowDefaultStruct
315315
.Should().NotThrow();
316316

317317
FluentActions.Invoking(() => Deserialize<ComplexValueObjectDoesNotAllowDefaultStructsWithIntBasedStruct>("{ \"Property\": 0 }"))
318-
.Should().Throw<JsonException>().WithMessage("Cannot convert null to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
318+
.Should().Throw<JsonException>().WithMessage("Cannot convert the value 0 to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
319319

320320
FluentActions.Invoking(() => Deserialize<ComplexValueObjectDoesNotAllowDefaultStructsWithStringBasedStruct>("{ \"Property\": null }"))
321321
.Should().Throw<JsonException>().WithMessage("Cannot convert null to type \"StringBasedStructValueObject\" because it doesn't allow default values.");

test/Thinktecture.Runtime.Extensions.MessagePack.Tests/Formatters/ThinktectureMessagePackFormatterTests/RoundtripSerialize.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void Should_try_deserialize_keyed_value_object_when_value_is_null_or_defa
9696

9797
FluentActions.Invoking(() => RoundTrip(0, IntBasedStructValueObjectDoesNotAllowDefaultStructs.Create(-1))) // AllowDefaultStructs = true
9898
.Should().Throw<MessagePackSerializationException>()
99-
.WithInnerException<MessagePackSerializationException>().WithMessage("Cannot convert null to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
99+
.WithInnerException<MessagePackSerializationException>().WithMessage("Cannot convert the value 0 to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
100100

101101
// nullable struct - string
102102
RoundTrip((object)null, (StringBasedStructValueObject?)null);
@@ -117,7 +117,7 @@ public void Should_throw_if_AllowDefaultStructs_is_disabled_on_keyed_value_objec
117117
{
118118
FluentActions.Invoking(() => RoundTrip(new GenericClass<int>(0), (GenericClass<IntBasedStructValueObjectDoesNotAllowDefaultStructs>)null))
119119
.Should().Throw<MessagePackSerializationException>()
120-
.WithInnerException<MessagePackSerializationException>().WithMessage("Cannot convert null to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
120+
.WithInnerException<MessagePackSerializationException>().WithMessage("Cannot convert the value 0 to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
121121

122122
FluentActions.Invoking(() => RoundTrip(new GenericClass<object>(null), (GenericClass<StringBasedStructValueObject>)null))
123123
.Should().Throw<MessagePackSerializationException>()
@@ -367,7 +367,7 @@ public void Should_throw_if_complex_value_object_property_has_AllowDefaultStruct
367367

368368
FluentActions.Invoking(() => RoundTrip(new GenericClass<int>(0), ComplexValueObjectDoesNotAllowDefaultStructsWithIntBasedStruct.Create(IntBasedStructValueObjectDoesNotAllowDefaultStructs.Create(0))))
369369
.Should().Throw<MessagePackSerializationException>()
370-
.WithInnerException<MessagePackSerializationException>().WithMessage("Cannot convert null to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
370+
.WithInnerException<MessagePackSerializationException>().WithMessage("Cannot convert the value 0 to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
371371

372372
FluentActions.Invoking(() => RoundTrip(new GenericClass<object>(null), ComplexValueObjectDoesNotAllowDefaultStructsWithStringBasedStruct.Create(StringBasedStructValueObject.Create(""))))
373373
.Should().Throw<MessagePackSerializationException>()

test/Thinktecture.Runtime.Extensions.Newtonsoft.Json.Tests/Json/ThinktectureNewtonsoftJsonConverterTests/ReadJson.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void Should_try_deserialize_keyed_value_object_when_value_is_null_or_defa
8585
.Should().Throw<JsonException>().WithMessage("Error converting value {null} to type 'System.Int32'. Path '', line 1, position 4.");
8686

8787
FluentActions.Invoking(() => Deserialize<IntBasedStructValueObjectDoesNotAllowDefaultStructs>("0")) // AllowDefaultStructs = true
88-
.Should().Throw<JsonException>().WithMessage("Cannot convert null to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
88+
.Should().Throw<JsonException>().WithMessage("Cannot convert the value 0 to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
8989

9090
// nullable struct - string
9191
Deserialize<StringBasedStructValueObject?>("null").Should().Be(null);
@@ -270,7 +270,7 @@ public void Should_throw_if_complex_value_object_property_has_AllowDefaultStruct
270270
.Should().NotThrow();
271271

272272
FluentActions.Invoking(() => Deserialize<ComplexValueObjectDoesNotAllowDefaultStructsWithIntBasedStruct>("{ \"Property\": 0 }"))
273-
.Should().Throw<JsonException>().WithMessage("Cannot convert null to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
273+
.Should().Throw<JsonException>().WithMessage("Cannot convert the value 0 to type \"IntBasedStructValueObjectDoesNotAllowDefaultStructs\" because it doesn't allow default values.");
274274

275275
FluentActions.Invoking(() => Deserialize<ComplexValueObjectDoesNotAllowDefaultStructsWithStringBasedStruct>("{ \"Property\": null }"))
276276
.Should().Throw<JsonException>().WithMessage("Cannot convert null to type \"StringBasedStructValueObject\" because it doesn't allow default values.");

0 commit comments

Comments
 (0)