30

I created this HTTPInterceptor to be able to better handle http errors, it was working well before I did a git pull and ran npm install.

This is my code:

import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse} from '@angular/common/http';
import {Observable} from "rxjs";
import {ToasterService} from "angular2-toaster";

@Injectable()
export class GobaeInterceptor implements HttpInterceptor {
    constructor(private toasterService: ToasterService){
    }
    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(req)
            .do(event => {
                if (event instanceof HttpResponse) {
                    let response = event.body;
                    if(response.Error){
                        this.toasterService.pop('error', 'Error '+response.Code, response.Message);
                    }
                }
            });
    }
}

And this is the error I get:

TypeError: next.handle(...).do is not a function at GobaeInterceptor.webpackJsonp.../../../../../src/app/services/gobae.interceptor.ts.GobaeInterceptor.intercept (gobae.interceptor.ts:12) at HttpInterceptorHandler.webpackJsonp.../../../common/@angular/common/http.es5.js.HttpInterceptorHandler.handle (

Did something that can affect my code changed lately? what can I do now to "catch" the http response on my interceptor?

1
  • heck if the provider for the interceptor is still there , better just do a compare of the code before and after pull or what files where changed after pull . as i guess the module providers is missing . or even the httpclientmodule Commented Aug 23, 2017 at 18:59

4 Answers 4

53

This error is thrown because you are missing the do operator. The below import with patch the observable object with the do operator.

import 'rxjs/add/operator/do';

RxJs does not come bundled with all the operator functions by default to reduce the library size. You are required to import the operators you would like to use individually.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting this, and I already have that in my interceptor file. Does this then need to be added to any unit test that does an http call? (aka EVERY unit test that does a get/post/put/delete/etc)
This is not valid anymore as of Angular 6. Lettable operators are to be used.
Saved the day.!! <3
26

rxjs 6 / angular 6 will need the pipe

return next.handle(req).pipe(
  tap(event => {
    if (event instanceof HttpResponse) {
      ...
    }
  })
);

Comments

8

You have to use import.

import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import 'rxjs/Observable';
import 'rxjs/add/observable/throw';

2 Comments

Same as for other answer; this is not valid anymore as of Angular 6, because lettable operators are to be used.
:) This question was answered a while ago, sure rxjs has changed a lot since then, but surely you know that @Jago
-1

I ended up changing the operator from this:

next.handle(req).do

To this:

next.handle(req).subscribe

It seems that:

  1. Operators from rxjs are not loaded by default anymore

  2. Since Angular has implemented observables on HTTP calls, subscribe is the right one to go

1 Comment

Subscribe will duplicate request.

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.