Skip to content

Commit

Permalink
Add RegexMatchTimeoutExceptionDestructurer for RegexMatchTimeoutExcep…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Driedas authored and RehanSaeed committed Feb 19, 2022
1 parent c9f1374 commit 578cb79
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class DestructuringOptionsBuilder : IDestructuringOptions
new ArgumentExceptionDestructurer(),
new ArgumentOutOfRangeExceptionDestructurer(),
new AggregateExceptionDestructurer(),
new RegexMatchTimeoutExceptionDestructurer(),
new ReflectionTypeLoadExceptionDestructurer(),
new OperationCanceledExceptionDestructurer(),
new TaskCanceledExceptionDestructurer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Serilog.Exceptions.Destructurers
{
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Serilog.Exceptions.Core;

/// <summary>
/// Destructurer for <see cref="RegexMatchTimeoutException"/>.
/// </summary>
public class RegexMatchTimeoutExceptionDestructurer
: ExceptionDestructurer
{
/// <inheritdoc cref="IExceptionDestructurer.TargetTypes"/>
public override Type[] TargetTypes => new Type[]
{
typeof(RegexMatchTimeoutException),
};

/// <inheritdoc cref="IExceptionDestructurer.Destructure"/>
public override void Destructure(Exception exception, IExceptionPropertiesBag propertiesBag, Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);

#pragma warning disable CA1062 // Validate arguments of public methods
var typedException = (RegexMatchTimeoutException)exception;
propertiesBag.AddProperty(nameof(RegexMatchTimeoutException.Input), typedException.Input);
propertiesBag.AddProperty(nameof(RegexMatchTimeoutException.Pattern), typedException.Pattern);
propertiesBag.AddProperty(nameof(RegexMatchTimeoutException.MatchTimeout), typedException.MatchTimeout.ToString("c"));
#pragma warning restore CA1062 // Validate arguments of public methods
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Serilog.Exceptions.Test.Destructurers
{
using System;
using System.Text.RegularExpressions;
using Serilog.Exceptions.Core;
using Serilog.Exceptions.Destructurers;
using Xunit;
using static LogJsonOutputUtils;

public class RegexMatchTimeoutExceptionDestructurerTests
{
[Fact]
public void RegexMatchTimeoutException_ParamsAttachedAsProperties()
{
var exception = new RegexMatchTimeoutException("input", "pattern", TimeSpan.FromSeconds(1));

var optionsBuilder = new DestructuringOptionsBuilder()
.WithDestructurers(new IExceptionDestructurer[]
{
new RegexMatchTimeoutExceptionDestructurer(),
});

var loggedExceptionDetails = ExtractExceptionDetails(LogAndDestructureException(exception, optionsBuilder));

Assert_ContainsPropertyWithValue(loggedExceptionDetails, nameof(RegexMatchTimeoutException.Input), exception.Input);
Assert_ContainsPropertyWithValue(loggedExceptionDetails, nameof(RegexMatchTimeoutException.Pattern), exception.Pattern);
Assert_ContainsPropertyWithValue(loggedExceptionDetails, nameof(RegexMatchTimeoutException.MatchTimeout), exception.MatchTimeout.ToString("c"));
}
}
}

0 comments on commit 578cb79

Please sign in to comment.