我是Angular的新手。 为什么ErrorHandler没有捕获“第一个”循环中抛出的错误?
错误处理程序类
@Injectable()
export class RavenErrorHandler implements ErrorHandler {
handleError(err: any): void {
console.log('handleError', err);
Raven.captureException(err.originalError || err);
}
}
随机组件内的代码
ngOnInit() {
console.log('init');
// This is not caught for some reason
throw new Error('Test error')
// This is ok
setTimeout(() => {
throw new Error('Test error 3')
}, 1)
// This is catched correctly
return new Promise((resolve, reject) => {
reject(new Error('Test error 2'))
});
}
handleButtonClick() {
// OK
throw new Error('test error 4')
}
app.module.ts
@NgModule({
...,
providers: [{provide: ErrorHandler, useClass: RavenErrorHandler}],
...,
})
export class AppModule {}