Angular cancels http requests very fast and I want to intercept those cancelled requests. Is it possible to capture cancelled requests in the interceptor? Below is a fragment of my interceptor code, where I want to catch cancelled request.
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
this.onStartRequest();
// Pass the cloned request instead of the original request to the next handle
return next.handle(req).do(
(event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do something
}
},
(err: any) => {
if (err instanceof HttpErrorResponse) {
// do something
}
}
);
}
You have to use the catchError
RxJs operator when you use the next
Handler, here's the code:
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
this.onStartRequest();
// Pass the cloned request instead of the original request to the next handle
return next.handle(req).do(
(event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do something
}
})
.catchError((err: HttpErrorResponse) => {
// Handle errors
});
}
PS: Don't forget to import the catchError operator.
Did you try with the finalize event?
return next.handle(req).pipe(
finalize(() => {
// request completes, errors, or is cancelled
})
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.