Skip to content

Commit

Permalink
Extend information in exception raised by ComplexTypeProvider.
Browse files Browse the repository at this point in the history
  • Loading branch information
drewnoakes committed Sep 29, 2016
1 parent 4eb5035 commit 307c389
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Dasher.Tests/DeserialiserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void ThrowsOnNonMapData()

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

Expand Down
1 change: 1 addition & 0 deletions Dasher/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal static class Methods

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);
public static MethodInfo String_Equals_String_StringComparison { get; } = typeof(string).GetMethod(nameof(string.Equals), new[] {typeof(string), typeof(StringComparison)});
public static MethodInfo String_Format_String_Object { get; } = typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object)});
public static MethodInfo String_Format_String_Object_Object { get; } = typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object)});
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)});

Expand Down
28 changes: 17 additions & 11 deletions Dasher/TypeProviders/ComplexTypeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ public bool TryEmitDeserialiseCode(ILGenerator ilg, ThrowBlockGatherer throwBloc
ilg.Emit(OpCodes.Throw);
};

Action loadPeekedFormatName = () =>
{
var format = ilg.DeclareLocal(typeof(Format));
ilg.Emit(OpCodes.Ldloc, unpacker);
ilg.Emit(OpCodes.Ldloca, format);
ilg.Emit(OpCodes.Call, Methods.Unpacker_TryPeekFormat);
// Drop the return value: if false, 'format' will be 'Unknown' which is fine.
ilg.Emit(OpCodes.Pop);
ilg.Emit(OpCodes.Ldloc, format);
ilg.Emit(OpCodes.Box, typeof(Format));
ilg.Emit(OpCodes.Call, Methods.Format_ToString);
};

#region Initialise locals for constructor args

var valueLocals = new LocalBuilder[parameters.Length];
Expand Down Expand Up @@ -204,7 +217,9 @@ public bool TryEmitDeserialiseCode(ILGenerator ilg, ThrowBlockGatherer throwBloc
ilg.Emit(OpCodes.Ldstr, "Data stream empty");
throwException();
ilg.MarkLabel(lblNotEmpty);
ilg.Emit(OpCodes.Ldstr, "Message must be encoded as a MsgPack map");
ilg.Emit(OpCodes.Ldstr, "Message must be encoded as a MsgPack map, not \"{0}\".");
loadPeekedFormatName();
ilg.Emit(OpCodes.Call, Methods.String_Format_String_Object);
throwException();
});
}
Expand Down Expand Up @@ -325,18 +340,9 @@ public bool TryEmitDeserialiseCode(ILGenerator ilg, ThrowBlockGatherer throwBloc
{
throwBlocks.Throw(() =>
{
var format = ilg.DeclareLocal(typeof(Format));
ilg.Emit(OpCodes.Ldloc, unpacker);
ilg.Emit(OpCodes.Ldloca, format);
ilg.Emit(OpCodes.Call, Methods.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, Methods.Format_ToString);
loadPeekedFormatName();
ilg.Emit(OpCodes.Ldstr, targetType.Name);
ilg.Emit(OpCodes.Call, Methods.String_Format_String_Object_Object_Object);
throwException();
Expand Down

0 comments on commit 307c389

Please sign in to comment.