Skip to content

Commit

Permalink
Added a unit test for the ToSerializable method.
Browse files Browse the repository at this point in the history
  • Loading branch information
yallie committed Dec 10, 2024
1 parent 47dd9d8 commit 5d057e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
20 changes: 20 additions & 0 deletions CoreRemoting.Tests/ExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ public void Exception_can_be_checked_if_it_is_serializable()
Assert.False(new Exception("Goodbye", new NonSerializableException()).IsSerializable());
}

[Fact]
public void Exception_can_be_turned_to_serializable()
{
var slotName = "SomeData";
var ex = new Exception("Bang!", new NonSerializableException("Zoom!"));
ex.Data[slotName] = DateTime.Now.ToString();
ex.InnerException.Data[slotName] = DateTime.Today.Ticks;
Assert.False(ex.IsSerializable());

var sx = ex.ToSerializable();
Assert.True(sx.IsSerializable());
Assert.NotSame(ex, sx);
Assert.NotSame(ex.InnerException, sx.InnerException);

Assert.Equal(ex.Message, sx.Message);
Assert.Equal(ex.Data[slotName], sx.Data[slotName]);
Assert.Equal(ex.InnerException.Message, sx.InnerException.Message);
Assert.Equal(ex.InnerException.Data[slotName], sx.InnerException.Data[slotName]);
}

[Fact]
public void SkipTargetInvocationException_returns_the_first_meaningful_inner_exception()
{
Expand Down
8 changes: 3 additions & 5 deletions CoreRemoting/Serialization/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Castle.MicroKernel.ComponentActivator;
using System;
using System;
using System.Linq;
using System.Reflection;
using Castle.MicroKernel.ComponentActivator;

namespace CoreRemoting.Serialization;

Expand All @@ -22,7 +22,7 @@ public static class ExceptionExtensions
agg.InnerException.IsSerializable() &&
agg.GetType().IsSerializable,

// pesky exception that looks like serializable but really isn't
// this exception is not deserializable
ComponentActivatorException cax => false,

_ => ex.GetType().IsSerializable &&
Expand Down Expand Up @@ -64,9 +64,7 @@ public static TException CopyDataFrom<TException>(this TException ex, Exception
public static Exception SkipTargetInvocationExceptions(this Exception ex)
{
while (ex is TargetInvocationException && ex.InnerException != null)
{
ex = ex.InnerException;
}

return ex;
}
Expand Down

0 comments on commit 5d057e4

Please sign in to comment.