Skip to content

Commit 231e282

Browse files
authored
Handle errors in response bodies
And also clean up the way we manage XMLHttpRequest's response, which allows for simplifying "handle response end-of-body" and "handle errors". (Also ends up removing the remaining dependency on Streams.) Tests: web-platform-tests/wpt#27778. Fixes #314.
1 parent 26f974f commit 231e282

File tree

1 file changed

+27
-44
lines changed

1 file changed

+27
-44
lines changed

xhr.bs

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,7 @@ return <a>this</a>'s <a>cross-origin credentials</a>.
839839
<ol>
840840
<li><p>Set <a>this</a>'s <a for=XMLHttpRequest>response</a> to <var>response</var>.
841841

842-
<li><p><a>Handle errors</a> for <a>this</a> and <a>this</a>'s
843-
<a for=XMLHttpRequest>response</a>.
842+
<li><p><a>Handle errors</a> for <a>this</a>.
844843

845844
<li><p>If <a>this</a>'s <a for=XMLHttpRequest>response</a> is a <a>network error</a>, then
846845
return.
@@ -852,8 +851,7 @@ return <a>this</a>'s <a>cross-origin credentials</a>.
852851
<li><p>If <a>this</a>'s <a>state</a> is not <i>headers received</i>, then return.
853852

854853
<li><p>If <a>this</a>'s <a for=XMLHttpRequest>response</a>'s <a for=response>body</a> is null,
855-
then run <a>handle response end-of-body</a> for <a>this</a> and <a>this</a>'s
856-
<a for=XMLHttpRequest>response</a>, and then return.
854+
then run <a>handle response end-of-body</a> for <a>this</a> and return.
857855

858856
<li>
859857
<p>Let <var>processBodyChunk</var> given <var>bytes</var> be these steps:
@@ -879,10 +877,16 @@ return <a>this</a>'s <a>cross-origin credentials</a>.
879877
</ol>
880878

881879
<li><p>Let <var>processEndOfBody</var> be this step: run <a>handle response end-of-body</a> for
882-
<a>this</a> and <a>this</a>'s <a for=XMLHttpRequest>response</a>.
880+
<a>this</a>.
883881

884-
<li><p>Let <var>processBodyError</var> be this step: run <a>handle errors</a> for <a>this</a>
885-
and <a>this</a>'s <a for=XMLHttpRequest>response</a>.
882+
<li>
883+
<p>Let <var>processBodyError</var> be these steps:
884+
885+
<ol>
886+
<li><p>Set <a>this</a>'s <a for=XMLHttpRequest>response</a> to a <a for=/>network error</a>.
887+
888+
<li><p>Run <a>handle errors</a> for <a>this</a>.
889+
</ol>
886890

887891
<li><p><a for=body>Incrementally read</a> <a>this</a>'s <a for=XMLHttpRequest>response</a>'s
888892
<a for=response>body</a>, given <var>processBodyChunk</var>, <var>processEndOfBody</var>,
@@ -916,18 +920,17 @@ return <a>this</a>'s <a>cross-origin credentials</a>.
916920
<ol>
917921
<li><p>If <a>this</a>'s <a>relevant settings object</a> has a <a>responsible document</a> which
918922
is not <a>allowed to use</a> the "<code><a>sync-xhr</a></code>" feature, then run
919-
<a>handle response end-of-body</a> for <a>this</a> and a <a>network error</a>, and then return.
920-
921-
<li><p>Let <var>response</var> be a <a for=/>network error</a>.
923+
<a>handle response end-of-body</a> for <a>this</a> and return.
922924

923925
<li><p>Let <var>processedResponse</var> be false.
924926

925927
<li>
926-
<p>Let <var>processResponseEndOfBody</var>, given a <var>fetchResponse</var> and
928+
<p>Let <var>processResponseEndOfBody</var>, given a <var>response</var> and
927929
<var>nullOrFailureOrBytes</var>, be these steps:
928930

929931
<ol>
930-
<li><p>Set <var>response</var> to <var>fetchResponse</var>.
932+
<li><p>If <var>nullOrFailureOrBytes</var> is not failure, then set <a>this</a>'s
933+
<a for=XMLHttpRequest>response</a> to <var>response</var>.
931934

932935
<li><p>If <var>nullOrFailureOrBytes</var> is a <a for=/>byte sequence</a>, then append
933936
<var>nullOrFailureOrBytes</var> to <a>this</a>'s <a>received bytes</a>.
@@ -949,31 +952,24 @@ return <a>this</a>'s <a>cross-origin credentials</a>.
949952
<li><p>If <var>processedResponse</var> is false, then set <a>this</a>'s <a>timed out flag</a> and
950953
<a for=fetch>terminate</a> <a for=/>fetching</a>.
951954

952-
<li><p>Run <a>handle response end-of-body</a> for <a>this</a> and <var>response</var>.
955+
<li><p>Run <a>handle response end-of-body</a> for <a>this</a>.
953956
</ol>
954957
</ol>
955958

