Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throw error inside rxjs Observable

Tags:

I'm trying to throw an error inside a rxjs Observable

 new Observable(subscriber => {    Observable.throw("error1");    return Observable.throw("error2");  })  .subscribe(    () => {},    err => console.error(err)  ); 

error 1 is not caught.

error2 gives a compilation error:

Argument of type '(this: Observable<{}>, subscriber: Subscriber<{}>) => ErrorObservable<string>' is not assignable to parameter of type '(this: Observable<{}>, subscriber: Subscriber<{}>) => TeardownLogic'. Type 'ErrorObservable<string>' is not assignable to type 'TeardownLogic' 

what is the proper way to throw an error inside an observable?

like image 721
phzonta Avatar asked Feb 15 '17 11:02

phzonta


1 Answers

Use Subscriber.error:

new Rx.Observable(subscriber => {    subscriber.error("error1");    subscriber.error("error2"); // not called because a stream will be finalized after any error  })  .subscribe(    () => {},    err => console.error(err)  );
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>
like image 191
olsn Avatar answered Sep 21 '22 06:09

olsn