-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathError.js
More file actions
314 lines (274 loc) · 12.6 KB
/
Error.js
File metadata and controls
314 lines (274 loc) · 12.6 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#if COPYRIGHT
//------------------------------------------------------------------------------
// <copyright file="Error.js" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
#endif
Error.__typeName = 'Error';
Error.__class = true;
Error.create = function(message, errorInfo) {
/// <summary>Use this method to create a new error.</summary>
/// <param name="message" type="String" optional="true" mayBeNull="true">The error message.</param>
/// <param name="errorInfo" optional="true" mayBeNull="true">
/// A plain JavaScript object that contains extended information about the error.
/// The object should have a 'name' field that contains a string that identifies the error
/// and any additional fields that are necessary to fully describe the error.
/// </param>
/// <returns type="Error">An Error object.</returns>
// If message string can be converted to a number, IE sets e.message to the number, not the string.
// Workaround this issue by explicitly setting e.message to the string.
var err = new Error(message);
err.message = message;
if (errorInfo) {
for (var v in errorInfo) {
err[v] = errorInfo[v];
}
}
err.popStackFrame();
return err;
}
// The ArgumentException ctor in .NET has the message *before* paramName. This
// is inconsistent with all the other Argument*Exception ctors in .NET.
// We feel the paramName is more important than the message, and we want all our
// argument errors to be consistent, so our Error.argument() takes the paramName
// before the message. This is inconsistent with .NET, but overall we feel
// it is the better design.
Error.argument = function(paramName, message) {
/// <summary>
/// Creates an ArgumentException with a specified error message
/// and the name of the parameter that caused this exception.
/// </summary>
/// <param name="paramName" type="String" optional="true" mayBeNull="true">
/// The name of the parameter that caused the exception.
/// </param>
/// <param name="message" type="String" optional="true" mayBeNull="true">
/// A message that describes the error.
/// </param>
/// <returns>An Error instance that represents an ArgumentException.</returns>
var displayMessage = "Sys.ArgumentException: " + (message ? message : Sys.Res.argument);
if (paramName) {
displayMessage += "\n" + String.format(Sys.Res.paramName, paramName);
}
var err = Error.create(displayMessage, { name: "Sys.ArgumentException", paramName: paramName });
err.popStackFrame();
return err;
}
Error.argumentNull = function(paramName, message) {
/// <summary>
/// Creates an ArgumentNullException with a specified error message
/// and the name of the parameter that caused this exception.
/// </summary>
/// <param name="paramName" type="String" optional="true" mayBeNull="true">
/// The name of the parameter that caused the exception.
/// </param>
/// <param name="message" type="String" optional="true" mayBeNull="true">
/// A message that describes the error.
/// </param>
/// <returns>An Error instance that represents an ArgumentNullException.</returns>
var displayMessage = "Sys.ArgumentNullException: " + (message ? message : Sys.Res.argumentNull);
if (paramName) {
displayMessage += "\n" + String.format(Sys.Res.paramName, paramName);
}
var err = Error.create(displayMessage, { name: "Sys.ArgumentNullException", paramName: paramName });
err.popStackFrame();
return err;
}
Error.argumentOutOfRange = function(paramName, actualValue, message) {
/// <summary>
/// Creates an ArgumentOutOfRangeException with a specified error message
/// and the name and actual value of the parameter that caused this exception.
/// </summary>
/// <param name="paramName" type="String" optional="true" mayBeNull="true">
/// The name of the parameter that caused the exception.
/// </param>
/// <param name="actualValue" optional="true" mayBeNull="true">
/// The actual value of the parameter.
/// </param>
/// <param name="message" type="String" optional="true" mayBeNull="true">
/// A message that describes the error.
/// </param>
/// <returns>An Error instance that represents an ArgumentOutOfRangeException.</returns>
var displayMessage = "Sys.ArgumentOutOfRangeException: " + (message ? message : Sys.Res.argumentOutOfRange);
if (paramName) {
displayMessage += "\n" + String.format(Sys.Res.paramName, paramName);
}
// .NET implementation of ArgumentOutOfRangeException does not display actualValue if it is null.
// For parity with .NET, we do not display if actualValue is null or undefined. This is OK,
// since more specific exceptions exist for null and undefined.
if (typeof(actualValue) !== "undefined" && actualValue !== null) {
displayMessage += "\n" + String.format(Sys.Res.actualValue, actualValue);
}
var err = Error.create(displayMessage, {
name: "Sys.ArgumentOutOfRangeException",
paramName: paramName,
actualValue: actualValue
});
err.popStackFrame();
return err;
}
Error.argumentType = function(paramName, actualType, expectedType, message) {
/// <summary>
/// Creates an ArgumentTypeException with a specified error message
/// and the name, actual type, and expected type of the parameter that
/// caused this exception.
/// </summary>
/// <param name="paramName" type="String" optional="true" mayBeNull="true">
/// The name of the parameter that caused the exception.
/// </param>
/// <param name="actualType" type="Type" optional="true" mayBeNull="true">
/// The actual type of the parameter value.
/// </param>
/// <param name="expectedType" type="Type" optional="true" mayBeNull="true">
/// The expected type of the parameter value.
/// </param>
/// <param name="message" type="String" optional="true" mayBeNull="true">
/// A message that describes the error.
/// </param>
/// <returns>An Error instance that represents an ArgumentTypeException.</returns>
var displayMessage = "Sys.ArgumentTypeException: ";
if (message) {
displayMessage += message;
}
else if (actualType && expectedType) {
displayMessage +=
String.format(Sys.Res.argumentTypeWithTypes, actualType.getName(), expectedType.getName());
}
else {
displayMessage += Sys.Res.argumentType;
}
if (paramName) {
displayMessage += "\n" + String.format(Sys.Res.paramName, paramName);
}
var err = Error.create(displayMessage, {
name: "Sys.ArgumentTypeException",
paramName: paramName,
actualType: actualType,
expectedType: expectedType
});
err.popStackFrame();
return err;
}
Error.argumentUndefined = function(paramName, message) {
/// <summary>
/// Creates an ArgumentUndefinedException with a specified error message
/// and the name of the parameter that caused this exception.
/// </summary>
/// <param name="paramName" type="String" optional="true" mayBeNull="true">
/// The name of the parameter that caused the exception.
/// </param>
/// <param name="message" type="String" optional="true" mayBeNull="true">
/// A message that describes the error.
/// </param>
/// <returns>An Error instance that represents an ArgumentUndefinedException.</returns>
var displayMessage = "Sys.ArgumentUndefinedException: " + (message ? message : Sys.Res.argumentUndefined);
if (paramName) {
displayMessage += "\n" + String.format(Sys.Res.paramName, paramName);
}
var err = Error.create(displayMessage, { name: "Sys.ArgumentUndefinedException", paramName: paramName });
err.popStackFrame();
return err;
}
Error.format = function(message) {
/// <summary>
/// Creates a format error.
/// </summary>
/// <param name="message" type="String" optional="true" mayBeNull="true">The error message.</param>
/// <returns>An Error object that represents a FormatException.</returns>
var displayMessage = "Sys.FormatException: " + (message ? message : Sys.Res.format);
var err = Error.create(displayMessage, {name: 'Sys.FormatException'});
err.popStackFrame();
return err;
}
Error.invalidOperation = function(message) {
/// <summary>
/// Creates an invalid operation error.
/// </summary>
/// <param name="message" type="String" optional="true" mayBeNull="true">The error message.</param>
/// <returns>An Error instance that represents an InvalidOperationException.</returns>
var displayMessage = "Sys.InvalidOperationException: " + (message ? message : Sys.Res.invalidOperation);
var err = Error.create(displayMessage, {name: 'Sys.InvalidOperationException'});
err.popStackFrame();
return err;
}
Error.notImplemented = function(message) {
/// <summary>
/// Creates a not implemented error.
/// </summary>
/// <param name="message" type="String" optional="true" mayBeNull="true">The error message.</param>
/// <returns>An Error instance that represents a NotImplementedException.</returns>
var displayMessage = "Sys.NotImplementedException: " + (message ? message : Sys.Res.notImplemented);
var err = Error.create(displayMessage, {name: 'Sys.NotImplementedException'});
err.popStackFrame();
return err;
}
Error.parameterCount = function(message) {
/// <summary>
/// Creates a ParameterCountException with a specified error message.
/// </summary>
/// <param name="message" type="String" optional="true" mayBeNull="true">
/// A message that describes the error.
/// </param>
/// <returns>An Error instance that represents a ParameterCountException.</returns>
var displayMessage = "Sys.ParameterCountException: " + (message ? message : Sys.Res.parameterCount);
var err = Error.create(displayMessage, {name: 'Sys.ParameterCountException'});
err.popStackFrame();
return err;
}
Error.prototype.popStackFrame = function() {
/// <summary>
/// Updates the fileName and lineNumber fields based on the next frame in the
/// stack trace. Call this method whenever an instance of Error is returned
/// from a function. This makes the fileName and lineNumber reported in the
/// FireFox console point to the location where the exception was thrown, not
/// the location where the instance of Error was created.
/// </summary>
/// <example>
/// function checkParam(param, expectedType) {
/// if (!expectedType.isInstanceOfType(param)) {
/// var e = new Error("invalid type");
/// e.popStackFrame();
/// return e;
/// }
/// }
/// </example>
// Example stack frame
// ===================
// Error("test error")@:0
// createError()@http://localhost/app/Error.js:2
// throwError()@http://localhost/app/Error.js:6
// callThrowError()@http://localhost/app/Error.js:10
// @http://localhost/app/Error:js:14
if (typeof(this.stack) === "undefined" || this.stack === null ||
typeof(this.fileName) === "undefined" || this.fileName === null ||
typeof(this.lineNumber) === "undefined" || this.lineNumber === null) {
return;
}
var stackFrames = this.stack.split("\n");
// Find current stack frame. It may not be the first stack frame, since the very
// first frame when the Error is constructed does not correspond to any actual file
// or line number. See example stack frame above.
var currentFrame = stackFrames[0];
var pattern = this.fileName + ":" + this.lineNumber;
while(typeof(currentFrame) !== "undefined" &&
currentFrame !== null &&
currentFrame.indexOf(pattern) === -1) {
stackFrames.shift();
currentFrame = stackFrames[0];
}
var nextFrame = stackFrames[1];
// Special-case last stack frame, to stop shifting frames off the stack.
if (typeof(nextFrame) === "undefined" || nextFrame === null) {
return;
}
// Update fields to correspond with next stack frame
var nextFrameParts = nextFrame.match(/@(.*):(\d+)$/);
if (typeof(nextFrameParts) === "undefined" || nextFrameParts === null) {
return;
}
this.fileName = nextFrameParts[1];
// This should always succeed, since the regex matches "\d+"
this.lineNumber = parseInt(nextFrameParts[2]);
stackFrames.shift();
this.stack = stackFrames.join("\n");
}