From 2ff34cace930d428b5acffb24cbe3491561a6933 Mon Sep 17 00:00:00 2001 From: Pavel J Date: Tue, 25 Feb 2014 22:01:10 -0500 Subject: [PATCH] fix(scope): correctly setup NgZone onError handler with ExceptionHandler --- lib/core/scope.dart | 1 + test/core/scope_spec.dart | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/core/scope.dart b/lib/core/scope.dart index 1ab469663..7a86b02d2 100644 --- a/lib/core/scope.dart +++ b/lib/core/scope.dart @@ -388,6 +388,7 @@ class RootScope extends Scope { new RootWatchGroup(new DirtyCheckingChangeDetector(cacheGetter), context)) { _zone.onTurnDone = apply; + _zone.onError = (e, s, ls) => _exceptionHandler(e, s); } RootScope get rootScope => this; diff --git a/test/core/scope_spec.dart b/test/core/scope_spec.dart index 15554f4e9..7296264e8 100644 --- a/test/core/scope_spec.dart +++ b/test/core/scope_spec.dart @@ -3,6 +3,7 @@ library scope2_spec; import '../_specs.dart'; import 'package:angular/change_detection/change_detection.dart' hide ExceptionHandler; import 'package:angular/change_detection/dirty_checking_change_detector.dart'; +import 'dart:async'; import 'dart:math'; main() => describe('scope', () { @@ -1285,6 +1286,40 @@ main() => describe('scope', () { }); }); }); + + describe('exceptionHander', () { + it('should call ExceptionHandler on zone errors', () { + module((Module module) { + module.type(ExceptionHandler, implementedBy: LoggingExceptionHandler); + }); + async((inject((RootScope rootScope, NgZone zone, ExceptionHandler e) { + zone.run(() { + scheduleMicrotask(() => throw 'my error'); + }); + var errors = (e as LoggingExceptionHandler).errors; + expect(errors.length).toEqual(1); + expect(errors.first.error).toEqual('my error'); + }))); + }); + + it('should call ExceptionHandler on digest errors', () { + module((Module module) { + module.type(ExceptionHandler, implementedBy: LoggingExceptionHandler); + }); + async((inject((RootScope rootScope, NgZone zone, ExceptionHandler e) { + rootScope.context['badOne'] = () => new Map(); + rootScope.watch('badOne()', (_, __) => null); + + try { + zone.run(() => null); + } catch(_) {} + + var errors = (e as LoggingExceptionHandler).errors; + expect(errors.length).toEqual(1); + expect(errors.first.error, startsWith('Model did not stabilize')); + }))); + }); + }); }); @NgFilter(name: 'multiply')