I cant catch HTTP 401 error code:
If the user credentials are incorrect the server returns 401, this is not handled by Ionic 2 on ios and I have not found any solution for how to solve this. I am unable to identify if the user has typed in invalid credentials with the call:
this.mobileAppService.fetchGeneralAppSettings(this.login).subscribe(generalAppSettings => {
...
}, err => { console.log("Got Error--> " + err); },
() => console.log("Finished ? ") );
Bellow are some code for the rest call:
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do'; // debug
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { GeneralAppSettings } from '../models/general-app-settings';
import { UserData } from '../providers/user-data';
import { LastLogin } from '../models/last-login';
@Injectable()
export class MobileAppService {
constructor(public http: Http, public userData: UserData) { }
createAuthorizationHeader(headers: Headers, lastLogin: LastLogin) {
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Basic ' + btoa(lastLogin.username + ':' + lastLogin.password));
}
fetchGeneralAppSettings(lastLogin: LastLogin): Observable<GeneralAppSettings> {
let headers = new Headers();
this.createAuthorizationHeader(headers, lastLogin);
return this.http.get(
"https://myUrl/secure/json/mobileAppService/fetchGeneralAppSettings?versionCode=48"
, { headers: headers }
).map((res:Response) => <GeneralAppSettings>res.json())
.do((res:Response) => console.log(" Received Data:', JSON.stringify(res))) // debug
.catch(this._serverError);
}
_serverError(err: any) {
console.log('sever error:', err);
if(err instanceof Response) {
return Observable.throw(err.json().error || 'backend server error');
}
return Observable.throw(err || 'backend server error');
}
...