title: Can I catch certain errors before "subscribe()" in an RXJS observable in Angular2?
body: <p>Is it possible for a base class to <code>catch</code> certain errors before allowing the subclass to <code>subscribe</code> to the observable in Angular2.</p>
  
  <p>e.g.</p>
  
  <pre><code>export class SomeBaseClass {
      constructor(private _http: Http, private _location: Location) {}
  
      protected _fetchData(url): Observable&lt;any&gt; {
          const headers = new Headers();
          headers.append('Authorization', 'Token foo');
          return this._http.get(url, {headers})
              .map(response =&gt; response.json())
              .catch(response =&gt; this._handle401(error));
      }
  
      private _handle401(response: Response) {
          if(response.status === 401) {
              this._location.go('/login');
          }
  
          // What should this return?
      }
  }
  
  export class SomeClass extends SomeBaseClass {
      constructor( _http: Http,  _location: Location) {
          super(_http, _location);
      }
  
      doTheThing() {
          this._fetchData('/someUrl')
              .subscribe(
                  response =&gt; this._handleResponse(response),
                  error =&gt; this._handleErrorThatIsNot401(error));
      }
  
      private _handleResponse(response) {
          // ...
      }
  
      private _handleErrorThatIsNot401(error) {
          // ...
      }
  }
  </code></pre>
  
  <p>Is <code>catch</code> what I am looking for? Should I be using <code>map</code> (or something else)? Or am I going about this the wrong way entirely?</p>
  
  <h1>Update</h1>
  
  <p>Both answers (so far) put me on the right track, ultimately - I solved it like this:</p>
  
  <pre><code>protected _get(url: string, data?: any): Observable&lt;any&gt; {
      return super._get(url, data, this._authorizationHeader)
          .map(response =&gt; response.json())
          .catch(response =&gt; this._handle401(response));
  }
  
  private _handle401(response: Response): Observable&lt;any&gt; {
      try {
          if(response.status === 401) {
              this._router.navigateByUrl('/login');      
              return Observable.throw(response.status);  
          }
      } catch(err) {
          console.warn('AuthenticatedHttpService._handle401');
          console.error(err);
      }
  
      return Observable.of(response);
  }
  </code></pre>