956959
<p id=handle-response-end-of-file>To <dfn>handle response end-of-body</dfn> for an
957-
{{XMLHttpRequest}} object <var>xhr</var> and a <a for=/>response</a> <var>response</var>, run these
958-
steps:
960+
{{XMLHttpRequest}} object <var>xhr</var>, run these steps:
959961

960962
<ol>
961-
<li><p>If <var>xhr</var>'s <a>synchronous flag</a> is set, then set <var>xhr</var>'s
962-
<a for=XMLHttpRequest>response</a> to <var>response</var>.
963-
964-
<li><p><a>Handle errors</a> for <var>xhr</var> and <var>response</var>.
963+
<li><p><a>Handle errors</a> for <var>xhr</var>.
965964

966965
<li><p>If <var>xhr</var>'s <a for=XMLHttpRequest>response</a> is a <a>network error</a>, then
967966
return.
968967

969-
<li><p>If <var>xhr</var>'s <a>synchronous flag</a> is unset, then update <var>xhr</var>'s
970-
<a for=XMLHttpRequest>response</a>'s <a for=response>body</a> using <var>response</var>.
971-
972968
<li><p>Let <var>transmitted</var> be <var>xhr</var>'s <a>received bytes</a>'s
973969
<a for="byte sequence">length</a>.
974970

975-
<li><p>Let <var>length</var> be <var>response</var>'s <a for=response>body</a>'s
976-
<a for=body>total bytes</a>.
971+
<li><p>Let <var>length</var> be <var>xhr</var>'s <a for=XMLHttpRequest>response</a>'s
972+
<a for=response>body</a>'s <a for=body>total bytes</a>.
977973

978974
<li><p>If <var>xhr</var>'s <a>synchronous flag</a> is unset, then <a>fire a progress event</a>
979975
named <a event><code>progress</code></a> at <var>xhr</var> with <var>transmitted</var> and
@@ -992,8 +988,7 @@ steps:
992988
<var>transmitted</var> and <var>length</var>.
993989
</ol>
994990

995-
<p>To <dfn>handle errors</dfn> for an {{XMLHttpRequest}} object <var>xhr</var> and a
996-
<a for=/>response</a> <var>response</var>, run these steps:
991+
<p>To <dfn>handle errors</dfn> for an {{XMLHttpRequest}} object <var>xhr</var>, run these steps:
997992

998993
<ol>
999994
<li><p>If <var>xhr</var>'s <a><code>send()</code> flag</a> is unset, then return.
@@ -1002,25 +997,13 @@ steps:
1002997
for <var>xhr</var>, <a event><code>timeout</code></a>, and "{{TimeoutError!!exception}}"
1003998
{{DOMException}}.
1004999

1005-
<li><p>If <var>response</var> is a <a>network error</a>, then run the <a>request error steps</a>
1006-
for <var>xhr</var>, <a event><code>error</code></a>, and "{{NetworkError!!exception}}"
1007-
{{DOMException}}.
1008-
1009-
<li>
1010-
<p>Otherwise, if <var>response</var>'s <a for=response>body</a>'s <a for=body>stream</a> is
1011-
<a for=ReadableStream>errored</a>, then:
1012-
1013-
<ol>
1014-
<li><p>Set <var>xhr</var>'s <a>state</a> to <i>done</i>.
1015-
1016-
<li><p>Unset <var>xhr</var>'s <a><code>send()</code> flag</a>.
1017-
1018-
<li><p>Set <var>xhr</var>'s <a for=XMLHttpRequest>response</a> to a <a>network error</a>.
1019-
</ol>
1000+
<li><p>Otherwise, if <var>xhr</var>'s <a for=XMLHttpRequest>response</a>'s
1001+
<a for=response>aborted flag</a> is set, run the <a>request error steps</a> for <var>xhr</var>,
1002+
<a event><code>abort</code></a>, and "{{AbortError!!exception}}" {{DOMException}}.
10201003

1021-
<li><p>Otherwise, if <var>xhr</var>'s <var>response</var>'s <a for=response>aborted flag</a> is
1022-
set, then run the <a>request error steps</a> for <var>xhr</var>, <a event><code>abort</code></a>,
1023-
and "{{AbortError!!exception}}" {{DOMException}}.
1004+
<li><p>Otherwise, if <var>xhr</var>'s <a for=XMLHttpRequest>response</a>'s is a
1005+
<a for=/>network error</a>, run the <a>request error steps</a> for <var>xhr</var>,
1006+
<a event><code>error</code></a>, and "{{NetworkError!!exception}}" {{DOMException}}.
10241007
</ol>
10251008

10261009
<p>The <dfn>request error steps</dfn> for an {{XMLHttpRequest}} object <var>xhr</var>,

0 commit comments

Comments
 (0)