I\'m trying to add some error handling to a service, following the Angular guide.
Relevant snippet:
private handleError (error: Response | any) {
// I
No, it won't work because you wrote:
const body = error.json() || '';
Which means that body
can be an empty string, and a string doesn't have the error
property.
This should be better:
const body = error.json() || { error: null };
Oh, error.json()
returns a Promise
, which means that you won't be able to use this synchronous block, instead you'll need to:
error.json().then(body => {
if (!body) {
body = "";
}
const err = body.error || JSON.stringify(body);
const errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
});