Skip to content

Commit

Permalink
Include received MsgPack format in exception message for unexpected f…
Browse files Browse the repository at this point in the history
…ield.
  • Loading branch information
drewnoakes committed Jun 26, 2016
1 parent 2aea3ca commit 0c63273
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dasher.Tests/DeserialiserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void ThrowsOnUnexpectedField()
() => deserialiser.Deserialise(bytes));

Assert.Equal(typeof(UserScore), ex.TargetType);
Assert.Equal("Encountered unexpected field \"SUPRISE\" for type \"UserScore\".", ex.Message);
Assert.Equal("Encountered unexpected field \"SUPRISE\" of MsgPack format \"FixStr\" for CLR type \"UserScore\".", ex.Message);
}

[Fact]
Expand Down
14 changes: 12 additions & 2 deletions Dasher/DeserialiserEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,20 @@ public static Func<Unpacker, DasherContext, object> Build(Type type, UnexpectedF
// If we got here then the property was not recognised. Either throw or ignore, depending upon configuration.
if (unexpectedFieldBehaviour == UnexpectedFieldBehaviour.Throw)
{
ilg.Emit(OpCodes.Ldstr, "Encountered unexpected field \"{0}\" for type \"{1}\".");
var format = ilg.DeclareLocal(typeof(Format));
ilg.Emit(OpCodes.Ldloc, unpacker);
ilg.Emit(OpCodes.Ldloca, format);
ilg.Emit(OpCodes.Call, typeof(Unpacker).GetMethod(nameof(Unpacker.TryPeekFormat)));
// Drop the return value: if false, 'format' will be 'Unknown' which is fine.
ilg.Emit(OpCodes.Pop);

ilg.Emit(OpCodes.Ldstr, "Encountered unexpected field \"{0}\" of MsgPack format \"{1}\" for CLR type \"{2}\".");
ilg.Emit(OpCodes.Ldloc, key);
ilg.Emit(OpCodes.Ldloc, format);
ilg.Emit(OpCodes.Box, typeof(Format));
ilg.Emit(OpCodes.Call, typeof(Format).GetMethod(nameof(Format.ToString), new Type[0]));
ilg.Emit(OpCodes.Ldstr, type.Name);
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object)}));
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object), typeof(object)}));
throwException();
}
else
Expand Down

0 comments on commit 0c63273

Please sign in to comment.