【发布时间】:2018-07-24 07:11:20
【问题描述】:
我有一个带有这样的 globalError 处理程序的应用程序:
import { Injectable, ErrorHandler, Injector } from "@angular/core";
import { Router } from "@angular/router";
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
constructor(
private injector: Injector) { }
public handleError(error: any) {
console.error("Something went wrong");
//.. Handle error here
}
}
这始终适用于每个星座。如果抛出错误,全局处理程序会捕获并处理它。
现在升级到 RxJs 6.2.2 后,我了解到捕获 http 错误发生了变化。
代码错误仍然有效,但 HttpClient 抛出的错误不会被全局捕获。 GlobalErrorHandler 不再被触发。
我知道我可以处理我的服务中的错误,并且可以像这样正常工作:
doSomething() {
return this.http
.get("http://someURL").pipe(
map((res: any) => { console.log(res) }),
catchError(this.handleError<any>(`blabla`))
);
}
/**
* Handle Http operation that failed.
* Let the app continue.
* @param operation - name of the operation that failed
* @param result - optional value to return as the observable result
*/
private handleError<T>(operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
console.log("Now throwing error");
// TODO: send the error to remote logging infrastructure
console.error(error); // log to console instead
// Let the app keep running by returning an empty result.
// ToDo: Global Error handler hier aufrufen....
return of(result as T);
};
}
但我实际上想集中处理所有错误。
Angular 6 和 RxJs 6 集中处理错误的正确方法是什么?
【问题讨论】:
-
这可能是一个 RxJS 错误 github.com/ReactiveX/rxjs/issues/2583
-
试试下面的链接也许你会得到解决方案,stackoverflow.com/questions/50492977/…
标签: angular rxjs angular6 rxjs6 angular-errorhandler