捕获Angular 2异常 [英] Catch Angular 2 exceptions

查看:326
本文介绍了捕获Angular 2异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果Angular 2遇到内部异常,则不会将其记录到控制台。

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?

推荐答案

有ExceptionHandler接口,您可以通过该接口提供您选择的自定义实现。请参见此处文档

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

来自BETA.0来源: (facade / exception_handler.d.ts)

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})])
 *
 * ```
 */

已更新:

BETA.0有点问题

I had a little problem with 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
    })
]);

这篇关于捕获Angular 2异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