jslearner07
jslearner07

Reputation: 471

Observable error message- Unable to catch in subscribe

I am using RXJS observerable with Angular 4

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';

My function is as follows

public temp(){
         return Observable.create(observer => {

            this.platform.ready().then(() => {
                this.sqlite.create({
                    name: 'offline.db',
                    location: 'default'
                }).then((db: SQLiteObject) => {
                    db.executeSql("select * from TEMP_INSTANCE WHERE CRE_BY=? AND AUD_NUMBER=? ", [localStorage.getItem("user_name"), localStorage.getItem("audNo")])                                               auditNumber]).then(
                        a => {      
                            if (a && a.rows && a.rows.length > 0) {
                                this._util.logData('instance already downloaded.' +  localStorage.getItem("audNo"));
                                return Observable.throw("Instance already downloaded for offline use");

                                } else {
                                         observer.next(true);
                                         observer.complete();

                                }
                        });

                });
            }); 
    }

This function is subscribed in my service as follows

this._dbService.temp().subscribe(a =>{
                                    if(a){
                                        alert('Yet to download');

                                    }
                                },
                        error => {
                           alert('Error'+e);
                        });

For some reason I am unable to get errors. Can anyone please advise hw to throw error? Pls help.

Upvotes: 3

Views: 955

Answers (1)

Olaf Horstmann
Olaf Horstmann

Reputation: 16892

Instead of return Observable.throw("..."); you have to use:

observer.error("Instance already downloaded for offline use");

Upvotes: 1

Related Questions