Skip to content

Commit 0c63273

Browse files
committed
Include received MsgPack format in exception message for unexpected field.
1 parent 2aea3ca commit 0c63273

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Dasher.Tests/DeserialiserTests.cs

Lines changed: 1 addition & 1 deletion
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\" for type \"UserScore\".", ex.Message);
109+
Assert.Equal("Encountered unexpected field \"SUPRISE\" of MsgPack format \"FixStr\" for CLR type \"UserScore\".", ex.Message);
110110
}
111111

112112
[Fact]

Dasher/DeserialiserEmitter.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,20 @@ public static Func<Unpacker, DasherContext, object> Build(Type type, UnexpectedF
268268
// If we got here then the property was not recognised. Either throw or ignore, depending upon configuration.
269269
if (unexpectedFieldBehaviour == UnexpectedFieldBehaviour.Throw)
270270
{
271-
ilg.Emit(OpCodes.Ldstr, "Encountered unexpected field \"{0}\" for type \"{1}\".");
271+
var format = ilg.DeclareLocal(typeof(Format));
272+
ilg.Emit(OpCodes.Ldloc, unpacker);
273+
ilg.Emit(OpCodes.Ldloca, format);
274+
ilg.Emit(OpCodes.Call, typeof(Unpacker).GetMethod(nameof(Unpacker.TryPeekFormat)));
275+
// Drop the return value: if false, 'format' will be 'Unknown' which is fine.
276+
ilg.Emit(OpCodes.Pop);
277+
278+
ilg.Emit(OpCodes.Ldstr, "Encountered unexpected field \"{0}\" of MsgPack format \"{1}\" for CLR type \"{2}\".");
272279
ilg.Emit(OpCodes.Ldloc, key);
280+
ilg.Emit(OpCodes.Ldloc, format);
281+
ilg.Emit(OpCodes.Box, typeof(Format));
282+
ilg.Emit(OpCodes.Call, typeof(Format).GetMethod(nameof(Format.ToString), new Type[0]));
273283
ilg.Emit(OpCodes.Ldstr, type.Name);
274-
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object)}));
284+
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object), typeof(object)}));
275285
throwException();
276286
}
277287
else

0 commit comments

Comments
 (0)