Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 (RxJs 6.x) - How to use throwError

Tags:

angular

rxjs6

I want use second param (error) on my subcriber but dot not work.

Code of my Observable:

return Observable.create(obs => {
  cognitoidentityserviceprovider.adminCreateUser(params, function(error, data) {
    if (error) {
      console.log(error);
      return throwError(error || 'Server error');
    } else {
      console.log(data);
      return obs.next(data.User);
    }
  });
});

my console.log(error); is OK but after nothing (no trace).

Code of my subscriber:

this.myService.createUser(user).subscribe(
         result => this.getUsers(),
         error =>  this.errorUsersProcessor(error));

second param (error) on my subcriber never call.

like image 576
Stéphane GRILLON Avatar asked Dec 07 '25 05:12

Stéphane GRILLON


1 Answers

From the documentation : throwError

Creates an Observable that emits no items to the Observer and immediately emits > an error notification.

So my guess here is that the throwError is emiting an Error to a new observer not the one you are subscribing to.

You could try to notify you're actual observer to emit the error doing :

obs.error(error || new Error('Server error'));
like image 161
Felix Movee Avatar answered Dec 08 '25 17:12

Felix Movee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!