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()), |
| 23 Function onError }) { |
| 24 if (onScheduleMicrotask == null) { |
| 25 return runZoned(body, onError: onError); |
| 26 } |
| 27 HandleUncaughtErrorHandler errorHandler; |
| 28 if (onError != null) { |
| 29 errorHandler = (Zone self, ZoneDelegate parent, Zone zone, |
| 30 error, StackTrace stackTrace) { |
| 31 try { |
| 32 return self.parent.runUnary(onError, error); |
| 33 } catch(e, s) { |
| 34 if (identical(e, error)) { |
| 35 return parent.handleUncaughtError(zone, error, stackTrace); |
| 36 } else { |
| 37 return parent.handleUncaughtError(zone, e, s); |
| 38 } |
| 39 } |
| 40 }; |
| 41 } |
| 42 ScheduleMicrotaskHandler asyncHandler; |
| 43 if (onScheduleMicrotask != null) { |
| 44 asyncHandler = (Zone self, ZoneDelegate parent, Zone zone, f()) { |
| 45 self.parent.runUnary(onScheduleMicrotask, () => zone.runGuarded(f)); |
| 46 }; |
| 47 } |
| 48 ZoneSpecification specification = |
| 49 new ZoneSpecification(handleUncaughtError: errorHandler, |
| 50 scheduleMicrotask: asyncHandler); |
| 51 Zone zone = Zone.current.fork(specification: specification); |
| 52 if (onError != null) { |
| 53 return zone.runGuarded(body); |
| 54 } else { |
| 55 return zone.run(body); |
| 56 } |
| 57 } |
OLD | NEW |