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!
saveConfigor fromcheckApiServer?checkApiServer. Thanks, but @TimothyGroote solved my problem. Thanks!