Skip to content

Commit

Permalink
Represent Uri as string, not destructure it, closes #46
Browse files Browse the repository at this point in the history
  • Loading branch information
cd21h committed Jan 26, 2018
1 parent 656a2b4 commit 55af49d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ private object DestructureValue(object value, int level, IDictionary<object, IDi
return this.DestructureValueEnumerable(value, level, destructuredObjects, ref nextCyclicRefId);
}

if (typeof(Uri).GetTypeInfo().IsAssignableFrom(valueTypeInfo))
{
return this.DestructureUri((Uri)value, destructuredObjects, ref nextCyclicRefId);
}

return this.DestructureObject(value, valueType, level, destructuredObjects, ref nextCyclicRefId);
}

Expand All @@ -126,6 +131,22 @@ private object DestructureValueEnumerable(object value, int level, IDictionary<o
return resultList;
}

private object DestructureUri(Uri value, IDictionary<object, IDictionary<string, object>> destructuredObjects, ref int nextCyclicRefId)
{
if (destructuredObjects.ContainsKey(value))
{
var destructuredObject = destructuredObjects[value];
var refId = GetOrGenerateRefId(ref nextCyclicRefId, destructuredObject);

return new Dictionary<string, object>
{
{ RefLabel, refId }
};
}

return value.ToString();
}

private object DestructureValueDictionary(object value, int level, IDictionary<object, IDictionary<string, object>> destructuredObjects, ref int nextCyclicRefId)
{
if (destructuredObjects.ContainsKey(value))
Expand Down
66 changes: 66 additions & 0 deletions Tests/Serilog.Exceptions.Test/Destructurers/UriDestructurerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections;
using Serilog.Exceptions.Core;
using Serilog.Exceptions.Destructurers;
using Xunit;

namespace Serilog.Exceptions.Test.Destructurers
{
public class UriDestructurerTest
{
private ReflectionBasedDestructurer destructurer;

public UriDestructurerTest()
{
this.destructurer = new ReflectionBasedDestructurer();
}

[Fact]
public void CanDestructureUriProperty()
{
const string uriValue = "http://localhost/property";
var exception = new UriException("test", new Uri(uriValue));

var propertiesBag = new ExceptionPropertiesBag(exception);
this.destructurer.Destructure(exception, propertiesBag, null);

var properties = propertiesBag.GetResultDictionary();
var uriPropertyValue = properties[nameof(UriException.Uri)];
Assert.IsType<string>(uriPropertyValue);
Assert.Equal(uriValue, uriPropertyValue);
}

[Fact]
public void CanDestructureDataItem()
{
const string uriValue = "http://localhost/data-item";
var exception = new Exception("test")
{
Data =
{
{"UriDataItem", new Uri(uriValue)}
}
};

var propertiesBag = new ExceptionPropertiesBag(exception);
this.destructurer.Destructure(exception, propertiesBag, null);

var properties = propertiesBag.GetResultDictionary();
var data = (IDictionary) properties[nameof(Exception.Data)];
var uriDataValue = data["UriDataItem"];
Assert.IsType<string>(uriDataValue);
Assert.Equal(uriValue, uriDataValue);
}

public class UriException : Exception
{
public Uri Uri { get; }

public UriException(string message, Uri uri) : base(message)
{
Uri = uri;
}
}

}
}

0 comments on commit 55af49d

Please sign in to comment.