Best practice for handling error in Angular2 : <p>Hi  i am trying to receive error sent from catch block (service).
in multiple components i need to show a popup with the error message displayed.
please let me know how to create a generic method and call it inside service block.  Like what i am doing now with "showErrorPage()".</p>

<pre><code>import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map'

@Injectable()
export class DataService {
    private reqData = {};
    private url: string;
    constructor(private http: Http) {
    }

    getResult(searchObject: {}): Observable&lt;Response&gt; {
        // some logic
        return this.http.post(&lt;my url&gt;, &lt;data to be sent&gt;)

        .map((response: Response) =&gt; {
            return response;
        })
        .catch((error: any) =&gt; {
            if (error.status === 302 || error.status === "302" ) {
                // do some thing
            }
            else {
              return Observable.throw(new Error(error.status));
            }
        });
    }
}
</code></pre>

<p>And in my component i am calling it like</p>

<pre><code>import { Component,EventEmitter, Output, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
// importing DataService ';

@Component({
  selector: 'o-result',
  templateUrl: './o-result.component.html',
})

export class AComp implements OnInit {
    constructor(
        private dataService: DataService
    ){

    }

    ngOnInit() {
      this.dataService.getResult(&lt;url&gt;, &lt;params&gt;)
      .subscribe(
         response =&gt; {
             // doing logic with responce
         }
          ,
          error =&gt; {
            this.showErrorPage();
         }
      )

    }

    showErrorPage(): void {
      // displaying error in popup
    }
}
</code></pre>
