From ea89617880353b6a7456572a041b23c94ab5fd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Oko=C5=84ski?= Date: Mon, 13 Apr 2020 17:10:36 +0200 Subject: [PATCH] docs(docs-infra): fix handling of client-side errors in networking (#36608) Previously example for handling client-side errors in networking didn't work when there was no Internet connection. Caught error is ProgressEvent in such case, not ErrorEvent and error.error.message is undefined. PR Close #36608 --- aio/content/examples/http/src/app/config/config.service.ts | 4 ++-- aio/content/guide/http.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/aio/content/examples/http/src/app/config/config.service.ts b/aio/content/examples/http/src/app/config/config.service.ts index 7d81ef7281..0c24de5078 100644 --- a/aio/content/examples/http/src/app/config/config.service.ts +++ b/aio/content/examples/http/src/app/config/config.service.ts @@ -72,9 +72,9 @@ export class ConfigService { // #docregion handleError private handleError(error: HttpErrorResponse) { - if (error.error instanceof ErrorEvent) { + if (error.status === 0) { // A client-side or network error occurred. Handle it accordingly. - console.error('An error occurred:', error.error.message); + console.error('An error occurred:', error.error); } else { // The backend returned an unsuccessful response code. // The response body may contain clues as to what went wrong. diff --git a/aio/content/guide/http.md b/aio/content/guide/http.md index 70bd85f10f..1022149b6f 100644 --- a/aio/content/guide/http.md +++ b/aio/content/guide/http.md @@ -327,7 +327,7 @@ Two types of errors can occur. * The server backend might reject the request, returning an HTTP response with a status code such as 404 or 500. These are error _responses_. -* Something could go wrong on the client-side such as a network error that prevents the request from completing successfully or an exception thrown in an RxJS operator. These errors produce JavaScript `ErrorEvent` objects. +* Something could go wrong on the client-side such as a network error that prevents the request from completing successfully or an exception thrown in an RxJS operator. These errors have `status` set to `0` and the `error` property contains a `ProgressEvent` object, whose `type` might provide further information. `HttpClient` captures both kinds of errors in its `HttpErrorResponse`. You can inspect that response to identify the error's cause.