From 1030307b90b7dee84069307b76c17c15e41220ff Mon Sep 17 00:00:00 2001 From: Ahmed Ahmed Date: Thu, 27 Mar 2025 12:16:06 +0000 Subject: [PATCH] Fix serialization error when serializing exceptions with a TargetSite --- src/Bugsnag/SimpleJson.cs | 5 +++++ tests/Bugsnag.Tests/SerializerTests.cs | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Bugsnag/SimpleJson.cs b/src/Bugsnag/SimpleJson.cs index 9d58a6b3..335639e0 100644 --- a/src/Bugsnag/SimpleJson.cs +++ b/src/Bugsnag/SimpleJson.cs @@ -1548,6 +1548,11 @@ protected virtual bool TrySerializeKnownTypes(object input, out object output) output = ((Guid)input).ToString("D"); else if (input is Uri) output = input.ToString(); + else if (typeof(System.Reflection.MethodBase).IsAssignableFrom(input.GetType())) + { + // MethodBase (i.e. TargetSite in Exceptions) cannot be serialized, so ignore + output = null; + } else { Enum inputEnum = input as Enum; diff --git a/tests/Bugsnag.Tests/SerializerTests.cs b/tests/Bugsnag.Tests/SerializerTests.cs index 860f7f75..3e677889 100644 --- a/tests/Bugsnag.Tests/SerializerTests.cs +++ b/tests/Bugsnag.Tests/SerializerTests.cs @@ -58,6 +58,20 @@ public void NullValueTest() Assert.NotNull(json); } + [Fact] + public void CanSerializeException() + { + try + { + throw new System.Exception("Serialize me"); + } + catch (System.Exception exception) + { + var json = Serializer.SerializeObject(exception); + Assert.NotNull(json); + } + } + private class Circular { public string Name { get; set; }