Http Subscribe Error does not work on device


#1

Hey there!

I’m making a http request and subscribing to it so I can treat the responses. The problem is, when I run my project in the browser, everything works fine. But when I run on the device, if the response has an error, it is never “caught”.

Below is my code.

VIEW:

userDidClickConnect() {
    if (this.username && this.password) {
      let loader = this.loadingCtrl.create({ content: 'Autenticando...' });
      loader.present();

      this.sfa.register(this.username, this.password).subscribe(
        (data: LoginResponse) => {
          this.loginResponse = data;
          console.log(this.loginResponse.d.ApplicationConnectionId);
          this.sfa.setAppcidValue(this.loginResponse.d.ApplicationConnectionId);
          this.sfa.getPartnerId().subscribe(
            (data: PartnerResponse) => {
              loader.dismiss();
              this.sfa.setPartnerId(data.d.PartnerId);
              this.navCtrl.setRoot('TabControllerPage');
            },
            (err: Response) => {
              loader.dismiss();
              this.error = err;
              let toast = this.toastCtrl.create({
                message: 'Algo deu errado. Tente novamente mais tarde.',
                duration: 3000
              });
              toast.present();
            }
          )
        },
        (err: Response) => {
          loader.dismiss();
          this.error = err;
          let toast = this.toastCtrl.create({
            message: 'Usuário e/ou senha incorretos',
            duration: 3000
          });
          toast.present();
        });
    } else {
      let toast = this.toastCtrl.create({
        message: 'Usuário e/ou senha inválidos.',
        duration: 3000
      });
      toast.present();
    }
  }

SERVICE:

register(username: string, password: string): Observable<LoginResponse> {
        this.username = username;
        this.password = password;
        let authString = 'Basic ' + btoa(`${this.username}:${this.password}`);

        let registrationInfo = {
            'DeviceType': this.device.platform,
            'DeviceModel': this.device.model,
            'UserName': this.device.uuid
        };

        let headers = new Headers();
        headers.append('Authorization', authString);
        headers.append('Content-Type', 'application/json');
        headers.append('Accept', 'application/json');

        let options = new RequestOptions({ headers: headers });

        return this.http.post(this.connectionRoot, registrationInfo, options)
            .do(this.logData)
            .map(this.extractData)
            .do(this.logData)
            .catch(this.handleError);
    }

private extractData(response: Response) {
        return response.json();
    }

    private handleError(error: Response) {
        return Observable.throw(error);
    }

    private logData(response: Response) {
        console.log(response);
    }

So, for example, if I type wrong user and password, it never displays the toast and the loader is never dismissed.

Any idea of what I’m doing wrong?

Thanks in advance and sorry for the bad english. :slight_smile: