AggregateException

This commit is contained in:
Ben Adams 2017-11-13 12:14:12 +00:00
parent d4bf1639c7
commit 5eb9c25574
2 changed files with 97 additions and 0 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) Ben A Adams. All rights reserved. // Copyright (c) Ben A Adams. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic.Enumerable;
using System.Reflection; using System.Reflection;
namespace System.Diagnostics namespace System.Diagnostics
@ -20,6 +21,14 @@ namespace System.Diagnostics
stackTraceString.SetValue(exception, stackTrace.ToString()); stackTraceString.SetValue(exception, stackTrace.ToString());
} }
if (exception is AggregateException aggEx)
{
foreach (var ex in EnumerableIList.Create(aggEx.InnerExceptions))
{
ex.Demystify();
}
}
exception.InnerException?.Demystify(); exception.InnerException?.Demystify();
} }
catch catch

View File

@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
namespace Demystify
{
public class AggregateException
{
[Fact]
public void DemystifiesAggregateExceptions()
{
Exception demystifiedException = null;
try
{
var tasks = new List<Task>
{
Task.Run(async () => await Throw1()),
Task.Run(async () => await Throw2()),
Task.Run(async () => await Throw3())
};
Task.WaitAll(tasks.ToArray());
}
catch (Exception ex)
{
demystifiedException = ex.Demystify();
}
// Assert
var stackTrace = demystifiedException.ToString();
stackTrace = ReplaceLineEndings.Replace(stackTrace, "");
var trace = string.Join("", stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
// Remove items that vary between test runners
.Where(s =>
s != " at Task Demystify.DynamicCompilation.DoesNotPreventStackTrace()+()=>{}" &&
!s.Contains("System.Threading.Tasks.Task.WaitAll")
)
.Skip(1)
.ToArray())
// Remove Full framework back arrow
.Replace("<---", "");
var expected = string.Join("", new[] {
" at async Task Demystify.AggregateException.Throw1()",
" at async void Demystify.AggregateException.DemystifiesAggregateExceptions()+(?)=>{}",
" --- End of inner exception stack trace ---",
" at void Demystify.AggregateException.DemystifiesAggregateExceptions()",
"---> (Inner Exception #0) System.ArgumentException: Value does not fall within the expected range.",
" at async Task Demystify.AggregateException.Throw1()",
" at async void Demystify.AggregateException.DemystifiesAggregateExceptions()+(?)=>{}",
"---> (Inner Exception #1) System.NullReferenceException: Object reference not set to an instance of an object.",
" at async Task Demystify.AggregateException.Throw2()",
" at async void Demystify.AggregateException.DemystifiesAggregateExceptions()+(?)=>{}",
"---> (Inner Exception #2) System.InvalidOperationException: Operation is not valid due to the current state of the object.",
" at async Task Demystify.AggregateException.Throw3()",
" at async void Demystify.AggregateException.DemystifiesAggregateExceptions()+(?)=>{}"});
Assert.Equal(expected, trace);
}
async Task Throw1()
{
await Task.Delay(1).ConfigureAwait(false);
throw new ArgumentException();
}
async Task Throw2()
{
await Task.Delay(1).ConfigureAwait(false);
throw new NullReferenceException();
}
async Task Throw3()
{
await Task.Delay(1).ConfigureAwait(false);
throw new InvalidOperationException();
}
private Regex ReplaceLineEndings = new Regex(" in [^\n\r]+");
}
}