Skip to content

Commit 307c389

Browse files
committed
Extend information in exception raised by ComplexTypeProvider.
1 parent 4eb5035 commit 307c389

3 files changed

Lines changed: 19 additions & 12 deletions

File tree

Dasher.Tests/DeserialiserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void ThrowsOnNonMapData()
179179

180180
var deserialiser = new Deserialiser<UserScore>();
181181
var ex = Assert.Throws<DeserialisationException>(() => deserialiser.Deserialise(bytes));
182-
Assert.Equal("Message must be encoded as a MsgPack map", ex.Message);
182+
Assert.Equal("Message must be encoded as a MsgPack map, not \"FixArray\".", ex.Message);
183183
Assert.Equal(typeof(UserScore), ex.TargetType);
184184
}
185185

Dasher/Methods.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal static class Methods
1818

1919
public static MethodInfo String_Equals_String_String { get; } = typeof(string).GetMethod(nameof(string.Equals), BindingFlags.Static | BindingFlags.Public, null, new[] {typeof(string), typeof(string)}, null);
2020
public static MethodInfo String_Equals_String_StringComparison { get; } = typeof(string).GetMethod(nameof(string.Equals), new[] {typeof(string), typeof(StringComparison)});
21+
public static MethodInfo String_Format_String_Object { get; } = typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object)});
2122
public static MethodInfo String_Format_String_Object_Object { get; } = typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object)});
2223
public static MethodInfo String_Format_String_Object_Object_Object { get; } = typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object), typeof(object)});
2324

Dasher/TypeProviders/ComplexTypeProvider.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ public bool TryEmitDeserialiseCode(ILGenerator ilg, ThrowBlockGatherer throwBloc
120120
ilg.Emit(OpCodes.Throw);
121121
};
122122

123+
Action loadPeekedFormatName = () =>
124+
{
125+
var format = ilg.DeclareLocal(typeof(Format));
126+
ilg.Emit(OpCodes.Ldloc, unpacker);
127+
ilg.Emit(OpCodes.Ldloca, format);
128+
ilg.Emit(OpCodes.Call, Methods.Unpacker_TryPeekFormat);
129+
// Drop the return value: if false, 'format' will be 'Unknown' which is fine.
130+
ilg.Emit(OpCodes.Pop);
131+
ilg.Emit(OpCodes.Ldloc, format);
132+
ilg.Emit(OpCodes.Box, typeof(Format));
133+
ilg.Emit(OpCodes.Call, Methods.Format_ToString);
134+
};
135+
123136
#region Initialise locals for constructor args
124137

125138
var valueLocals = new LocalBuilder[parameters.Length];
@@ -204,7 +217,9 @@ public bool TryEmitDeserialiseCode(ILGenerator ilg, ThrowBlockGatherer throwBloc
204217
ilg.Emit(OpCodes.Ldstr, "Data stream empty");
205218
throwException();
206219
ilg.MarkLabel(lblNotEmpty);
207-
ilg.Emit(OpCodes.Ldstr, "Message must be encoded as a MsgPack map");
220+
ilg.Emit(OpCodes.Ldstr, "Message must be encoded as a MsgPack map, not \"{0}\".");
221+
loadPeekedFormatName();
222+
ilg.Emit(OpCodes.Call, Methods.String_Format_String_Object);
208223
throwException();
209224
});
210225
}
@@ -325,18 +340,9 @@ public bool TryEmitDeserialiseCode(ILGenerator ilg, ThrowBlockGatherer throwBloc
325340
{
326341
throwBlocks.Throw(() =>
327342
{
328-
var format = ilg.DeclareLocal(typeof(Format));
329-
ilg.Emit(OpCodes.Ldloc, unpacker);
330-
ilg.Emit(OpCodes.Ldloca, format);
331-
ilg.Emit(OpCodes.Call, Methods.Unpacker_TryPeekFormat);
332-
// Drop the return value: if false, 'format' will be 'Unknown' which is fine.
333-
ilg.Emit(OpCodes.Pop);
334-
335343
ilg.Emit(OpCodes.Ldstr, "Encountered unexpected field \"{0}\" of MsgPack format \"{1}\" for CLR type \"{2}\".");
336344
ilg.Emit(OpCodes.Ldloc, key);
337-
ilg.Emit(OpCodes.Ldloc, format);
338-
ilg.Emit(OpCodes.Box, typeof(Format));
339-
ilg.Emit(OpCodes.Call, Methods.Format_ToString);
345+
loadPeekedFormatName();
340346
ilg.Emit(OpCodes.Ldstr, targetType.Name);
341347
ilg.Emit(OpCodes.Call, Methods.String_Format_String_Object_Object_Object);
342348
throwException();

0 commit comments

Comments
 (0)