Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RegexMatchTimeoutExceptionDestructurer for RegexMatchTimeoutException #460

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ public class DestructuringOptionsBuilder : IDestructuringOptions
new ArgumentExceptionDestructurer(),
new ArgumentOutOfRangeExceptionDestructurer(),
new AggregateExceptionDestructurer(),
new RegexMatchTimeoutExceptionDestructurer(),
new ReflectionTypeLoadExceptionDestructurer(),
new OperationCanceledExceptionDestructurer(),
new TaskCanceledExceptionDestructurer(),
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"));
}
}
}