Skip to content

docs: fix network error description in the HTTP testing guide - #71964

Open
MeAkib wants to merge 1 commit into
angular:mainfrom
MeAkib:docs/http-testing-network-error
Open

MeAkib wants to merge 1 commit into
angular:mainfrom
MeAkib:docs/http-testing-network-error

Conversation

@MeAkib

@MeAkib MeAkib commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

With the default fetch backend, network errors are reported as an HttpErrorResponse with status 0 whose error is the error thrown by fetch, not a ProgressEvent.

With the default fetch backend, network errors are reported as an
`HttpErrorResponse` with status 0 whose `error` is the error thrown by
`fetch`, not a `ProgressEvent`.
@angular-robot angular-robot Bot added the area: docs Related to the documentation label Nov 11, 2026
@pullapprove
pullapprove Bot requested a review from atscott November 11, 2026 17:51
@ngbot ngbot Bot added this to the Backlog milestone Nov 11, 2026
Comment on lines +142 to 148
Requests can also fail due to network errors, which `HttpClient` reports as an `HttpErrorResponse` with a `status` of `0`. These can be simulated with the `error()` method:

```ts
const req = httpTesting.expectOne('/api/config');
req.error(new ProgressEvent('network error!'));

// Assert that the application successfully handled the network error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to update the example below ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. TestRequest.error() wraps the ProgressEvent in an HttpErrorResponse with status 0, which matches what the fetch backend emits, so code that checks the status behaves the same in tests and in the app. The example can't pass the TypeError that fetch actually throws, since error() only accepts a ProgressEvent (the ErrorEvent overload is deprecated). Happy to change it if you'd prefer something else.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in that case the user gets a ProgressEvent on the stream and the sentence would be correct, no ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the test, yes: err.error is whatever was passed to error(). But the sentence describes real network errors, and with the default fetch backend those carry the TypeError thrown by fetch, not a ProgressEvent (only withXhr() gives a ProgressEvent):

status error
FetchBackend (default) 0 TypeError from fetch
withXhr() 0 ProgressEvent
req.error(new ProgressEvent(...)) in a test 0 the given ProgressEvent

So a handler that checks err.error instanceof ProgressEvent would pass this test but never match in the app. Status 0 is the part that's the same in tests and with both backends, which is why I described that.

I can also mention that error() wraps the given ProgressEvent, if that makes it clearer:

Suggested change
Requests can also fail due to network errors, which `HttpClient` reports as an `HttpErrorResponse` with a `status` of `0`. These can be simulated with the `error()` method, which wraps the given `ProgressEvent` in such a response:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: docs Related to the documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants