X Tutup
Skip to content

Commit 07b9be7

Browse files
committed
fix(exception_handler): log errors that are thrown by the compiler
1 parent b4a0629 commit 07b9be7

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

modules/angular2/src/core/application_common.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,8 @@ function _injectorBindings(appComponentType): List<Type | Binding | List<any>> {
158158
];
159159
}
160160

161-
export function createNgZone(handler: ExceptionHandler): NgZone {
162-
// bootstrapErrorReporter is needed because we cannot use custom exception handler
163-
// configured via DI until the root Injector has been created.
164-
var bootstrapErrorReporter = (exception, stackTrace) => handler.call(exception, stackTrace);
165-
var zone = new NgZone({enableLongStackTrace: assertionsEnabled()});
166-
zone.overrideOnErrorHandler(bootstrapErrorReporter);
167-
return zone;
161+
export function createNgZone(): NgZone {
162+
return new NgZone({enableLongStackTrace: assertionsEnabled()});
168163
}
169164

170165
/**
@@ -299,16 +294,16 @@ export function commonBootstrap(
299294
BrowserDomAdapter.makeCurrent();
300295
wtfInit();
301296
var bootstrapProcess = PromiseWrapper.completer();
302-
var zone = createNgZone(new ExceptionHandler(DOM, isDart ? false : true));
303-
zone.run(() => {
304-
// TODO(rado): prepopulate template cache, so applications with only
305-
// index.html and main.js are possible.
297+
var zone = createNgZone();
306298

307-
var appInjector = _createAppInjector(appComponentType, componentInjectableBindings, zone);
308-
var exceptionHandler = appInjector.get(ExceptionHandler);
309-
zone.overrideOnErrorHandler((e, s) => exceptionHandler.call(e, s));
299+
zone.run(() => {
300+
var exceptionHandler;
310301

311302
try {
303+
var appInjector = _createAppInjector(appComponentType, componentInjectableBindings, zone);
304+
exceptionHandler = appInjector.get(ExceptionHandler);
305+
zone.overrideOnErrorHandler((e, s) => exceptionHandler.call(e, s));
306+
312307
var compRefToken: Promise<any> = appInjector.get(appComponentRefPromiseToken);
313308
var tick = (componentRef) => {
314309
var appChangeDetector = internalView(componentRef.hostView).changeDetector;
@@ -327,10 +322,17 @@ export function commonBootstrap(
327322
PromiseWrapper.then(tickResult, null,
328323
(err, stackTrace) => { bootstrapProcess.reject(err, stackTrace); });
329324
} catch (e) {
325+
if (isPresent(exceptionHandler)) {
326+
exceptionHandler.call(e, e.stack);
327+
} else {
328+
// The error happened during the creation of an injector, most likely because of a bug in
329+
// DI.
330+
// We cannot use the provided exception handler, so we default to writing to the DOM.
331+
DOM.logError(e);
332+
}
330333
bootstrapProcess.reject(e, e.stack);
331334
}
332335
});
333-
334336
return bootstrapProcess.promise;
335337
}
336338

modules/angular2/src/web-workers/ui/impl.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ import {
2929
import {createNgZone} from 'angular2/src/core/application_common';
3030
import {WorkerElementRef} from 'angular2/src/web-workers/shared/api';
3131
import {AnchorBasedAppRootUrl} from 'angular2/src/services/anchor_based_app_root_url';
32-
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
3332
import {Injectable} from 'angular2/di';
3433
import {BrowserDomAdapter} from 'angular2/src/dom/browser_adapter';
35-
import {DOM} from 'angular2/src/dom/dom_adapter';
3634
import {
3735
serializeMouseEvent,
3836
serializeKeyboardEvent,
@@ -46,7 +44,7 @@ import {
4644
*/
4745
export function bootstrapUICommon(bus: MessageBus) {
4846
BrowserDomAdapter.makeCurrent();
49-
var zone = createNgZone(new ExceptionHandler(DOM));
47+
var zone = createNgZone();
5048
zone.run(() => {
5149
var injector = createInjector(zone);
5250
var webWorkerMain = injector.get(WebWorkerMain);

modules/angular2/src/web-workers/worker/application_common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function bootstrapWebworkerCommon(
139139
componentInjectableBindings: List<Type | Binding | List<any>> = null): Promise<ApplicationRef> {
140140
var bootstrapProcess: PromiseCompleter<any> = PromiseWrapper.completer();
141141

142-
var zone = createNgZone(new ExceptionHandler(new PrintLogger()));
142+
var zone = createNgZone();
143143
zone.run(() => {
144144
// TODO(rado): prepopulate template cache, so applications with only
145145
// index.html and main.js are possible.

modules/angular2/test/core/application_spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,16 @@ export function main() {
9292

9393
it('should throw if bootstrapped Directive is not a Component',
9494
inject([AsyncTestCompleter], (async) => {
95-
var refPromise = bootstrap(HelloRootDirectiveIsNotCmp, [testBindings]);
95+
var logger = new _ArrayLogger();
96+
var exceptionHandler = new ExceptionHandler(logger, false);
97+
var refPromise =
98+
bootstrap(HelloRootDirectiveIsNotCmp,
99+
[testBindings, bind(ExceptionHandler).toValue(exceptionHandler)]);
96100

97101
PromiseWrapper.then(refPromise, null, (exception) => {
98102
expect(exception).toContainError(
99103
`Could not load '${stringify(HelloRootDirectiveIsNotCmp)}' because it is not a component.`);
104+
expect(logger.res.join("")).toContain("Could not load");
100105
async.done();
101106
return null;
102107
});

0 commit comments

Comments
 (0)
X Tutup