P
P
Pogran2016-12-07 16:08:44
Angular
Pogran, 2016-12-07 16:08:44

Why doesn't error handling work correctly?

I have this method

editStatusUser(id : number, status : string) :Observable<boolean> {
        return this.http.get('assets/fake/fake-userss.json')
            .map((response: Response) => {
                // Todo: Delete on real data
                return Math.round(Math.random());
            })
            .delay(2000)
            .catch((error: Response) => this.handleError(error));
    }

    private handleError (error: Response | any) {
        // In a real world app, we might use a remote logging infrastructure
        let errMsg: string;
        if (error instanceof Response) {
            console.log(error.json());
            const body = error.json() || '';
            const err = body.error || JSON.stringify(body);
            errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
        } else {
            errMsg = error.message ? error.message : error.toString();
        }
        console.error(errMsg);
        return Observable.throw(errMsg);
    }

It swears at error.json() , writes SyntaxError: Unexpected token C in JSON at position 0(…) . And it’s not clear why, because the Response object is returned, but for some reason it cannot be converted

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question