mnv mnv - 1 year ago 206
HTTP Question

How to catch exception correctly from http.request()?

Part of my code:

import {Injectable} from 'angular2/core';
import {Http, Headers, Request, Response} from 'angular2/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';

export class myClass {

constructor(protected http: Http) {}

public myMethod() {
let request = new Request({
method: "GET",
url: "http://my_url"

return this.http.request(request)
.map(res => res.json())
.catch(this.handleError); // Trouble line.
// Without this line code works perfectly.

public handleError(error: Response) {
return Observable.throw(error.json().error || 'Server error');


produces exception in console of browser:

ORIGINAL EXCEPTION: TypeError: this.http.request(...).map(...).catch is not a function

Answer Source

Perhaps you can try adding this in your imports:

import 'rxjs/add/operator/catch';

You can also do:

return this.http.request(request)
  .map(res => res.json())
    data => console.log(data),
    err => console.log(err),
    () => console.log('yay')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download