0

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);
      })
  }
2
  • If you use latest rxjs library, your operators (timeout, catchError) should be wrapped in pipe stackblitz.com/edit/… Commented Jul 10, 2019 at 9:41
  • yeah bt it works even if they are chained right and also that was not my question Commented Jul 11, 2019 at 12:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.