[英]Angular - ErrorHandler for interceptor for custom HTTP error handling
我對使用偵聽ErrorHandler來處理Angular 6+中HTTP請求的自定義錯誤和客戶端錯誤的方法進行了澄清。
正確調用客戶端錯誤。 但是對於HTTP錯誤,當添加了HTTP請求訂閱者的錯誤處理程序時,不會調用自定義錯誤處理程序(請參見以下代碼)。 從訂戶中刪除錯誤處理程序時,將同時調用自定義錯誤處理程序。 那是預期的行為。 在Angular文檔中找不到與此文檔相關的任何文檔。
.subscribe(
success => {
this.processGetChart(success);
},
error => {
this.errors = error;
console.log('API Error: ', error);
},
() => {
}
);
謝謝,
彼得
你可以有一個HttpInterceptor
在內部,您會捕獲不同類型的錯誤。
像這個 :
@Injectable()
export class customInterceptor implements HttpInterceptor {
constructor() {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap((event: HttpEvent<any>) => {
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 403 || err.status === 401) {
// DO SOMETHING HERE.
}
}
}
)
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.