1 // AggregateException.cs
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 using System.Collections.ObjectModel;
28 using System.Collections.Generic;
29 using System.Runtime.Serialization;
34 [System.SerializableAttribute]
35 [System.Diagnostics.DebuggerDisplay ("Count = {InnerExceptions.Count}")]
36 public class AggregateException : Exception
38 List<Exception> innerExceptions = new List<Exception> ();
39 const string defaultMessage = "One or more errors occured";
41 public AggregateException () : base (defaultMessage)
45 public AggregateException (string message): base (message)
49 public AggregateException (string message, Exception innerException): base (message, innerException)
51 if (innerException == null)
52 throw new ArgumentNullException ("innerException");
53 innerExceptions.Add (innerException);
56 protected AggregateException (SerializationInfo info, StreamingContext context)
57 : base (info, context)
61 public AggregateException (params Exception[] innerExceptions)
62 : this (string.Empty, innerExceptions)
66 public AggregateException (string message, params Exception[] innerExceptions)
67 : base (message, innerExceptions == null || innerExceptions.Length == 0 ? null : innerExceptions[0])
69 if (innerExceptions == null)
70 throw new ArgumentNullException ("innerExceptions");
71 foreach (var exception in innerExceptions)
72 if (exception == null)
73 throw new ArgumentException ("One of the inner exception is null", "innerExceptions");
75 this.innerExceptions.AddRange (innerExceptions);
78 public AggregateException (IEnumerable<Exception> innerExceptions)
79 : this (defaultMessage, innerExceptions)
83 public AggregateException (string message, IEnumerable<Exception> innerExceptions)
84 : this (message, new List<Exception> (innerExceptions).ToArray ())
88 public AggregateException Flatten ()
90 List<Exception> inner = new List<Exception> ();
92 foreach (Exception e in innerExceptions) {
93 AggregateException aggEx = e as AggregateException;
95 inner.AddRange (aggEx.Flatten ().InnerExceptions);
101 return new AggregateException (inner);
104 public void Handle (Func<Exception, bool> predicate)
106 List<Exception> failed = new List<Exception> ();
107 foreach (var e in innerExceptions) {
112 throw new AggregateException (failed);
115 if (failed.Count > 0)
116 throw new AggregateException (failed);
119 public ReadOnlyCollection<Exception> InnerExceptions {
121 return innerExceptions.AsReadOnly ();
125 internal void AddChildException (AggregateException childEx)
127 if (innerExceptions == null)
128 innerExceptions = new List<Exception> ();
132 innerExceptions.Add (childEx);
135 public override string ToString ()
137 System.Text.StringBuilder finalMessage = new System.Text.StringBuilder (base.ToString ());
139 int currentIndex = -1;
140 foreach (Exception e in innerExceptions) {
141 finalMessage.Append (Environment.NewLine);
142 finalMessage.Append (" --> (Inner exception ");
143 finalMessage.Append (++currentIndex);
144 finalMessage.Append (") ");
145 finalMessage.Append (e.ToString ());
146 finalMessage.Append (Environment.NewLine);
148 return finalMessage.ToString ();
151 public override void GetObjectData (SerializationInfo info, StreamingContext context)
154 throw new ArgumentNullException("info");
156 base.GetObjectData(info, context);
157 info.AddValue ("InnerExceptions", innerExceptions.ToArray(), typeof (Exception[]));
160 public override Exception GetBaseException ()
162 if (innerExceptions == null || innerExceptions.Count == 0)
164 return innerExceptions[0].GetBaseException ();