如果Angular 2遇到内部异常,则不会将其记录到控制台。
如何检测以下异常?
EXCEPTION: Error during instantiation of MainFormComponent!.
ORIGINAL EXCEPTION: TypeError: Cannot set property 'crashMeMaybe' of undefined
ORIGINAL STACKTRACE:
... stacktrace ...
ERROR CONTEXT:
... context object ...
是否有可用的订阅?这样的文件在哪里?
答案 0 :(得分:8)
有ExceptionHandler接口,您可以通过它提供您选择的自定义实现。请参阅here和documentation。
来自BETA.0来源:(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})])
*
* ```
*/
<强>更新:强>
我对BETA.0有一点问题
我的运行方式如下:
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
})
]);