HttpInterceptor don't intercept call


#1

This is my situation:

app.module.ts

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { CoreModule } from './modules/core/core.module';
import { AppRoutingModule } from './app.routing';
import { AppComponent } from "./app.component";

@NgModule({
    bootstrap: [
        AppComponent
    ],
    imports: [
        CoreModule,
        AppRoutingModule
    ],
    declarations: [
        AppComponent
    ]
})

export class AppModule { }

core.module.ts

// NativeScript
import { NativeScriptModule } from 'nativescript-angular/nativescript.module';

// Angular
import { NgModule, Optional, SkipSelf } from '@angular/core';

// app
import { PROVIDERS } from './services';

const MODULES: any[] = [
  NativeScriptModule
];

@NgModule({
  imports: [
    ...MODULES
  ],
  providers: [
    ...PROVIDERS
  ],
  exports: [
    ...MODULES
  ]
})
export class CoreModule {
  constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
    if(parentModule) {
      throw new Error(
        'Use AppModule'
      )
    }
  }
}

services/index.ts

...
import { HttpErrorsService } from './http_errors.service';

export const PROVIDERS: any[] = [
  ...,
  HttpErrorsService
]

...
export * from './http_errors.service';

http_errors.service.ts

import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpErrorResponse, HTTP_INTERCEPTORS, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { _throw } from 'rxjs/observable/throw';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';

import { RouterExtensions } from "nativescript-angular/router";

import { LogService } from './log.service';

@Injectable()
export class HttpErrorsInterceptor implements HttpInterceptor {

  constructor(
    private router: RouterExtensions,
    private logService: LogService
  ) { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req)
      .do(evt => {
        if (evt instanceof HttpResponse) {
          console.log('---> status:', evt.status);
        }
      })
      .catch(errorReponse => {
        let errMsg: string;
        if (errorReponse instanceof HttpErrorResponse) {
          const err = errorReponse.message || JSON.stringify(errorReponse.error);
          errMsg = `${errorReponse.status} - ${errorReponse.statusText || ''} Details: ${err}`;
          if (+errorReponse.status == 401) {
            this.logService.error("401");
          } else if (+errorReponse.status == 404) {
            this.logService.error("404");
          } else {
            this.logService.error(errMsg);
          }

        } else {
          errMsg = errorReponse.message ? errorReponse.message : errorReponse.toString();
          this.logService.error(errMsg);
        }
        return _throw(errMsg);
      });
  }
}

export const HttpErrorsService = {
  provide: HTTP_INTERCEPTORS,
  useClass: HttpErrorsInterceptor,
  multi: true,
};

testservice.ts

import { HttpClient, HttpHeaders, HttpParams, HttpResponse } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from 'rxjs/Observable';
//import "rxjs/add/operator/do";

import { AuthService, BackendService } from "../../../modules/core/services";
import { ITest } from "../../shared/models/testmodel";

@Injectable()
export class TestService {
  constructor(
    private _http: HttpClient,
    private _authService: AuthService
  ) { }

  load(): Observable<ITest> {
    let headers = BackendService.defaultHeaders
      .append("Authorization", "Bearer " + this._authService.token);

    return this._http.get<ITest>(BackendService.apiUrl + '/test', { headers: headers });
  }
}

The call is not intercepted but no errors in console.
Where am I doing wrong?


#2

Doest nativescript interceptor works for you?