Angular HttpClient error handling difficult

后端 未结 2 1982
执念已碎
执念已碎 2021-01-07 10:12

The Angular documentation on the new HttpClient https://angular.io/guide/http has a section \"Getting error details\" where they show an example like below. I have modified

2条回答
  •  既然无缘
    2021-01-07 10:31

    This might help you

    private handleError(err: HttpErrorResponse) {
        let errorMessage = '';
        if (err.error instanceof Error) {
            // A client-side or network error occurred. 
            errorMessage = `An error occurred: ${err.error.message}`;
        } else {
            // The backend returned an unsuccessful response code.
            // The response body may contain clues as to what went wrong,
            errorMessage = `Server returned code: ${err.status}, error message is: ${err.message}`;
        }
        console.error(errorMessage);
        return Observable.throw(errorMessage);
    }
    

提交回复
热议问题