OLD | NEW |
(Empty) | |
| 1 library catch_errors; |
| 2 |
| 3 import 'dart:async'; |
| 4 |
| 5 Stream catchErrors(void body()) { |
| 6 StreamController controller; |
| 7 |
| 8 bool onError(e, st) { |
| 9 controller.add(e); |
| 10 return true; |
| 11 } |
| 12 |
| 13 void onListen() { |
| 14 runZoned(body, onError: onError); |
| 15 } |
| 16 |
| 17 controller = new StreamController(onListen: onListen); |
| 18 return controller.stream; |
| 19 } |
| 20 |
| 21 runZonedScheduleMicrotask(body(), |
| 22 {void onScheduleMicrotask(void callback()), Function onError}) { |
| 23 if (onScheduleMicrotask == null) { |
| 24 return runZoned(body, onError: onError); |
| 25 } |
| 26 HandleUncaughtErrorHandler errorHandler; |
| 27 if (onError != null) { |
| 28 errorHandler = (Zone self, ZoneDelegate parent, Zone zone, error, |
| 29 StackTrace stackTrace) { |
| 30 try { |
| 31 return self.parent.runUnary(onError, error); |
| 32 } catch (e, s) { |
| 33 if (identical(e, error)) { |
| 34 return parent.handleUncaughtError(zone, error, stackTrace); |
| 35 } else { |
| 36 return parent.handleUncaughtError(zone, e, s); |
| 37 } |
| 38 } |
| 39 }; |
| 40 } |
| 41 ScheduleMicrotaskHandler asyncHandler; |
| 42 if (onScheduleMicrotask != null) { |
| 43 asyncHandler = (Zone self, ZoneDelegate parent, Zone zone, f()) { |
| 44 self.parent.runUnary(onScheduleMicrotask, () => zone.runGuarded(f)); |
| 45 }; |
| 46 } |
| 47 ZoneSpecification specification = new ZoneSpecification( |
| 48 handleUncaughtError: errorHandler, scheduleMicrotask: asyncHandler); |
| 49 Zone zone = Zone.current.fork(specification: specification); |
| 50 if (onError != null) { |
| 51 return zone.runGuarded(body); |
| 52 } else { |
| 53 return zone.run(body); |
| 54 } |
| 55 } |
OLD | NEW |