0

So I followed the error handling guide in the angular docs. I always seem to receive the error in my service, however, my console always displays my error object as '[object Object]'. Does anyone know what causes this and how I can fix this?

My service file

subViewPlus(section, sign){
        return this.http.get('http://localhost:3000/forum/subViewPlus/' + section + '/' + sign)
            .pipe(catchError(this.handleError)
    }

    private handleError(error: HttpErrorResponse) {
        if (error.error instanceof ErrorEvent) {
            // A client-side or network error occurred. Handle it accordingly.
            console.error('An error occurred:', error.error.message);
        } else {
            // The backend returned an unsuccessful response code.
            // The response body may contain clues as to what went wrong,
            console.error(
                `Backend returned code ${error.status}, ` +
                `body was: ${error.error}`);
        }
        // return an ErrorObservable with a user-facing error message
        return new ErrorObservable(
            'Something bad happened; please try again later.');
    };

Screenshot that shows the logs to the console: how do I transform this [body body] into something more useful?

enter image description here

1 Answer 1

1

dont do interpolation on the object. Instead do something like this.

console.error({
    "Backend returned code: ": error.status,
    "body was: ": error.error
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.