Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / corlib / System / AggregateException.cs
1 // AggregateException.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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
21 // THE SOFTWARE.
22 //
23 //
24
25 #if NET_4_0 || MOBILE
26 using System;
27 using System.Collections.ObjectModel;
28 using System.Collections.Generic;
29 using System.Runtime.Serialization;
30
31 namespace System
32 {
33
34         [System.SerializableAttribute]
35         [System.Diagnostics.DebuggerDisplay ("Count = {InnerExceptions.Count}")]
36         public class AggregateException : Exception
37         {
38                 List<Exception> innerExceptions = new List<Exception> ();
39                 const string defaultMessage = "One or more errors occured";
40                 
41                 public AggregateException () : base (defaultMessage)
42                 {
43                 }
44                 
45                 public AggregateException (string message): base (message)
46                 {
47                 }
48                 
49                 public AggregateException (string message, Exception innerException): base (message, innerException)
50                 {
51                         if (innerException == null)
52                                 throw new ArgumentNullException ("innerException");
53                         innerExceptions.Add (innerException);
54                 }
55                 
56                 protected AggregateException (SerializationInfo info, StreamingContext context)
57                         : base (info, context)
58                 {
59                 }
60                 
61                 public AggregateException (params Exception[] innerExceptions)
62                         : this (string.Empty, innerExceptions)
63                 {
64                 }
65                 
66                 public AggregateException (string message, params Exception[] innerExceptions)
67                         : base (message, innerExceptions == null || innerExceptions.Length == 0 ? null : innerExceptions[0])
68                 {
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");
74
75                         this.innerExceptions.AddRange (innerExceptions);
76                 }
77                 
78                 public AggregateException (IEnumerable<Exception> innerExceptions)
79                         : this (defaultMessage, innerExceptions)
80                 {
81                 }
82                 
83                 public AggregateException (string message, IEnumerable<Exception> innerExceptions)
84                         : this (message, new List<Exception> (innerExceptions).ToArray ())
85                 {
86                 }
87                 
88                 public AggregateException Flatten ()
89                 {
90                         List<Exception> inner = new List<Exception> ();
91                         
92                         foreach (Exception e in innerExceptions) {
93                                 AggregateException aggEx = e as AggregateException;
94                                 if (aggEx != null) {
95                                         inner.AddRange (aggEx.Flatten ().InnerExceptions);
96                                 } else {
97                                         inner.Add (e);
98                                 }                               
99                         }
100
101                         return new AggregateException (inner);
102                 }
103                 
104                 public void Handle (Func<Exception, bool> predicate)
105                 {
106                         List<Exception> failed = new List<Exception> ();
107                         foreach (var e in innerExceptions) {
108                                 try {
109                                         if (!predicate (e))
110                                                 failed.Add (e);
111                                 } catch {
112                                         throw new AggregateException (failed);
113                                 }
114                         }
115                         if (failed.Count > 0)
116                                 throw new AggregateException (failed);
117                 }
118                 
119                 public ReadOnlyCollection<Exception> InnerExceptions {
120                         get {
121                                 return innerExceptions.AsReadOnly ();
122                         }
123                 }
124
125                 internal void AddChildException (AggregateException childEx)
126                 {
127                         if (innerExceptions == null)
128                                 innerExceptions = new List<Exception> ();
129                         if (childEx == null)
130                                 return;
131
132                         innerExceptions.Add (childEx);
133                 }
134                 
135                 public override string ToString ()
136                 {
137                         System.Text.StringBuilder finalMessage = new System.Text.StringBuilder (base.ToString ());
138
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);
147                         }
148                         return finalMessage.ToString ();
149                 }
150
151                 public override void GetObjectData (SerializationInfo info,     StreamingContext context)
152                 {
153                         if (info == null) {
154                                 throw new ArgumentNullException("info");
155                         }
156                         base.GetObjectData(info, context);
157                         info.AddValue ("InnerExceptions", innerExceptions.ToArray(), typeof (Exception[]));
158                 }
159
160                 public override Exception GetBaseException ()
161                 {
162                         if (innerExceptions == null || innerExceptions.Count == 0)
163                                 return this;
164                         return innerExceptions[0].GetBaseException ();
165                 }
166         }
167 }
168 #endif