-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathExceptionExtensions.cs
197 lines (166 loc) · 5.78 KB
/
ExceptionExtensions.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.IO;
using System.Reflection;
using System.Text;
#if NET45_OR_GREATER || TARGETS_NETSTANDARD || TARGETS_NETCOREAPP
using System.Threading.Tasks;
#endif
using CodeJam.Strings;
using CodeJam.Targeting;
using JetBrains.Annotations;
namespace CodeJam
{
/// <summary>
/// The <see cref="Exception"/> class extensions.
/// </summary>
[PublicAPI]
public static class ExceptionExtensions
{
/// <summary>
/// Returns detailed exception text.
/// </summary>
/// <param name="exception">Exception to process.</param>
/// <param name="stringBuilder"><see cref="StringBuilder"/> instance.</param>
/// <returns>Detailed exception text.</returns>
public static StringBuilder ToDiagnosticString(this Exception exception, StringBuilder stringBuilder)
{
Code.NotNull(exception, nameof(exception));
Code.NotNull(stringBuilder, nameof(stringBuilder));
using var writer = new StringWriter(stringBuilder);
ToDiagnosticString(exception, writer, stringBuilder.Length == 0);
writer.Flush();
return stringBuilder;
}
/// <summary>
/// Returns detailed exception text.
/// </summary>
/// <param name="exception">Exception to process.</param>
/// <param name="writer"><see cref="TextWriter"/> instance.</param>
/// <param name="fromNewLine">If <c>true</c> - do not inject separator line from start.</param>
/// <returns>Detailed exception text.</returns>
public static void ToDiagnosticString(
this Exception? exception,
TextWriter writer,
bool fromNewLine = true)
{
Code.NotNull(writer, nameof(writer));
// ReSharper disable once PossibleNullReferenceException
for (var ex = exception; ex != null; ex = ex?.InnerException)
{
var exceptionText = $"Exception: {ex.GetType()}";
if (!fromNewLine)
{
for (var i = 0; i < exceptionText.Length; i++)
writer.Write('-');
writer.WriteLine();
}
else
fromNewLine = false;
writer.WriteLine(exceptionText);
if (ex.Message.NotNullNorEmpty())
writer.WriteLine(ex.Message);
if (ex.StackTrace.NotNullNorEmpty())
writer.WriteLine(ex.StackTrace);
switch (ex)
{
case FileNotFoundException fex:
writer.WriteLine($"File Name: {fex.FileName}");
if (fex.GetFusionLog().IsNullOrEmpty())
writer.WriteLine("Fusion log is empty or disabled.");
else
writer.Write(fex.GetFusionLog());
break;
case AggregateException aex:
var foundInnerException = false;
foreach (var e in aex.InnerExceptions)
{
foundInnerException = foundInnerException || e != ex.InnerException;
ToDiagnosticString(e, writer, false);
}
if (foundInnerException)
ex = ex.InnerException;
break;
case ReflectionTypeLoadException{LoaderExceptions: { } inners }:
foreach (var e in inners)
if (e != null)
ToDiagnosticString(e, writer, false);
break;
}
}
}
#if NET45_OR_GREATER || TARGETS_NETSTANDARD || TARGETS_NETCOREAPP // PUBLIC_API_CHANGES. TODO: update after fixes in Theraot.Core
/// <summary>
/// Returns detailed exception text.
/// </summary>
/// <param name="exception">Exception to process.</param>
/// <param name="writer"><see cref="TextWriter"/> instance.</param>
/// <param name="fromNewLine">If <c>true</c> - do not inject separator line from start.</param>
/// <returns>Detailed exception text.</returns>
public static Task ToDiagnosticStringAsync(
this Exception? exception,
TextWriter writer,
bool fromNewLine = true)
{
Code.NotNull(writer, nameof(writer));
return ToDiagnosticStringImplAsync(exception, writer, fromNewLine);
}
private static async Task ToDiagnosticStringImplAsync(
this Exception? exception,
TextWriter writer,
bool fromNewLine = true)
{
// ReSharper disable once PossibleNullReferenceException
for (var ex = exception; ex != null; ex = ex?.InnerException)
{
var exceptionText = $"Exception: {ex.GetType()}";
if (!fromNewLine)
{
for (var i = 0; i < exceptionText.Length; i++)
await writer.WriteAsync('-').ConfigureAwait(false);
await writer.WriteLineAsync().ConfigureAwait(false);
}
else
fromNewLine = false;
await writer.WriteLineAsync(exceptionText).ConfigureAwait(false);
if (ex.Message.NotNullNorEmpty())
await writer.WriteLineAsync(ex.Message).ConfigureAwait(false);
if (ex.StackTrace.NotNullNorEmpty())
await writer.WriteLineAsync(ex.StackTrace).ConfigureAwait(false);
switch (ex)
{
case FileNotFoundException fex:
await writer.WriteLineAsync($"File Name: {fex.FileName}").ConfigureAwait(false);
if (fex.GetFusionLog().IsNullOrEmpty())
await writer.WriteLineAsync("Fusion log is empty or disabled.").ConfigureAwait(false);
else
await writer.WriteAsync(fex.GetFusionLog()).ConfigureAwait(false);
break;
case AggregateException aex:
var foundInnerException = false;
foreach (var e in aex.InnerExceptions)
{
foundInnerException = foundInnerException || e != ex.InnerException;
await ToDiagnosticStringAsync(e, writer, false).ConfigureAwait(false);
}
if (foundInnerException)
ex = ex.InnerException;
break;
case ReflectionTypeLoadException {LoaderExceptions:{} inners}:
foreach (var e in inners)
if (e != null)
await ToDiagnosticStringAsync(e, writer, false).ConfigureAwait(false);
break;
}
}
}
#endif
/// <summary>
/// Returns detailed exception text.
/// </summary>
/// <param name="exception">Exception to process.</param>
/// <returns>Detailed exception text.</returns>
[Pure, System.Diagnostics.Contracts.Pure]
public static string ToDiagnosticString(this Exception? exception) =>
exception == null ? "" : exception.ToDiagnosticString(new StringBuilder()).ToString();
}
}