-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAggregateException.cs
165 lines (143 loc) · 4.3 KB
/
AggregateException.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (C) 2003-2010 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Created by: Alex Yakunin
// Created: 2008.07.03
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Security;
using System.Text;
using System.Linq;
namespace Xtensive.Core
{
/// <summary>
/// Aggregates a set of caught exceptions.
/// </summary>
[Serializable]
public class AggregateException : Exception
{
private Exception[] exceptions;
/// <summary>
/// Gets the list of caught exceptions.
/// </summary>
public IReadOnlyList<Exception> Exceptions
{
[DebuggerStepThrough]
get { return exceptions; }
}
/// <summary>
/// Gets the "flat" list with all aggregated exceptions.
/// If other <see cref=" AggregateException"/>s were aggregated,
/// their inner exceptions are included instead of them.
/// </summary>
/// <returns>Flat list of aggregated exceptions.</returns>
public List<Exception> GetFlatExceptions()
{
var result = new List<Exception>();
foreach (var exception in exceptions) {
var ae = exception as AggregateException;
if (ae!=null)
result.AddRange(ae.GetFlatExceptions());
else
result.Add(exception);
}
return result;
}
/// <inheritdoc/>
public override string ToString()
{
StringBuilder sb = new StringBuilder(64);
sb.Append(base.ToString());
sb.AppendFormat("\r\n{0}:", Strings.OriginalExceptions);
int i = 1;
foreach (Exception exception in exceptions)
sb.AppendFormat("\r\n{0}: {1}", i++, exception);
return sb.ToString();
}
#region Private \ internal methods
private void SetExceptions(Exception[] exceptions)
{
this.exceptions = exceptions;
}
private void SetExceptions(Exception exception)
{
exceptions = new Exception[] { exception };
}
#endregion
// Constructors
/// <summary>
/// Initializes a new instance of this type.
/// </summary>
public AggregateException()
: base(Strings.ExASetOfExceptionsIsCaught)
{
}
/// <summary>
/// Initializes a new instance of this type.
/// </summary>
/// <param name="text">Text of message.</param>
public AggregateException(string text)
: base(text)
{
}
/// <summary>
/// Initializes a new instance of this type.
/// </summary>
/// <param name="message">Text of message.</param>
/// <param name="innerException">Inner exception.</param>
public AggregateException(string message, Exception innerException)
: base(message, innerException)
{
SetExceptions(innerException);
}
/// <summary>
/// Initializes a new instance of this type.
/// </summary>
/// <param name="exceptions">Inner exceptions.</param>
public AggregateException(Exception[] exceptions)
: base(Strings.ExASetOfExceptionsIsCaught, exceptions.First())
{
SetExceptions(exceptions);
}
/// <summary>
/// Initializes a new instance of this type.
/// </summary>
/// <param name="message">Text of message.</param>
/// <param name="exceptions">Inner exceptions.</param>
public AggregateException(string message, Exception[] exceptions)
: base(message, exceptions.First())
{
SetExceptions(exceptions);
}
// Serialization
/// <summary>
/// Deserializes instance of this type.
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
#if NET8_0_OR_GREATER
[Obsolete(DiagnosticId = "SYSLIB0051")]
#endif
protected AggregateException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
exceptions = (Exception[]) info.GetValue("Exceptions", typeof (Exception[]));
}
/// <summary>
/// Serializes instance of this type.
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
[SecurityCritical]
#if NET8_0_OR_GREATER
[Obsolete(DiagnosticId = "SYSLIB0051")]
#endif
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("Exceptions", exceptions);
}
}
}