我有一个关于新类ErrorHandler(包含在RC.6中)的问题。
我按照官方文档的示例进行了操作: https://angular.io/docs/ts/latest/api/core/index/ErrorHandler-class.html
import{ErrorHandler} from "@angular/core";
import{NgModule} from "@angular/core";
export class MyErrorHandler implements ErrorHandler {
call(error: any, stackTrace: any = null, reason: any = null) {
// do something with the exception
console.log("do something with the exception");
}
// I handle the given error.
public handleError( error: any ): void {
console.log("I handle the given error");
}
}
@NgModule({
providers: [
{
provide: ErrorHandler,
useClass: MyErrorHandler
}
]
})
export class MyErrorModule {}
我编辑了我的app.module文件
import {MyErrorHandler} from "./error.module";
import {MyErrorModule } from "./error.module";
..
@NgModule({
imports: [
MyErrorModule
....
],
...
providers: [MyErrorHandler]
....
现在MyErrorHandler捕获错误:
throw new Error("my test error");
但是它无法捕获像这样的http错误:"GET http://example.com/rest/user 401 (未经授权)"。有人能给我解释一下吗?
提前感谢!
handleHTTPError
方法,以便统一处理这些HTTP错误。 - Kosmonaft