4

If Angular 2 encounters an internal exception, it won't be logged to the console.

How can I detect such exceptions like the following one?

EXCEPTION: Error during instantiation of MainFormComponent!.
ORIGINAL EXCEPTION: TypeError: Cannot set property 'crashMeMaybe' of undefined
ORIGINAL STACKTRACE:
... stacktrace ...
ERROR CONTEXT:
... context object ...

Are there any available subscriptions? Where is such documentation?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Gábor Imre
  • 5,899
  • 2
  • 35
  • 48
  • Have you tried with [try...catch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch)? – Eric Martinez Aug 11 '15 at 19:04
  • I want a general solution to handle errors like window.onerror or console.error, or node's process.on('uncaughtException'), for the errors that are not, or maybe cannot be caught. Now Angular2 just "swallows" such exceptions but I want to detect them somehow. – Gábor Imre Aug 11 '15 at 19:09
  • 1
    And what about [catch()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch) from Promises? I personally use `System.import("app").catch(console.log.bind(console));` where [import()](https://github.com/systemjs/systemjs/blob/master/docs/system-api.md) returns a Promise. – Eric Martinez Aug 11 '15 at 19:18
  • Good concept, I might use it, but still not solves the original problem: detecting if something inside the belly of Angular2 breaks. – Gábor Imre Aug 11 '15 at 20:33
  • 1
    The default [error handler](https://github.com/angular/angular/blob/master/modules/angular2/src/core/exception_handler.ts) uses `console.log`. However, there seems to be an [issue](https://github.com/angular/angular/issues/1489) with promises in which is cannot be fixed by the angular2 codebase. – Jesse Good Aug 11 '15 at 23:09

1 Answers1

8

There is interface ExceptionHandler through which you can provide custom implementation of your choosing. See here and documentation.

From BETA.0 sources: (facade/exception_handler.d.ts)

/**
 * Provides a hook for centralized exception handling.
 *
 * The default implementation of `ExceptionHandler` prints error messages to the `Console`. To
 * intercept error handling,
 * write a custom exception handler that replaces this default as appropriate for your app.
 *
 * ### Example
 *
 * ```javascript
 *
 * class MyExceptionHandler implements ExceptionHandler {
 *   call(error, stackTrace = null, reason = null) {
 *     // do something with the exception
 *   }
 * }
 *
 * bootstrap(MyApp, [provide(ExceptionHandler, {useClass:    MyExceptionHandler})])
 *
 * ```
 */

UPDATED:

I had a little problem with BETA.0

  • the import had to be hacked,
  • the interface seems to be polluted with internal details.

I've run it like:

import {ExceptionHandler} from 'angular2/src/facade/exception_handler';

export interface IExceptionHandler {
    call(exception: any, stackTrace?: any, reason?: string): void;
}

export class CustomExceptionHandler implements IExceptionHandler {
    call(exception: any, stackTrace: any, reason: string): void {
        alert(exception);
    }
}

bootstrap(DemoApp, [
    new Provider(ExceptionHandler, {
        useClass: CustomExceptionHandler
    })
]);
Community
  • 1
  • 1
wendro
  • 254
  • 3
  • 6