-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
InternalLoggerException.cs
267 lines (238 loc) · 9.97 KB
/
InternalLoggerException.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Runtime.Serialization;
#if FEATURE_SECURITY_PERMISSIONS
using System.Security.Permissions;
#endif
using Microsoft.Build.Shared;
using Microsoft.Build.Framework;
using Microsoft.Build.Framework.BuildException;
using System.Collections.Generic;
#nullable disable
namespace Microsoft.Build.Exceptions
{
/// <summary>
/// This exception is used to wrap an unhandled exception from a logger. This exception aborts the build, and it can only be
/// thrown by the MSBuild engine.
/// </summary>
// WARNING: marking a type [Serializable] without implementing ISerializable imposes a serialization contract -- it is a
// promise to never change the type's fields i.e. the type is immutable; adding new fields in the next version of the type
// without following certain special FX guidelines, can break both forward and backward compatibility
[Serializable]
public sealed class InternalLoggerException : BuildExceptionBase
{
#region Unusable constructors
/// <summary>
/// Default constructor.
/// </summary>
/// <remarks>
/// This constructor only exists to satisfy .NET coding guidelines. Use the rich constructor instead.
/// </remarks>
/// <exception cref="InvalidOperationException"></exception>
public InternalLoggerException()
{
ErrorUtilities.ThrowInvalidOperation("InternalLoggerExceptionOnlyThrownByEngine");
}
/// <summary>
/// Creates an instance of this exception using the specified error message.
/// </summary>
/// <remarks>
/// This constructor only exists to satisfy .NET coding guidelines. Use the rich constructor instead.
/// </remarks>
/// <param name="message"></param>
/// <exception cref="InvalidOperationException"></exception>
public InternalLoggerException(string message)
: base(message)
{
ErrorUtilities.ThrowInvalidOperation("InternalLoggerExceptionOnlyThrownByEngine");
}
/// <summary>
/// Creates an instance of this exception using the specified error message and inner exception.
/// </summary>
/// <remarks>
/// This constructor only exists to satisfy .NET coding guidelines. Use the rich constructor instead.
/// </remarks>
/// <param name="message"></param>
/// <param name="innerException"></param>
/// <exception cref="InvalidOperationException"></exception>
public InternalLoggerException(string message, Exception innerException)
: base(message, innerException)
{ }
#endregion
/// <summary>
/// Creates an instance of this exception using rich error information.
/// Internal for unit testing
/// </summary>
/// <remarks>This is the only usable constructor.</remarks>
/// <param name="message"></param>
/// <param name="innerException"></param>
/// <param name="e">Can be null.</param>
/// <param name="errorCode"></param>
/// <param name="helpKeyword"></param>
/// <param name="initializationException"></param>
internal InternalLoggerException(
string message,
Exception innerException,
BuildEventArgs e,
string errorCode,
string helpKeyword,
bool initializationException)
: base(message, innerException)
{
ErrorUtilities.VerifyThrow(!string.IsNullOrEmpty(message), "Need error message.");
ErrorUtilities.VerifyThrow(innerException != null || initializationException, "Need the logger exception.");
ErrorUtilities.VerifyThrow(!string.IsNullOrEmpty(errorCode), "Must specify the error message code.");
ErrorUtilities.VerifyThrow(!string.IsNullOrEmpty(helpKeyword), "Must specify the help keyword for the IDE.");
this.e = e;
this.errorCode = errorCode;
this.helpKeyword = helpKeyword;
this.initializationException = initializationException;
}
#region Serialization (update when adding new class members)
/// <summary>
/// Protected constructor used for (de)serialization.
/// If we ever add new members to this class, we'll need to update this.
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
#if NET8_0_OR_GREATER
[Obsolete(DiagnosticId = "SYSLIB0051")]
#endif
private InternalLoggerException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
e = (BuildEventArgs)info.GetValue("e", typeof(BuildEventArgs));
errorCode = info.GetString("errorCode");
helpKeyword = info.GetString("helpKeyword");
initializationException = info.GetBoolean("initializationException");
}
/// <summary>
/// ISerializable method which we must override since Exception implements this interface
/// If we ever add new members to this class, we'll need to update this.
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
#if FEATURE_SECURITY_PERMISSIONS
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
#endif
#if NET8_0_OR_GREATER
[Obsolete(DiagnosticId = "SYSLIB0051")]
#endif
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("e", e);
info.AddValue("errorCode", errorCode);
info.AddValue("helpKeyword", helpKeyword);
info.AddValue("initializationException", initializationException);
}
protected override IDictionary<string, string> FlushCustomState()
{
return new Dictionary<string, string>()
{
{ nameof(errorCode), errorCode },
{ nameof(helpKeyword), helpKeyword },
{ nameof(initializationException), initializationException.ToString() },
};
}
protected override void InitializeCustomState(IDictionary<string, string> state)
{
errorCode = state[nameof(errorCode)];
helpKeyword = state[nameof(helpKeyword)];
initializationException = bool.Parse(state[nameof(initializationException)]);
}
/// <summary>
/// Provide default values for optional members
/// </summary>
[OnDeserializing] // Will happen before the object is deserialized
private void SetDefaultsBeforeSerialization(StreamingContext sc)
{
initializationException = false;
}
/// <summary>
/// Don't actually have anything to do in the method, but the method is required when implementing an optional field
/// </summary>
[OnDeserialized]
private void SetValueAfterDeserialization(StreamingContext sx)
{
// Have nothing to do
}
#endregion
#region Properties
/// <summary>
/// Gets the details of the build event (if any) that was being logged.
/// </summary>
/// <value>The build event args, or null.</value>
public BuildEventArgs BuildEventArgs
{
get
{
return e;
}
}
/// <summary>
/// Gets the error code associated with this exception's message (not the inner exception).
/// </summary>
/// <value>The error code string.</value>
public string ErrorCode
{
get
{
return errorCode;
}
}
/// <summary>
/// Gets the F1-help keyword associated with this error, for the host IDE.
/// </summary>
/// <value>The keyword string.</value>
public string HelpKeyword
{
get
{
return helpKeyword;
}
}
/// <summary>
/// True if the exception occurred during logger initialization
/// </summary>
public bool InitializationException
{
get
{
return initializationException;
}
}
#endregion
/// <summary>
/// Throws an instance of this exception using rich error information.
/// </summary>
/// <param name="innerException"></param>
/// <param name="e">Can be null.</param>
/// <param name="messageResourceName"></param>
/// <param name="initializationException"></param>
/// <param name="messageArgs"></param>
internal static void Throw(
Exception innerException,
BuildEventArgs e,
string messageResourceName,
bool initializationException,
params string[] messageArgs)
{
ErrorUtilities.VerifyThrow(messageResourceName != null, "Need error message.");
string errorCode;
string helpKeyword;
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out errorCode, out helpKeyword, messageResourceName, messageArgs);
throw new InternalLoggerException(message, innerException, e, errorCode, helpKeyword, initializationException);
}
// the event that was being logged when a logger failed (can be null)
private BuildEventArgs e;
// the error code for this exception's message (not the inner exception)
private string errorCode;
// the F1-help keyword for the host IDE
private string helpKeyword;
// This flag is set to indicate that the exception occurred during logger initialization
[OptionalField(VersionAdded = 2)]
private bool initializationException;
}
}