Skip to content

Commit

Permalink
Include target type name in exception message for unexpected field.
Browse files Browse the repository at this point in the history
  • Loading branch information
drewnoakes committed Jun 26, 2016
1 parent 0cdf31e commit 2aea3ca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions 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\".", ex.Message);
Assert.Equal("Encountered unexpected field \"SUPRISE\" for type \"UserScore\".", ex.Message);
}

[Fact]
Expand Down Expand Up @@ -181,7 +181,7 @@ public void ThrowsOnDuplicateField()
() => deserialiser.Deserialise(bytes));

Assert.Equal(typeof(UserScore), ex.TargetType);
Assert.Equal("Encountered duplicate field \"Score\".", ex.Message);
Assert.Equal("Encountered duplicate field \"Score\" for type \"UserScore\".", ex.Message);
}

[Fact]
Expand Down
10 changes: 6 additions & 4 deletions Dasher/DeserialiserEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,10 @@ public static Func<Unpacker, DasherContext, object> Build(Type type, UnexpectedF
var notSeenLabel = ilg.DefineLabel();
ilg.Emit(OpCodes.Brfalse, notSeenLabel);
{
ilg.Emit(OpCodes.Ldstr, "Encountered duplicate field \"{0}\".");
ilg.Emit(OpCodes.Ldstr, "Encountered duplicate field \"{0}\" for type \"{1}\".");
ilg.Emit(OpCodes.Ldloc, key);
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object)}));
ilg.Emit(OpCodes.Ldstr, type.Name);
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object)}));
throwException();
}

Expand All @@ -267,9 +268,10 @@ 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}\".");
ilg.Emit(OpCodes.Ldstr, "Encountered unexpected field \"{0}\" for type \"{1}\".");
ilg.Emit(OpCodes.Ldloc, key);
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object)}));
ilg.Emit(OpCodes.Ldstr, type.Name);
ilg.Emit(OpCodes.Call, typeof(string).GetMethod(nameof(string.Format), new[] {typeof(string), typeof(object), typeof(object)}));
throwException();
}
else
Expand Down

0 comments on commit 2aea3ca

Please sign in to comment.