Fix double expansion of AggregateException.Flatten message (#605)
authorPent Ploompuu <kaalikas@gmail.com>
Wed, 29 Jan 2020 19:37:51 +0000 (21:37 +0200)
committerGitHub <noreply@github.com>
Wed, 29 Jan 2020 19:37:51 +0000 (14:37 -0500)
* Fix double expansion of AggregateException.Flatten message

* Update tests

src/libraries/System.Private.CoreLib/src/System/AggregateException.cs
src/libraries/System.Threading.Tasks/tests/AggregateExceptionTests.cs

index 46baebfddd6e7bbe80f4372a0f867a5a2e332e89..204a4f06ca9aad8a22f1aa7b95e8959ad209cd6a 100644 (file)
@@ -377,7 +377,7 @@ namespace System
             while (exceptionsToFlatten.Count > nDequeueIndex)
             {
                 // dequeue one from exceptionsToFlatten
-                IList<Exception> currentInnerExceptions = exceptionsToFlatten[nDequeueIndex++].InnerExceptions;
+                ReadOnlyCollection<Exception> currentInnerExceptions = exceptionsToFlatten[nDequeueIndex++].InnerExceptions;
 
                 for (int i = 0; i < currentInnerExceptions.Count; i++)
                 {
@@ -401,8 +401,7 @@ namespace System
                 }
             }
 
-
-            return new AggregateException(Message, flattenedExceptions);
+            return new AggregateException(GetType() == typeof(AggregateException) ? base.Message : Message, flattenedExceptions);
         }
 
         /// <summary>Gets a message that describes the exception.</summary>
index fdf4a5dac29a36e233029feb5d2fc23100327ca1..5692231026432ee56e15103e2facec696691d99a 100644 (file)
@@ -2,10 +2,8 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using Xunit;
-using System;
 using System.Collections.Generic;
-using System.Diagnostics;
+using Xunit;
 
 namespace System.Threading.Tasks.Tests
 {
@@ -103,7 +101,9 @@ namespace System.Threading.Tasks.Tests
             Exception exceptionB = new Exception("B");
             Exception exceptionC = new Exception("C");
 
-            AggregateException aggExceptionBase = new AggregateException(exceptionA, exceptionB, exceptionC);
+            AggregateException aggExceptionBase = new AggregateException("message", exceptionA, exceptionB, exceptionC);
+
+            Assert.Equal("message (A) (B) (C)", aggExceptionBase.Message);
 
             // Verify flattening one with another.
             // > Flattening (no recursion)...
@@ -114,15 +114,17 @@ namespace System.Threading.Tasks.Tests
             };
 
             Assert.Equal(expected1, flattened1.InnerExceptions);
+            Assert.Equal("message (A) (B) (C)", flattened1.Message);
 
             // Verify flattening one with another, accounting for recursion.
-            AggregateException aggExceptionRecurse = new AggregateException(aggExceptionBase, aggExceptionBase);
+            AggregateException aggExceptionRecurse = new AggregateException("message", aggExceptionBase, aggExceptionBase);
             AggregateException flattened2 = aggExceptionRecurse.Flatten();
             Exception[] expected2 = new Exception[] {
                 exceptionA, exceptionB, exceptionC, exceptionA, exceptionB, exceptionC,
             };
 
             Assert.Equal(expected2, flattened2.InnerExceptions);
+            Assert.Equal("message (A) (B) (C) (A) (B) (C)", flattened2.Message);
         }
     }
 }