From 709c3ca6b5f0aa3360f5d6202e61bdee23a201fa Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Mon, 16 Feb 2015 14:55:00 +0100 Subject: [PATCH] refactor: use the ExceptionHandler service Fixes #533 Closes #672 --- modules/angular2/src/core/application.js | 2 +- .../angular2/src/core/life_cycle/life_cycle.js | 17 ++++++++--------- .../shadow_dom_emulation_integration_spec.js | 3 ++- .../src/naive_infinite_scroll/index.js | 11 +++++++++-- modules/benchmarks/src/tree/tree_benchmark.js | 11 +++++++++-- .../examples/src/hello_world/index_static.js | 11 +++++++++-- 6 files changed, 38 insertions(+), 17 deletions(-) diff --git a/modules/angular2/src/core/application.js b/modules/angular2/src/core/application.js index e247d675ea6..6a774972982 100644 --- a/modules/angular2/src/core/application.js +++ b/modules/angular2/src/core/application.js @@ -73,7 +73,7 @@ function _injectorBindings(appComponentType): List { [appViewToken]), bind(appComponentType).toFactory((rootView) => rootView.elementInjectors[0].getComponent(), [appViewToken]), - bind(LifeCycle).toFactory(() => new LifeCycle(null, assertionsEnabled()),[]), + bind(LifeCycle).toFactory((exceptionHandler) => new LifeCycle(exceptionHandler, null, assertionsEnabled()),[ExceptionHandler]), bind(EventManager).toFactory((zone) => { var plugins = [new HammerGesturesPlugin()]; return new EventManager(plugins, zone); diff --git a/modules/angular2/src/core/life_cycle/life_cycle.js b/modules/angular2/src/core/life_cycle/life_cycle.js index 23d6a32f19d..9d6d16e371d 100644 --- a/modules/angular2/src/core/life_cycle/life_cycle.js +++ b/modules/angular2/src/core/life_cycle/life_cycle.js @@ -1,32 +1,31 @@ import {FIELD, print} from 'angular2/src/facade/lang'; import {ChangeDetector} from 'angular2/change_detection'; import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone'; +import {ExceptionHandler} from 'angular2/src/core/exception_handler'; import {ListWrapper} from 'angular2/src/facade/collection'; import {isPresent} from 'angular2/src/facade/lang'; export class LifeCycle { + _errorHandler; _changeDetector:ChangeDetector; _enforceNoNewChanges:boolean; - constructor(changeDetector:ChangeDetector = null, enforceNoNewChanges:boolean = false) { + constructor(exceptionHandler:ExceptionHandler, changeDetector:ChangeDetector = null, enforceNoNewChanges:boolean = false) { + this._errorHandler = (exception, stackTrace) => { + exceptionHandler.call(exception, stackTrace); + throw exception; + }; this._changeDetector = changeDetector; // may be null when instantiated from application bootstrap this._enforceNoNewChanges = enforceNoNewChanges; } registerWith(zone:VmTurnZone, changeDetector:ChangeDetector = null) { - // temporary error handler, we should inject one - var errorHandler = (exception, stackTrace) => { - var longStackTrace = ListWrapper.join(stackTrace, "\n\n-----async gap-----\n"); - print(`${exception}\n\n${longStackTrace}`); - throw exception; - }; - if (isPresent(changeDetector)) { this._changeDetector=changeDetector; } zone.initCallbacks({ - onErrorHandler: errorHandler, + onErrorHandler: this._errorHandler, onTurnDone: () => this.tick() }); } diff --git a/modules/angular2/test/core/compiler/shadow_dom/shadow_dom_emulation_integration_spec.js b/modules/angular2/test/core/compiler/shadow_dom/shadow_dom_emulation_integration_spec.js index 147450ed803..1af6c75c1a6 100644 --- a/modules/angular2/test/core/compiler/shadow_dom/shadow_dom_emulation_integration_spec.js +++ b/modules/angular2/test/core/compiler/shadow_dom/shadow_dom_emulation_integration_spec.js @@ -6,6 +6,7 @@ import {isPresent, Type} from 'angular2/src/facade/lang'; import {Injector} from 'angular2/di'; import {Lexer, Parser, ChangeDetector, dynamicChangeDetection} from 'angular2/change_detection'; +import {ExceptionHandler} from 'angular2/src/core/exception_handler'; import {Compiler, CompilerCache} from 'angular2/src/core/compiler/compiler'; import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle'; @@ -53,7 +54,7 @@ export function main() { compiler.compile(MyComp) .then(createView) .then((view) => { - var lc = new LifeCycle(view.changeDetector, false); + var lc = new LifeCycle(new ExceptionHandler(), view.changeDetector, false); assertions(view, lc); }); } diff --git a/modules/benchmarks/src/naive_infinite_scroll/index.js b/modules/benchmarks/src/naive_infinite_scroll/index.js index 7b08debf26d..b50dc9d0613 100644 --- a/modules/benchmarks/src/naive_infinite_scroll/index.js +++ b/modules/benchmarks/src/naive_infinite_scroll/index.js @@ -4,6 +4,7 @@ import {MapWrapper} from 'angular2/src/facade/collection'; import {Parser, Lexer, ChangeDetector, ChangeDetection} from 'angular2/change_detection'; +import {ExceptionHandler} from 'angular2/src/core/exception_handler'; import { bootstrap, Component, Viewport, Template, ViewContainer, Compiler, onChange } from 'angular2/angular2'; @@ -225,9 +226,15 @@ export function setupReflectorForAngular() { 'annotations': [] }); + reflector.registerType(ExceptionHandler, { + "factory": () => new ExceptionHandler(), + "parameters": [], + "annotations": [] + }); + reflector.registerType(LifeCycle, { - "factory": (cd) => new LifeCycle(cd), - "parameters": [[ChangeDetector]], + "factory": (exHandler, cd) => new LifeCycle(exHandler, cd), + "parameters": [[ExceptionHandler, ChangeDetector]], "annotations": [] }); diff --git a/modules/benchmarks/src/tree/tree_benchmark.js b/modules/benchmarks/src/tree/tree_benchmark.js index 976360a1318..0387f9d7b27 100644 --- a/modules/benchmarks/src/tree/tree_benchmark.js +++ b/modules/benchmarks/src/tree/tree_benchmark.js @@ -1,5 +1,6 @@ import {Parser, Lexer, ChangeDetector, ChangeDetection, jitChangeDetection} from 'angular2/change_detection'; +import {ExceptionHandler} from 'angular2/src/core/exception_handler'; import {bootstrap, Component, Viewport, Template, ViewContainer, Compiler} from 'angular2/angular2'; @@ -114,9 +115,15 @@ function setupReflector() { 'annotations': [] }); + reflector.registerType(ExceptionHandler, { + "factory": () => new ExceptionHandler(), + "parameters": [], + "annotations": [] + }); + reflector.registerType(LifeCycle, { - "factory": (cd) => new LifeCycle(cd), - "parameters": [[ChangeDetector]], + "factory": (exHandler, cd) => new LifeCycle(exHandler, cd), + "parameters": [[ExceptionHandler, ChangeDetector]], "annotations": [] }); diff --git a/modules/examples/src/hello_world/index_static.js b/modules/examples/src/hello_world/index_static.js index 9f425ae1be7..b4403cc6b12 100644 --- a/modules/examples/src/hello_world/index_static.js +++ b/modules/examples/src/hello_world/index_static.js @@ -2,6 +2,7 @@ import * as app from './index_common'; import {Component, Decorator, Template, NgElement} from 'angular2/angular2'; import {Lexer, Parser, ChangeDetection, ChangeDetector} from 'angular2/change_detection'; +import {ExceptionHandler} from 'angular2/src/core/exception_handler'; import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle'; import {Compiler, CompilerCache} from 'angular2/src/core/compiler/compiler'; @@ -94,9 +95,15 @@ function setup() { "annotations": [] }); + reflector.registerType(ExceptionHandler, { + "factory": () => new ExceptionHandler(), + "parameters": [], + "annotations": [] + }); + reflector.registerType(LifeCycle, { - "factory": (cd) => new LifeCycle(cd), - "parameters": [[ChangeDetector]], + "factory": (exHandler, cd) => new LifeCycle(exHandler, cd), + "parameters": [[ExceptionHandler, ChangeDetector]], "annotations": [] });