Normalize line endings.
[mono.git] / mcs / class / corlib / System / AggregateException.cs
1 #if NET_4_0 || BOOTSTRAP_NET_4_0
2 // AggregateException.cs
3 //
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 //
24 //
25
26 using System;
27 using System.Collections.ObjectModel;
28 using System.Collections.Generic;
29 using System.Runtime.Serialization;
30
31 namespace System
32 {
33         [System.SerializableAttribute]
34         public class AggregateException : Exception
35         {
36                 List<Exception> innerExceptions;
37                 
38                 public AggregateException (): base()
39                 {
40                 }
41                 
42                 public AggregateException (string message): base (message, null)
43                 {
44                 }
45                 
46                 public AggregateException (string message, Exception e): base (message, e)
47                 {
48                 }
49                 
50                 protected AggregateException (SerializationInfo info, StreamingContext ctx)
51                         : base (info, ctx)
52                 {
53                 }
54                 
55                 public AggregateException (params Exception[] innerExceptions)
56                         : this (string.Empty, innerExceptions)
57                 {
58                 }
59                 
60                 public AggregateException (string message, params Exception[] innerExceptions)
61                         : this (message, (IEnumerable<Exception>)innerExceptions)
62                 {
63                 }
64                 
65                 public AggregateException (IEnumerable<Exception> innerExceptions)
66                         : this (string.Empty, innerExceptions)
67                 {
68                 }
69                 
70                 public AggregateException (string message, IEnumerable<Exception> inner)
71                         : base(GetFormattedMessage(message, inner))
72                 {
73                         this.innerExceptions = new List<Exception> (inner);
74                 }
75                 
76                 public AggregateException Flatten ()
77                 {
78                         List<Exception> inner = new List<Exception> ();
79                         
80                         foreach (Exception e in innerExceptions) {
81                                 AggregateException aggEx = e as AggregateException;
82                                 if (aggEx != null) {
83                                         inner.AddRange (aggEx.Flatten ().InnerExceptions);
84                                 } else {
85                                         inner.Add (e);
86                                 }                               
87                         }
88
89                         return new AggregateException (inner);
90                 }
91                 
92                 public void Handle (Func<Exception, bool> handler)
93                 {
94                         List<Exception> failed = new List<Exception> ();
95                         foreach (var e in innerExceptions) {
96                                 try {
97                                         if (!handler (e))
98                                                 failed.Add (e);
99                                 } catch {
100                                         throw new AggregateException (failed);
101                                 }
102                         }
103                         if (failed.Count > 0)
104                                 throw new AggregateException (failed);
105                 }
106                 
107                 public ReadOnlyCollection<Exception> InnerExceptions {
108                         get {
109                                 return innerExceptions.AsReadOnly ();
110                         }
111                 }
112                 
113                 public override string ToString ()
114                 {
115                         return this.Message;
116                 }
117                 
118                 const string baseMessage = "Exception(s) occurred : {0}.";
119                 static string GetFormattedMessage (string customMessage, IEnumerable<Exception> inner)
120                 {
121                         System.Text.StringBuilder finalMessage
122                                 = new System.Text.StringBuilder (string.Format (baseMessage, customMessage));
123                         foreach (Exception e in inner) {
124                                 finalMessage.Append (Environment.NewLine);
125                                 finalMessage.Append ("[ ");
126                                 finalMessage.Append (e.ToString ());
127                                 finalMessage.Append (" ]");
128                                 finalMessage.Append (Environment.NewLine);
129                         }
130                         return finalMessage.ToString ();
131                 }
132         }
133 }
134 #endif