可以使用tap操作符来进行错误处理,并将catchError保留在最后一个操作符中,以便处理未处理的错误。以下是代码示例:
import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpResponse } from '@angular/common/http'; import { Observable, throwError } from 'rxjs'; import { catchError, tap } from 'rxjs/operators';
@Injectable() export class MyInterceptor implements HttpInterceptor {
intercept(req: HttpRequest
return next.handle(req).pipe(
tap(event => {
if (event instanceof HttpResponse) {
// do something with the response
}
}),
catchError(error => {
// handle error
return throwError(error);
})
);
} }