Skip to content

Commit 2aea3ca

Browse files
committed
Include target type name in exception message for unexpected field.
1 parent 0cdf31e commit 2aea3ca

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

Dasher.Tests/DeserialiserTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void ThrowsOnUnexpectedField()
106106
() => deserialiser.Deserialise(bytes));
107107

108108
Assert.Equal(typeof(UserScore), ex.TargetType);
109-
Assert.Equal("Encountered unexpected field \"SUPRISE\".", ex.Message);
109+
Assert.Equal("Encountered unexpected field \"SUPRISE\" for type \"UserScore\".", ex.Message);
110110
}
111111

112112
[Fact]
@@ -181,7 +181,7 @@ public void ThrowsOnDuplicateField()
181181
() => deserialiser.Deserialise(bytes));
182182

183183
Assert.Equal(typeof(UserScore), ex.TargetType);
184-
Assert.Equal("Encountered duplicate field \"Score\".", ex.Message);
184+
Assert.Equal("Encountered duplicate field \"Score\" for type \"UserScore\".", ex.Message);
185185
}
186186

187187
[Fact]

Dasher/DeserialiserEmitter.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,10 @@ public static Func<Unpacker, DasherContext, object> Build(Type type, UnexpectedF
240240
var notSeenLabel = ilg.DefineLabel();
241241
ilg.Emit(OpCodes.Brfalse, notSeenLabel);
242242
{
243-
ilg.Emit(OpCodes.Ldstr, "Encountered duplicate field \"{0}\".");
243+
ilg.Emit(OpCodes.Ldstr, "Encountered duplicate field \"{0}\" for type \"{1}\".");
244244
ilg.Emit(OpCodes.Ldloc, key);
245-
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object)}));
245+
ilg.Emit(OpCodes.Ldstr, type.Name);
246+
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object)}));
246247
throwException();
247248
}
248249

@@ -267,9 +268,10 @@ public static Func<Unpacker, DasherContext, object> Build(Type type, UnexpectedF
267268
// If we got here then the property was not recognised. Either throw or ignore, depending upon configuration.
268269
if (unexpectedFieldBehaviour == UnexpectedFieldBehaviour.Throw)
269270
{
270-
ilg.Emit(OpCodes.Ldstr, "Encountered unexpected field \"{0}\".");
271+
ilg.Emit(OpCodes.Ldstr, "Encountered unexpected field \"{0}\" for type \"{1}\".");
271272
ilg.Emit(OpCodes.Ldloc, key);
272-
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object)}));
273+
ilg.Emit(OpCodes.Ldstr, type.Name);
274+
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object)}));
273275
throwException();
274276
}
275277
else

0 commit comments

Comments
 (0)