title: How to throw error from RxJS map operator (angular)
body: <p>I want to throw an error from my observable's <strong>map</strong> operator based on a condition. For instance if correct API data is not received. Please see the following code:</p>
  <pre><code>private userAuthenticate( email: string, password: string ) {
      return this.httpPost(`${this.baseApiUrl}/auth?format=json&amp;provider=login`, {userName: email, password: password})
          .map( res =&gt; { 
              if ( res.bearerToken ) {
                  return this.saveJwt(res.bearerToken); 
              } else {
                  // THIS DOESN'T THROW ERROR --------------------
                  return Observable.throw('Valid token not returned');
              }
          })
          .catch( err =&gt; Observable.throw(this.logError(err) )
          .finally( () =&gt; console.log(&quot;Authentication done.&quot;) );
  }
  </code></pre>
  <p>Basically as you can see in the code, if the response (<code>res</code> object)  doesn't have <code>bearerToken</code> I want to throw out an error. So that in my subscription it goes into the 2nd parameter (<code>handleError</code>) mentioned below.</p>
  <pre><code>.subscribe(success, handleError)
  </code></pre>
  <p>Any suggestions?</p>