I'm new to angular2 and I do my http requests like this :
this.http.get(...).map(res=>res.json()).subscribe(data=>{
//..do something
},err=>{
if(err.status == 400){
this.presentToast('Validation error');
}else if(err.status == 403){
this.presentToast('Authorization error'):
}else if(err.status == 500){
this.presentToast('Something wrong with server');
}else ...
});
I wanted a custom message for each of the common status codes that users can understand, but the problem is I write these if and else blocks to each and every http request I do, and in every ts file I have, so basically I import ToastController in every ts file and write the presentToast function.
Is there anyway to make a generic error handler that is decorated in a way that it presents the custom rules/messages as a toast and make it DRY?