1

I have an observable function, that does request:

public send(): Observable<ISms[]> {

    if (this.usedAttempts < this.maxAttempts) {
      return; // Here return custom error
    }
}

I take this observable like as:

this.sms.send().subscribe(
      response => {
        console.log(response);
      },
      error => {
        console.log(error);
      }
    );
  }

I need to check if user can send Sms if yes, send, otherwise return error.

2 Answers 2

3

Just use throw() operator:

if (this.usedAttempts < this.maxAttempts) {
    return Observable.throw('error description');
}

Don't forget to import it:

import 'rxjs/add/observable/throw';

Here is what the docs say:

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is it correct way, or better created separated method that check?
@Daniel, that's subjective, you probably could create a separate method which would be more expressive, like if (this.sms.canSend()) { this.sms.send() }. But if the server also performs some validation and may return the same error, then I would leave the check inside the send method
2
public send(): Observable<ISms[]> {

    if (this.usedAttempts < this.maxAttempts) {
      return Observable.throw(customError)
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.