I recently started using Angular 2 and quickly discovered that when utilizing the HTTP client that a return code of 404 from the server ends up rejecting the promise.
Here's the code I have in my service:
export class CalculatorService {
private _serviceUrl = 'http://localhost:3000';
private headers = new Headers({'Content-Type': 'application/json'});
constructor(private http: Http) { }
getCode(request: CalculatorRequest) {
return this.http.post(this._serviceUrl, JSON.stringify(request), {headers: this.headers})
.toPromise()
.then(response => response.json() as CalculatorResponse)
.catch(this.handleError);
}
private handleError(error): Promise<any> {
const errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server Error : Service Unavailable';
if (errMsg != null) {
return Promise.reject(errMsg);
}
}
}
Unfortunately, instead of resolving the promise, my handleError method is called as the 404 causes a rejection.
Is there a straightforward way to resolve a request with a return code of 404?
Thanks!