I'm implementing an angular interceptor service which looks for error and shows a snackbar, now I'm trying to add functionality to handle "No internet" or "Slow Internet". I tried with timeout operator but after the timeout, the HTTP call is canceled but my requirement is to notify the user that their internet is slow and then maybe cancel after some time, so I need the timeout operator to trigger a function rather than triggering an error
This is my code
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.user.getToken()}`
}
});
return next.handle(request).timeout(8000)
catchError((error) => {
console.log(error)
console.log(error.name)
if(error.name == 'TimeoutError'){
this.alert.snack('Your Internet seems to be very slow','OK',4000)
this.alert.showSlowInternet()
}
else if(error.error.detail === "Given token not valid for any token type"){
this.global.refresh(this.router.url)
}
else if(error.error.detail === "No Connectivity"){
this.alert.dismissableSnack('No Connectivity detected','DISMISS',)
}
else if(error.error.detail){
this.alert.snack(error.error.detail,'OK')
}
else{
this.alert.snack('Server not responding','OK')
}
return throwError(error);
})
}
operators(timeout, catchError) should be wrapped inpipestackblitz.com/edit/…