2 // AggregateException.cs
5 // Marek Safar (marek.safar@gmail.com)
7 // Copyright (c) 2008 Jérémie "Garuma" Laval
8 // Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 using System.Collections.ObjectModel;
33 using System.Collections.Generic;
34 using System.Runtime.Serialization;
38 [System.SerializableAttribute]
39 [System.Diagnostics.DebuggerDisplay ("Count = {InnerExceptions.Count}")]
40 public class AggregateException : Exception
42 List<Exception> innerExceptions = new List<Exception> ();
43 const string defaultMessage = "One or more errors occured";
45 public AggregateException () : base (defaultMessage)
49 public AggregateException (string message): base (message)
53 public AggregateException (string message, Exception innerException): base (message, innerException)
55 if (innerException == null)
56 throw new ArgumentNullException ("innerException");
57 innerExceptions.Add (innerException);
60 protected AggregateException (SerializationInfo info, StreamingContext context)
61 : base (info, context)
65 public AggregateException (params Exception[] innerExceptions)
66 : this (string.Empty, innerExceptions)
70 public AggregateException (string message, params Exception[] innerExceptions)
71 : base (message, innerExceptions == null || innerExceptions.Length == 0 ? null : innerExceptions[0])
73 if (innerExceptions == null)
74 throw new ArgumentNullException ("innerExceptions");
75 foreach (var exception in innerExceptions)
76 if (exception == null)
77 throw new ArgumentException ("One of the inner exception is null", "innerExceptions");
79 this.innerExceptions.AddRange (innerExceptions);
82 public AggregateException (IEnumerable<Exception> innerExceptions)
83 : this (defaultMessage, innerExceptions)
87 public AggregateException (string message, IEnumerable<Exception> innerExceptions)
88 : this (message, new List<Exception> (innerExceptions).ToArray ())
92 public AggregateException Flatten ()
94 List<Exception> inner = new List<Exception> ();
96 foreach (Exception e in innerExceptions) {
97 AggregateException aggEx = e as AggregateException;
99 inner.AddRange (aggEx.Flatten ().InnerExceptions);
105 return new AggregateException (inner);
108 public void Handle (Func<Exception, bool> predicate)
110 if (predicate == null)
111 throw new ArgumentNullException ("predicate");
113 List<Exception> failed = new List<Exception> ();
114 foreach (var e in innerExceptions) {
119 if (failed.Count > 0)
120 throw new AggregateException (failed);
123 public ReadOnlyCollection<Exception> InnerExceptions {
125 return innerExceptions.AsReadOnly ();
129 internal void AddChildException (AggregateException childEx)
131 if (innerExceptions == null)
132 innerExceptions = new List<Exception> ();
136 innerExceptions.Add (childEx);
139 public override string ToString ()
141 System.Text.StringBuilder finalMessage = new System.Text.StringBuilder (base.ToString ());
143 int currentIndex = -1;
144 foreach (Exception e in innerExceptions) {
145 finalMessage.Append (Environment.NewLine);
146 finalMessage.Append (" --> (Inner exception ");
147 finalMessage.Append (++currentIndex);
148 finalMessage.Append (") ");
149 finalMessage.Append (e.ToString ());
150 finalMessage.Append (Environment.NewLine);
152 return finalMessage.ToString ();
155 public override void GetObjectData (SerializationInfo info, StreamingContext context)
158 throw new ArgumentNullException("info");
160 base.GetObjectData(info, context);
161 info.AddValue ("InnerExceptions", innerExceptions.ToArray(), typeof (Exception[]));
164 public override Exception GetBaseException ()
166 Exception inner = this;
167 for (var ae = this; ae.innerExceptions.Count == 1;) {
168 inner = ae.InnerExceptions [0];
170 var aei = inner as AggregateException;