Microsoft.CodeAnalysis.Sarif.Stack.CreateStacks C# (CSharp) Method

CreateStacks() public static method

Create one or more Stack instances from a .NET exception. Captures inner exceptions and handles aggregated exceptions.
public static CreateStacks ( Exception exception ) : ISet
exception System.Exception
return ISet
        public static ISet<Stack> CreateStacks(Exception exception)
        {
            HashSet<Stack> stacks;
            Queue<Exception> exceptions;

            stacks = new HashSet<Stack>();
            exceptions = new Queue<Exception>(new Exception[] { exception });

            while (exceptions.Count > 0)
            {
                Stack stack;
                Exception current;

                current = exceptions.Dequeue();

                var aggregated = current as AggregateException;
                if (aggregated != null)
                {
                    foreach (Exception e in aggregated.InnerExceptions)
                    {
                        exceptions.Enqueue(e);
                    }
                }
                else
                {
                    // AggregatedExceptions surface the first exception
                    // in the aggregation as InnerException, so we don't
                    // reexamine this property for that exception type (as
                    // it is already enqueued from inspecting InnerExceptions).
                    if (current.InnerException != null)
                    {
                        exceptions.Enqueue(current.InnerException);
                    }
                }

                stack = Create(current.StackTrace);

                stack.Message = current.FormatMessage();

                stacks.Add(stack);
            }

            return stacks;
        }

Usage Example

Esempio n. 1
0
        public void Stack_CreateFromAggregatedExceptionWithInnerException()
        {
            bool caughtException = false;

            try
            {
                File.Create(Path.GetInvalidFileNameChars()[0].ToString(), 0);
            }
            catch (ArgumentException exception)
            {
                var innerException1 = new InvalidOperationException("Test exception 1.");
                var innerException2 = new InvalidOperationException("Test exception 2.", exception);

                var aggregated = new AggregateException(innerException1, innerException2);

                IList <Stack> stacks = Stack.CreateStacks(aggregated).ToList();

                stacks.Count.Should().Be(4);
                aggregated.StackTrace.Should().Be(null);

                Assert.Equal("[No frames]", stacks[0].ToString());
                Assert.Equal("[No frames]", stacks[1].ToString());
                Assert.Equal("[No frames]", stacks[2].ToString());
                stacks[3].ToString().Should().BeCrossPlatformEquivalentStrings(exception.StackTrace);

                Assert.Equal(aggregated.FormatMessage(), stacks[0].Message.Text);
                Assert.Equal(innerException1.FormatMessage(), stacks[1].Message.Text);
                Assert.Equal(innerException2.FormatMessage(), stacks[2].Message.Text);
                Assert.Equal(exception.FormatMessage(), stacks[3].Message.Text);

                caughtException = true;
            }
            Assert.True(caughtException);
        }
All Usage Examples Of Microsoft.CodeAnalysis.Sarif.Stack::CreateStacks