I want to detect connection errors to the server, so that I can display an appropriate message to the user.
When the back-end server (a Web API) is off, and I'm trying to access a method on this server by using http.get I expect to get a response with a 404 status, or some sort of a connection error. instead I'm getting
"Response with status: 0 for URL: null"
which is not clear. So how can I detect connection error instead?
This is the relevant code:
Login Component:
this.loginService.tryLogin(userName, password).subscribe(_loginStatus => {
//Some operations
},
_err => this.errMsg = _err; // this.errMsg will be displayed to the user
);
Login Service
tryLogin(user: string, pwd: string): Observable<LoginStatus> {
return this.http.get(this.serverUri + `Login/?username=${user}&password=${pwd}`)
.map(response => response.json())
.catch(err => this.handleError(err));
}
private handleError(error: any) {
const err = (error.json() ? error.json() : error);
console.log(err);
return Observable.throw(err);
}