Skip to content

Commit 709c3ca

Browse files
refactor: use the ExceptionHandler service
Fixes angular#533 Closes angular#672
1 parent a1f4060 commit 709c3ca

File tree

6 files changed

+38
-17
lines changed

6 files changed

+38
-17
lines changed

modules/angular2/src/core/application.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function _injectorBindings(appComponentType): List<Binding> {
7373
[appViewToken]),
7474
bind(appComponentType).toFactory((rootView) => rootView.elementInjectors[0].getComponent(),
7575
[appViewToken]),
76-
bind(LifeCycle).toFactory(() => new LifeCycle(null, assertionsEnabled()),[]),
76+
bind(LifeCycle).toFactory((exceptionHandler) => new LifeCycle(exceptionHandler, null, assertionsEnabled()),[ExceptionHandler]),
7777
bind(EventManager).toFactory((zone) => {
7878
var plugins = [new HammerGesturesPlugin()];
7979
return new EventManager(plugins, zone);

modules/angular2/src/core/life_cycle/life_cycle.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
import {FIELD, print} from 'angular2/src/facade/lang';
22
import {ChangeDetector} from 'angular2/change_detection';
33
import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone';
4+
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
45
import {ListWrapper} from 'angular2/src/facade/collection';
56
import {isPresent} from 'angular2/src/facade/lang';
67

78
export class LifeCycle {
9+
_errorHandler;
810
_changeDetector:ChangeDetector;
911
_enforceNoNewChanges:boolean;
1012

11-
constructor(changeDetector:ChangeDetector = null, enforceNoNewChanges:boolean = false) {
13+
constructor(exceptionHandler:ExceptionHandler, changeDetector:ChangeDetector = null, enforceNoNewChanges:boolean = false) {
14+
this._errorHandler = (exception, stackTrace) => {
15+
exceptionHandler.call(exception, stackTrace);
16+
throw exception;
17+
};
1218
this._changeDetector = changeDetector; // may be null when instantiated from application bootstrap
1319
this._enforceNoNewChanges = enforceNoNewChanges;
1420
}
1521

1622
registerWith(zone:VmTurnZone, changeDetector:ChangeDetector = null) {
17-
// temporary error handler, we should inject one
18-
var errorHandler = (exception, stackTrace) => {
19-
var longStackTrace = ListWrapper.join(stackTrace, "\n\n-----async gap-----\n");
20-
print(`${exception}\n\n${longStackTrace}`);
21-
throw exception;
22-
};
23-
2423
if (isPresent(changeDetector)) {
2524
this._changeDetector=changeDetector;
2625
}
2726

2827
zone.initCallbacks({
29-
onErrorHandler: errorHandler,
28+
onErrorHandler: this._errorHandler,
3029
onTurnDone: () => this.tick()
3130
});
3231
}

modules/angular2/test/core/compiler/shadow_dom/shadow_dom_emulation_integration_spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {isPresent, Type} from 'angular2/src/facade/lang';
66

77
import {Injector} from 'angular2/di';
88
import {Lexer, Parser, ChangeDetector, dynamicChangeDetection} from 'angular2/change_detection';
9+
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
910

1011
import {Compiler, CompilerCache} from 'angular2/src/core/compiler/compiler';
1112
import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle';
@@ -53,7 +54,7 @@ export function main() {
5354
compiler.compile(MyComp)
5455
.then(createView)
5556
.then((view) => {
56-
var lc = new LifeCycle(view.changeDetector, false);
57+
var lc = new LifeCycle(new ExceptionHandler(), view.changeDetector, false);
5758
assertions(view, lc);
5859
});
5960
}

modules/benchmarks/src/naive_infinite_scroll/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {MapWrapper} from 'angular2/src/facade/collection';
44

55
import {Parser, Lexer, ChangeDetector, ChangeDetection}
66
from 'angular2/change_detection';
7+
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
78
import {
89
bootstrap, Component, Viewport, Template, ViewContainer, Compiler, onChange
910
} from 'angular2/angular2';
@@ -225,9 +226,15 @@ export function setupReflectorForAngular() {
225226
'annotations': []
226227
});
227228

229+
reflector.registerType(ExceptionHandler, {
230+
"factory": () => new ExceptionHandler(),
231+
"parameters": [],
232+
"annotations": []
233+
});
234+
228235
reflector.registerType(LifeCycle, {
229-
"factory": (cd) => new LifeCycle(cd),
230-
"parameters": [[ChangeDetector]],
236+
"factory": (exHandler, cd) => new LifeCycle(exHandler, cd),
237+
"parameters": [[ExceptionHandler, ChangeDetector]],
231238
"annotations": []
232239
});
233240

modules/benchmarks/src/tree/tree_benchmark.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Parser, Lexer, ChangeDetector, ChangeDetection, jitChangeDetection}
22
from 'angular2/change_detection';
3+
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
34

45
import {bootstrap, Component, Viewport, Template, ViewContainer, Compiler} from 'angular2/angular2';
56

@@ -114,9 +115,15 @@ function setupReflector() {
114115
'annotations': []
115116
});
116117

118+
reflector.registerType(ExceptionHandler, {
119+
"factory": () => new ExceptionHandler(),
120+
"parameters": [],
121+
"annotations": []
122+
});
123+
117124
reflector.registerType(LifeCycle, {
118-
"factory": (cd) => new LifeCycle(cd),
119-
"parameters": [[ChangeDetector]],
125+
"factory": (exHandler, cd) => new LifeCycle(exHandler, cd),
126+
"parameters": [[ExceptionHandler, ChangeDetector]],
120127
"annotations": []
121128
});
122129

modules/examples/src/hello_world/index_static.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as app from './index_common';
22

33
import {Component, Decorator, Template, NgElement} from 'angular2/angular2';
44
import {Lexer, Parser, ChangeDetection, ChangeDetector} from 'angular2/change_detection';
5+
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
56
import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle';
67

78
import {Compiler, CompilerCache} from 'angular2/src/core/compiler/compiler';
@@ -94,9 +95,15 @@ function setup() {
9495
"annotations": []
9596
});
9697

98+
reflector.registerType(ExceptionHandler, {
99+
"factory": () => new ExceptionHandler(),
100+
"parameters": [],
101+
"annotations": []
102+
});
103+
97104
reflector.registerType(LifeCycle, {
98-
"factory": (cd) => new LifeCycle(cd),
99-
"parameters": [[ChangeDetector]],
105+
"factory": (exHandler, cd) => new LifeCycle(exHandler, cd),
106+
"parameters": [[ExceptionHandler, ChangeDetector]],
100107
"annotations": []
101108
});
102109

0 commit comments

Comments
 (0)