0

i try to make an http.get-Request with angluar/http in Ionic 3. The request is correct, but I'm not able to catch the error.

My methods look like this:

finalize() {
  this.saveConfig().then(() => {
    loader.dismiss();
    loader2.present();
    this.checkApiServer().then((data) => {
      console.log(data);
      loader2.dismiss();
    }).catch(err => {
      console.log('error', err);
      loader2.dismiss();
    });
  });

}

checkApiServer() {
  this.http.get(this.configuration.server + 'ping').subscribe(data => {
    if (data.status == 200) {
      resolve('server available');
    } else {
      reject(Error('server unan'));
    }
  });

}

If I'm hitting a correct endpoint everthing works, but if the server is not available, i can not catch the error.

Thank you so much for your help

Greeetings

EDIT:

this is the completed method now:

finalize() {
  this.saveConfig().then(() => {
    loader.dismiss();
    loader2.present();

    this.checkApiServer().then((data) => {
      console.log(data);
      loader2.dismiss();
    }).catch(err => {
      console.log('error', err);
      loader2.dismiss();
      this.slides.lockSwipes(false);
      this.slides.slideTo(1);
      this.slides.lockSwipes(true);
      this.error = true;
      this.errorMessage = 'Der Api-Server konnte nicht erreicht werden. Stellen Sie sicher, dass Sie mit einem WLAN verbunden sind und er korrekte API-Server eingestellt ist.'

    });
  });
}
}

checkApiServer() {
  return new Promise((resolve, reject) => {
    this.http.get(this.configuration.server + 'ping', {
      timeout: 3000
    }).subscribe(
      (data) => {
        if (data.status == 200) {
          resolve('server available');
        } else {
          reject(Error('server error'))
        }
      },
      (err) => {
        reject(Error('server unreachable'));
      }
    )
  })
}

THANKS!

3
  • What error, from saveConfig or from checkApiServer? Commented Nov 7, 2017 at 11:38
  • there is a syntax error in your example btw Commented Nov 7, 2017 at 11:50
  • from checkApiServer. Thanks, but @TimothyGroote solved my problem. Thanks! Commented Nov 7, 2017 at 12:14

2 Answers 2

1

i'm assuming you're hitting a timeout. you can pass an error handler along with the subscribe as an arrow function like this :

//timeout is an optional setting optional here
this.http.get(this.configuration.server + 'ping', {headers: headers, timeout: 1000}).subscribe(
    //we only hit this function when the subscribable returns an actual result (in this case, when the server answers)
    (data) => {
      if (data.status == 200) {
        resolve('server available');
      } else {
        reject(Error('server error'));
      }
    },
    //this is the default angular way of handling errors when using a subscribable
    //so it should work for Ionic as well.
    //we will only hit it if something goes wrong while waiting for our subscribable to finish
    (err) => {
      reject(Error('server unreachable?'));
    }
  });
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much. the ``` (err) =>{} ``` was the key.
0

He, you can use to the new HttpClient module of Angular 4.3, is too easy manage HTTP request.

 this.http.get(this.configuration.server, {headers: headers}).subscribe(

        (data) => {
           // Here the code to execute when status code is 200
        },

        (err) => {
          // Here when error
        }, 
() => {
        // and here the code to execute finally, (when http request finish)      
    }
      });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.