-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy patherror_stacktrace_test.dart
326 lines (294 loc) · 9.52 KB
/
error_stacktrace_test.dart
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
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Tests that the `Error.stackTrace` is set when thrown, not before,
// and that it contains the same stack trace text as the stack trace
// captured by `catch` the first time the error is thrown,
// and that it doesn't change if thrown again.
// (Derived from `runtime/tests/vm/dart/error_stacktrace_test.dart`.)
import "package:expect/async_helper.dart";
import "package:expect/expect.dart";
void main() async {
testSync();
testSyncStar(); // A `throw` in a `sync*` function.
asyncStart();
await testAsync();
await testAsyncStar();
asyncEnd();
}
void testSync() {
Never throwError(Error error) => throw error;
Never throwWithStack(Error error, StackTrace stack) =>
Error.throwWithStackTrace(error, stack);
dynamic throwNSM(dynamic c) => c * 4;
_testSync("sync", throwError, throwWithStack, throwNSM);
}
void testSyncStar() {
Iterable<Never> throwErrorStream(Error error) sync* {
yield throw error;
}
Iterable<Never> throwWithStackStream(Error error, StackTrace stack) sync* {
yield Error.throwWithStackTrace(error, stack);
}
Iterable<dynamic> throwNSMStream(dynamic c) sync* {
yield c * 4;
}
Never throwError(Error error) => throwErrorStream(error).first;
Never throwWithStack(Error error, StackTrace stack) =>
throwWithStackStream(error, stack).first;
dynamic throwNSM(dynamic c) => throwNSMStream(c).first;
_testSync("sync*", throwError, throwWithStack, throwNSM);
}
Future<void> testAsync() async {
Future<Never> throwError(Error error) async => throw error;
Future<Never> throwErrorWithStack(Error error, StackTrace stack) async =>
Error.throwWithStackTrace(error, stack);
Future<dynamic> throwNSM(dynamic c) async => c * 4;
return _testAsync("async", throwError, throwErrorWithStack, throwNSM);
}
Future<void> testAsyncStar() async {
Stream<Never> throwErrorStream(Error error) async* {
yield throw error;
}
Future<Never> throwError(Error error) => throwErrorStream(error).first;
Stream<Never> throwErrorWithStackStream(
Error error,
StackTrace stack,
) async* {
yield Error.throwWithStackTrace(error, stack);
}
Future<Never> throwErrorWithStack(Error error, StackTrace stack) =>
throwErrorWithStackStream(error, stack).first;
Stream<dynamic> throwNSMStream(dynamic c) async* {
yield c * 4;
}
Future<dynamic> throwNSM(dynamic c) => throwNSMStream(c).first;
return _testAsync("async*", throwError, throwErrorWithStack, throwNSM);
}
void _testSync(
String functionKind,
Never Function(Error) throwError,
Never Function(Error, StackTrace) throwWithStack,
dynamic Function(dynamic) throwNSM,
) {
// Checks that an error first thrown with [firstStack] as [Error.stackTrace],
// will keep that stack trace if thrown again asynchronously.
void testErrorSet(String throwKind, Error error, StackTrace firstStack) {
var desc = "$functionKind $throwKind";
// Was thrown with [stackTrace] as stack trace.
Expect.isNotNull(error.stackTrace, "$desc throw - did not set .stackTrace");
Expect.stringEquals(
firstStack.toString(),
error.stackTrace.toString(),
"$desc, caught stack/set stack - not same",
);
// Throw same error again, using `throw`, with different stack.
try {
throwError(error);
} on Error catch (e, s) {
var redesc = "$functionKind throw again";
Expect.identical(error, e, "$redesc - not same error");
Expect.notEquals(
firstStack.toString(),
s.toString(),
"$redesc, set stack/new stack - not different",
);
// Did not overwrite existing `error.stackTrace`.
Expect.equals(
firstStack.toString(),
e.stackTrace.toString(),
"$redesc - changed .stackTrace",
);
}
// Throw same error again using `Error.throwWithStackTrace`.
var stack2 = StackTrace.fromString("stack test string 2");
try {
throwWithStack(error, stack2);
} on Error catch (e, s) {
var redesc = "$functionKind E.tWST again";
Expect.identical(error, e, "$redesc - not same error");
Expect.equals(
stack2.toString(),
s.toString(),
"$redesc, thrown stack/caught stack - not same",
);
Expect.notEquals(
firstStack.toString(),
s.toString(),
"$redesc, first stack/new stack - not different",
);
// Did not overwrite existing `error.stackTrace`.
Expect.equals(
firstStack.toString(),
e.stackTrace.toString(),
"$redesc - changed .stackTrace",
);
}
}
{
// System thrown error.
try {
throwNSM(NoMult());
Expect.fail("Did not throw");
} on NoSuchMethodError catch (e, s) {
testErrorSet("throwNSM", e, s);
}
}
{
// User thrown error, explicit `throw`.
var error = StateError("error test string");
Expect.isNull(error.stackTrace);
try {
throwError(error);
} on Error catch (e, s) {
Expect.identical(
error,
e,
"$functionKind throw: thrown error/caught error - not same",
);
testErrorSet("throw", e, s);
}
}
{
// Thrown using `Error.throwWithStackTrace`.
var error = StateError("error test string");
Expect.isNull(error.stackTrace);
var stack = StackTrace.fromString("stack test string");
try {
throwWithStack(error, stack);
} on Error catch (e, s) {
Expect.identical(
error,
e,
"$functionKind E.tWST: thrown error/caught error - not same",
);
Expect.stringEquals(
stack.toString(),
s.toString(),
"$functionKind E.tWST: thrown stack/caught stack - not same",
);
testErrorSet("E.tWST", e, s);
}
}
}
Future<void> _testAsync(
String functionKind,
Future<Never> Function(Error) throwError,
Future<Never> Function(Error, StackTrace) throwWithStack,
Future<dynamic> Function(dynamic) throwNSM,
) async {
// Checks that an error first thrown with [firstStack] as [Error.stackTrace],
// will keep that stack trace if thrown again asynchronously.
Future<void> testErrorSet(
String throwKind,
Error error,
StackTrace firstStack,
) async {
var desc = "$functionKind $throwKind";
// Was thrown with [stackTrace] as stack trace.
Expect.isNotNull(error.stackTrace, "$desc throw - did not set .stackTrace");
Expect.stringEquals(
firstStack.toString(),
error.stackTrace.toString(),
"$desc, caught stack/set stack - not same",
);
// Throw same error again, using `throw`, with different stack.
try {
await throwError(error);
} on Error catch (e, s) {
var redesc = "$functionKind throw again";
Expect.identical(error, e, "$redesc - not same error");
if (functionKind != "async*") {
// An async* throw happens asynchronously, so its stack trace
// can be just the same short stack from the event loop every time.
Expect.notEquals(
firstStack.toString(),
s.toString(),
"$redesc, set stack/new stack - not different",
);
}
// Did not overwrite existing `error.stackTrace`.
Expect.equals(
firstStack.toString(),
e.stackTrace.toString(),
"$redesc - changed .stackTrace",
);
}
// Throw same error again using `Error.throwWithStackTrace`.
var stack2 = StackTrace.fromString("stack test string 2");
try {
await throwWithStack(error, stack2);
} on Error catch (e, s) {
var redesc = "$functionKind E.tWST again";
Expect.identical(error, e, "$redesc - not same error");
Expect.equals(
stack2.toString(),
s.toString(),
"$redesc, thrown stack/caught stack - not same",
);
if (functionKind != "async*") {
Expect.notEquals(
firstStack.toString(),
s.toString(),
"$redesc, first stack/new stack - not different",
);
}
// Did not overwrite existing `error.stackTrace`.
Expect.equals(
firstStack.toString(),
e.stackTrace.toString(),
"$redesc - changed .stackTrace",
);
}
}
asyncStart();
{
// System thrown error.
try {
await throwNSM(NoMult());
Expect.fail("Did not throw");
} on NoSuchMethodError catch (e, s) {
await testErrorSet("throwNSM", e, s);
}
}
{
// User thrown error, explicit `throw`.
var error = StateError("error test string");
Expect.isNull(error.stackTrace);
try {
await throwError(error);
} on Error catch (e, s) {
Expect.identical(
error,
e,
"$functionKind throw: thrown error/caught error - not same",
);
await testErrorSet("throw", e, s);
}
}
{
// Thrown using `Error.throwWithStackTrace`.
var error = StateError("error test string");
Expect.isNull(error.stackTrace);
var stack = StackTrace.fromString("stack test string");
try {
await throwWithStack(error, stack);
} on Error catch (e, s) {
Expect.identical(
error,
e,
"$functionKind E.tWST: thrown error/caught error - not same",
);
Expect.stringEquals(
stack.toString(),
s.toString(),
"$functionKind E.tWST: thrown stack/caught stack - not same",
);
await testErrorSet("E.tWST", e, s);
}
}
asyncEnd();
}
// Has no `operator *`, forcing a dynamic `NoSuchMethodError`
// when used in `c * 4`.
class NoMult {}