This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 26241 - Clarification request: What happens in FetchBodyStream on failure?
Summary: Clarification request: What happens in FetchBodyStream on failure?
Status: RESOLVED WORKSFORME
Alias: None
Product: WHATWG
Classification: Unclassified
Component: Fetch (show other bugs)
Version: unspecified
Hardware: All All
: P2 normal
Target Milestone: Unsorted
Assignee: Anne
QA Contact: sideshowbarker+fetchspec
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-30 16:56 UTC by Josh Karlin
Modified: 2014-08-12 13:15 UTC (History)
2 users (show)

See Also:


Attachments

Description Josh Karlin 2014-06-30 16:56:26 UTC
What if there is an error while reading (algorithm step 4.3) the stream?  Should the Promise reject?  With what error?
Comment 1 Anne 2014-06-30 19:51:43 UTC
I'm not sure in what ways reading a network IO stream can fail. On the response side I can see the network terminating (not harmful, should close the stream). On the request side there's no obvious failure case.

What am I missing?
Comment 2 Josh Karlin 2014-06-30 23:42:12 UTC
So if the network terminates early, the body will contain all of the bytes read up until termination?  Sounds good.
Comment 3 Domenic Denicola 2014-07-01 00:12:33 UTC
Hmm. While that is certainly possible, I think it would make the most sense to reject the promise with an error... I don't think users would expect a half-response.

Or do I misunderstand, and is early network termination not really an error?
Comment 4 Josh Karlin 2014-07-01 01:51:35 UTC
You're right, if the response's type is error then the promise is rejected.  I missed that part in my first read.  All clear now, thanks!
Comment 5 Anne 2014-07-01 06:23:30 UTC
Reopening since we're not on one line.

Comment 2 is accurate. We don't actively check Content-Length (some servers are bogus). There's also a flag set if the user, developer, or browser terminated the fetch for some reason. When that happens XMLHttpRequest currently clears the response. fetch() does nothing (as only the browser can currently terminate fetch() and that would be in a OOM or GC type of situation).

The promise that is rejected (comment 4) if response's type is error is fetch(), not fetch().then(r => r.body.asBlob()).

I think the current setup makes sense, you basically cannot fail once you got a successful batch of headers, but if you don't let me know.
Comment 6 Domenic Denicola 2014-07-01 20:57:49 UTC
> The promise that is rejected (comment 4) if response's type is error is fetch(), not fetch().then(r => r.body.asBlob()).

Notably if `fetch()` is rejected, then `r => r.body.asBlob()` will not run. In fact, you cannot get access to `r`, can you?

> I think the current setup makes sense, you basically cannot fail once you got a successful batch of headers 

To clarify, what would happen for the less-"raw" asX() methods? E.g. for asJSON, what if:

- the body is `{ x: "y" }` (length 10) but the Content-Length header is 12
- the body is `{ x: "y" }` but the Content-Length header is 2
Comment 7 Anne 2014-07-02 09:27:34 UTC
Yes, the promise fetch() returns would be rejected with a TypeError so there is no Response object.

For the first case you would get {x:"y"} as return value. The actual body that is being parsed would be of length ten as at that point the connection would be closed. The second case would be a SyntaxError as per HTTP everything after "{ " would be ignored. (HTTP defines how to deal with Content-Length mismatch.)
Comment 8 Anne 2014-07-31 13:57:27 UTC
Is this clear now?
Comment 9 Domenic Denicola 2014-07-31 18:55:01 UTC
(In reply to Anne from comment #8)
> Is this clear now?

yep, comment #7 sounds good. For people skimming the spec calling out those two scenarios explicitly (as non-normative examples) might be helpful, since you can imagine other behaviors, e.g. Content-Length mismatches causing rejection in the first case.
Comment 10 Anne 2014-08-01 05:55:26 UTC
Could you imagine that after reading HTTP? Unless we contradict HTTP I'm not sure examples are warranted.
Comment 11 Anne 2014-08-12 13:15:41 UTC
Leaving the examples to HTTP. Feel free to reopen if you disagree.