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