I am using @angular/http for http calls (Observable) and NativeStorage library for storage mechanism which is Promise. That's why I use FromPromise to convert Promise funtion "NativeStorage.getItem("xxx")" to Observable.
I am even not sure if this is a good practice and the chain is broken at the line "console.log("HIT SUCCESSFULLY");" and stops executing the code.
Since there is no item called "externalAccessToken" in the storage, it is normal to catch the exception null in Promise but I don't understand why it stops executing after that.
Till now, I have tried to return something else other than null and using "Promise.reject()" which caused "Unhandled Promise rejection" error.
How can I keep the the code executing and hit the catch function of Observable
public getExternalAccessTokenFromStorage(): Observable<any> {
let externalAccessTokenPromise = NativeStorage.getItem('externalAccessToken');
let getExternalAccessTokenFromStorage: Observable<any> = Observable.fromPromise(externalAccessTokenPromise.then(x => x)
.catch(() => {
console.log("HIT SUCCESSFULLY");
return null
}));
return getExternalAccessTokenFromStorage.map(x => {
console.log("NOT HIT AT ALL");
return x;
}).catch(() => {
console.log("NOT HIT AT ALL");
return null;
});
}
public getUserInfo(): Observable<StoredUserModel> {
//Get External Access Token From LocalStorage
return this.getExternalAccessTokenFromStorage().flatMap((x: IExternalAccessTokenBindingModel) => {
return this.getAccessTokenFromStorage().flatMap((accessToken: AccessTokenModel) => {
console.log("NOT HIT AT ALL");
let headers = new Headers();
headers.append("Authorization", "Bearer " + accessToken.access_token);
headers.append("Content-Type", "application/json");
let options = new RequestOptions({ headers: headers });
var externalBindingModel = JSON.stringify(x);
return this.http.post(this.baseUrl + '/api/Account/ExternalUserInfo', externalBindingModel, options).map((res: Response) => {
//ADD USER INTO NATIVESTORAGE
this.addUserIntoStorage(res.json());
return res.json();
});
});
}).catch(x => {
return this.getAccessTokenFromStorage().flatMap((accessToken: AccessTokenModel) => {
console.log("NOT HIT AT ALL");
let headers = new Headers();
headers.append("Authorization", "Bearer " + accessToken.access_token);
let options = new RequestOptions({ headers: headers });
return this.http.get(this.baseUrl + '/api/Account/UserInfo', options).map((res: Response) => {
//ADD USER INTO NATIVESTORAGE
let user: StoredUserModel = res.json();
this.addUserIntoStorage(res.json());
return user;
});
}).catch(error => {
return null;
});
});
}
UPDATED QUESTION:
I have removed Promise.catch and keep Observable.catch in order to catch unhandled exception in Observable;
public getExternalAccessTokenFromStorage(): Observable<any> {
let externalAccessTokenPromise = NativeStorage.getItem('externalAccessToken');
let getExternalAccessTokenFromStorage: Observable<any> = Observable.fromPromise(externalAccessTokenPromise);
return getExternalAccessTokenFromStorage.map(x => {
return x;
}).catch(() => {
return null;
});
}
And I get the following error;
Observable.catch
instead ofPromise.catch
, i.e. move the error handling out of thefromPromise
? – jonrsharpecatch
callback - learnrxjs.io/operators/error_handling/catch.html – jonrsharpe