Skip to content

Commit

Permalink
Simplify JSON serialization of exceptions
Browse files Browse the repository at this point in the history
+semver: patch
  • Loading branch information
natenho committed Mar 11, 2024
1 parent 61d295c commit 29ce70d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
49 changes: 49 additions & 0 deletions src/Mockaco.AspNetCore/Common/SimpleExceptionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Mockaco.Common
{
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

internal class SimpleExceptionConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(Exception).IsAssignableFrom(objectType);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Deserializing exceptions is not supported.");
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var exception = value as Exception;
if (exception == null)
{
serializer.Serialize(writer, null);
return;
}

var obj = new JObject
{
["Type"] = exception.GetType().Name,
["Message"] = exception.Message,

};

if (exception.Data.Count > 0)
{
obj["Data"] = JToken.FromObject(exception.Data, serializer);
}

if (exception.InnerException != null)
{
obj["InnerException"] = JToken.FromObject(exception.InnerException, serializer);
}

obj.WriteTo(writer);
}
}

}
12 changes: 8 additions & 4 deletions src/Mockaco.AspNetCore/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Mockaco.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Linq;
Expand All @@ -7,10 +8,13 @@ namespace System
{
internal static class ObjectExtensions
{
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
private static readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
Converters = new JsonConverter[] { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy()} },
Converters = [
new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy()},
new SimpleExceptionConverter()
],
NullValueHandling = NullValueHandling.Ignore
};

Expand All @@ -24,7 +28,7 @@ public static string ToJson<T>(this T param)

try
{
return JsonConvert.SerializeObject(param, JsonSerializerSettings);
return JsonConvert.SerializeObject(param, _jsonSerializerSettings);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private async Task LogHttpContext(HttpContext httpContext)

if (string.IsNullOrEmpty(body))
{
_logger.LogDebug("Body is not present", body);
_logger.LogDebug("Body is not present");
}
else
{
Expand Down

0 comments on commit 29ce70d

Please sign in to comment.