How to throw an observable error manually?

后端 未结 8 1546
慢半拍i
慢半拍i 2020-12-02 16:35

I am working on an Angular app in which I am making a rest call through HTTP as below:

login(email, password) {
    let headers = new Headers();
    headers.a         


        
8条回答
  •  没有蜡笔的小新
    2020-12-02 17:05

    Use the catch operator

    this.calcSub = this.http.post(this.constants.userUrl + "UpdateCalculation", body, { headers: headers })
       .map((response: Response) => {
          var result = response.json();
             return result;
       })
       .catch(this.handleError)
       .subscribe(
          dro => this.dro = dro,
          () => this.completeAddCalculation()
       );
    

    And handle the error like this:

    private handleError(error: Response) {
        console.error(error); // log to console instead
        return Observable.throw(error.json().error || 'Server Error');
    }
    

提交回复
热议问题