-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace_errors_of_unawaited_future_example.dart
More file actions
43 lines (37 loc) · 1.38 KB
/
trace_errors_of_unawaited_future_example.dart
File metadata and controls
43 lines (37 loc) · 1.38 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
// ignore_for_file: discarded_futures
import 'dart:async';
import 'package:error_trace/error_trace.dart';
import 'fakes/network_fakes.dart';
void main() {
runZonedGuarded(() {
// Calls `fakeNetworkOperation()` without chaining possible exceptions
fakeNetworkOperation(fail: true);
}, (error, stackTrace) {
// In this case 'stackTrace' only contains the call to
// `fakeNetworkOperation()` function and doesn't contain any `main()`
// functions calls. This makes hard to know when `fakeNetworkOperation()`
// was called.
print('### Uncaught error (Without chaining exceptions):');
printError(error, stackTrace);
print('');
});
runZonedGuarded(() {
// Calls `fakeNetworkOperation()` chaining possible exceptions by throwing a
// `Traceable`.
fakeNetworkOperation(fail: true)
.catchError((e, st) => throw NetworkException(e, st));
}, (error, stackTrace) {
// In this case `stackTrace` contains all the calls to
// `fakeNetworkOperation()`, and `main()` functions. This makes easy to know
// when `fakeNetworkOperation()` was called.
print('### Uncaught error (Chaining exceptions by throwing a Traceable):');
printError(error, stackTrace);
print('');
});
}
class NetworkException extends TraceableException {
NetworkException(
super.causeError,
super.causeStackTrace,
) : super(name: 'NetworkException');
}