Esempio n. 1
0
        // To be discussed: whether we need to keep consistent behaviour even for inner exceptions
        //[Fact]
        //public void WhenDestruringAggregateException_ResultShouldBeEquivalentToAggregateExceptionDestructurer()
        //{
        //    var argumentException = ThrowAndCatchException(() => throw new ArgumentException("MESSAGE", "paramName"));
        //    var aggregateException = ThrowAndCatchException(() => throw new AggregateException(argumentException));
        //    Test_ResultOfReflectionDestructurerShouldBeEquivalentToCustomOne(aggregateException, new AggregateExceptionDestructurer());
        //}

        private static void Test_ResultOfReflectionDestructurerShouldBeEquivalentToCustomOne(
            Exception exception,
            IExceptionDestructurer customDestructurer)
        {
            // Arrange
            var reflectionBasedResult       = new ExceptionPropertiesBag(exception);
            var customBasedResult           = new ExceptionPropertiesBag(exception);
            var reflectionBasedDestructurer = CreateReflectionBasedDestructurer();

            // Act
            Func <Exception, IReadOnlyDictionary <string, object> > InnerDestructure(IExceptionDestructurer destructurer)
            {
                return((ex) =>
                {
                    var resultsBag = new ExceptionPropertiesBag(ex);

                    destructurer.Destructure(ex, resultsBag, null);

                    return resultsBag.GetResultDictionary();
                });
            }

            reflectionBasedDestructurer.Destructure(exception, reflectionBasedResult, InnerDestructure(reflectionBasedDestructurer));
            customDestructurer.Destructure(exception, customBasedResult, InnerDestructure(new ArgumentExceptionDestructurer()));

            // Assert
            var reflectionBasedDictionary = (Dictionary <string, object>)reflectionBasedResult.GetResultDictionary();
            var customBasedDictionary     = (Dictionary <string, object>)customBasedResult.GetResultDictionary();

            reflectionBasedDictionary.Should().BeEquivalentTo(customBasedDictionary);
        }
        private static Func <Exception, IReadOnlyDictionary <string, object> > InnerDestructurer(
            IExceptionDestructurer destructurer) =>
        (ex) =>
        {
            var resultsBag = new ExceptionPropertiesBag(ex);

            destructurer.Destructure(ex, resultsBag, InnerDestructurer(destructurer));

            return(resultsBag.GetResultDictionary());
        };
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExceptionEnricher"/> class.
        /// </summary>
        /// <param name="destructuringOptions">The destructuring options, cannot be null.</param>
        public ExceptionEnricher(IDestructuringOptions destructuringOptions)
        {
            this.destructuringOptions        = destructuringOptions ?? throw new ArgumentNullException(nameof(destructuringOptions));
            this.reflectionBasedDestructurer = new ReflectionBasedDestructurer(destructuringOptions.DestructuringDepth);

            this.destructurers = new Dictionary <Type, IExceptionDestructurer>();
            foreach (var destructurer in this.destructuringOptions.Destructurers)
            {
                foreach (var targetType in destructurer.TargetTypes)
                {
                    this.destructurers.Add(targetType, destructurer);
                }
            }
        }
        private static void Test_ResultOfReflectionDestructurerShouldBeEquivalentToCustomOne(
            Exception exception,
            IExceptionDestructurer customDestructurer)
        {
            // Arrange
            var reflectionBasedResult       = new ExceptionPropertiesBag(exception);
            var customBasedResult           = new ExceptionPropertiesBag(exception);
            var reflectionBasedDestructurer = CreateReflectionBasedDestructurer();

            // Act
            reflectionBasedDestructurer.Destructure(exception, reflectionBasedResult, InnerDestructurer(reflectionBasedDestructurer));
            customDestructurer.Destructure(exception, customBasedResult, InnerDestructurer(new ArgumentExceptionDestructurer()));

            // Assert
            var reflectionBasedDictionary = (Dictionary <string, object>)reflectionBasedResult.GetResultDictionary();
            var customBasedDictionary     = (Dictionary <string, object>)customBasedResult.GetResultDictionary();

            reflectionBasedDictionary.Should().BeEquivalentTo(customBasedDictionary);
        }
Esempio n. 5
0
        /// <inheritdoc />
        public ExceptionDestructuringProcessor()
        {
            _destructuringOptions = FinalDestructuringOptions.Current;
            if (_destructuringOptions == null)
            {
                throw new ArgumentException("Final destructuring options cannot be null.",
                                            nameof(FinalDestructuringOptions.Current));
            }

            _reflectionBasedDestructurer = new ReflectionBasedDestructurer(_destructuringOptions.DestructureDepth);
            _destructurers = new Dictionary <Type, IExceptionDestructurer>();
            foreach (var destructurer in _destructuringOptions.Destructurers)
            {
                foreach (var targetType in destructurer.TargetTypes)
                {
                    _destructurers.Add(targetType, destructurer);
                }
            }
        }
 public ExceptionEnricher(List <string> ignoredProperties)
     : this(GetDefaultDestructurers(ignoredProperties))
 {
     reflectionBasedDestructurer = new ReflectionBasedDestructurer(ignoredProperties);
 }