From 0c63273cdd642013f030dad4520f92d6218eeb41 Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Sun, 26 Jun 2016 21:48:10 +0100 Subject: [PATCH] Include received MsgPack format in exception message for unexpected field. --- Dasher.Tests/DeserialiserTests.cs | 2 +- Dasher/DeserialiserEmitter.cs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Dasher.Tests/DeserialiserTests.cs b/Dasher.Tests/DeserialiserTests.cs index a90a3d1..dc6da08 100644 --- a/Dasher.Tests/DeserialiserTests.cs +++ b/Dasher.Tests/DeserialiserTests.cs @@ -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] diff --git a/Dasher/DeserialiserEmitter.cs b/Dasher/DeserialiserEmitter.cs index b0c91c8..2810cf2 100644 --- a/Dasher/DeserialiserEmitter.cs +++ b/Dasher/DeserialiserEmitter.cs @@ -268,10 +268,20 @@ public static Func 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