title: Intercept http and add headers - IONIC
body: <p>I'm running a Ionic 4 app which contains an http interceptor to add some loading between the requests.</p>
  <p>I also have a credential service which validates whether there is a token in my storage or not.</p>
  <p>this is my interceptor</p>
  <pre><code>intercept(req: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {
          this.requests.push(req);
          this.loaderService.isLoading.next(true);
          return new Observable(observer =&gt; {
              const subscription = next.handle(req)
                  .subscribe(
                      event =&gt; {
                          if (event instanceof HttpResponse) {
                              this.removeRequest(req);
                              observer.next(event);
                          }
                      },
                      (err: Error ) =&gt; {
                          this.alertservice.info(err.message, 'id1');
                          this.removeRequest(req);
                          observer.error(err);
                      },
                      () =&gt; {
                          this.removeRequest(req);
                          observer.complete();
                      });
              // remove request from queue when cancelled
              return () =&gt; {
                  this.removeRequest(req);
                  subscription.unsubscribe();
              };
          });
      }
  </code></pre>
  <p>It works, but when I'm trying to add the following lines, it stops working and redirects to my error page and consoles nothing, I can't track it.</p>
  <pre><code>    intercept(req: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {
          //console.log(this.credentialService.credentials.token);
          // console.log(authToken);
  </code></pre>
  <p>Can anyone help me understand why does it happen, and how to fix?</p>