Repository navigation
Conversation
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`.
| 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. |
There was a problem hiding this comment.
Do we need to update the example below ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
But in that case the user gets a ProgressEvent on the stream and the sentence would be correct, no ?
There was a problem hiding this comment.
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:
| 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: |
With the default fetch backend, network errors are reported as an
HttpErrorResponsewith status 0 whoseerroris the error thrown byfetch, not aProgressEvent.