Bug 1615399. Stop using NS_ERROR_DOM_TYPE_ERR in BodyStream. r=baku

Differential Revision: https://phabricator.services.mozilla.com/D62790

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Boris Zbarsky 2020-02-13 19:15:12 +00:00
Родитель 351adeed5d
Коммит d5851925ec
3 изменённых файлов: 34 добавлений и 15 удалений

Просмотреть файл

@ -336,10 +336,16 @@ void BodyStream::ErrorPropagation(JSContext* aCx,
}
// Let's use a generic error.
RefPtr<DOMException> error = DOMException::Create(NS_ERROR_DOM_TYPE_ERR);
ErrorResult rv;
// XXXbz can we come up with a better error message here to tell the
// consumer what went wrong?
rv.ThrowTypeError(u"Error in body stream");
JS::Rooted<JS::Value> errorValue(aCx);
if (ToJSValue(aCx, error, &errorValue)) {
bool ok = ToJSValue(aCx, std::move(rv), &errorValue);
MOZ_RELEASE_ASSERT(ok, "ToJSValue never fails for ErrorResult");
{
MutexAutoUnlock unlock(mMutex);
JS::ReadableStreamError(aCx, aStream, errorValue);
}

Просмотреть файл

@ -11,15 +11,21 @@ const WORKER =
'resources/fetch-event-respond-with-response-body-with-invalid-chunk-worker.js';
const SCOPE =
'resources/fetch-event-respond-with-response-body-with-invalid-chunk-iframe.html';
// Called by the iframe when done.
var done;
var done_was_called = new Promise(resolve => done = resolve);
// Called by the iframe when it has the reader promise we should watch.
var set_reader_promise;
let reader_promise = new Promise(resolve => set_reader_promise = resolve);
var set_fetch_promise;
let fetch_promise = new Promise(resolve => set_fetch_promise = resolve);
// This test creates an controlled iframe that makes a fetch request. The
// service worker returns a response with a body stream containing an invalid
// chunk.
promise_test(t => {
return service_worker_unregister_and_register(t, WORKER, SCOPE)
promise_test(async t => {
// Start off the process.
let errorConstructor;
await service_worker_unregister_and_register(t, WORKER, SCOPE)
.then(reg => {
add_completion_callback(() => reg.unregister());
return wait_for_state(t, reg.installing, 'activated');
@ -27,8 +33,14 @@ promise_test(t => {
.then(() => with_iframe(SCOPE))
.then(frame => {
t.add_cleanup(() => frame.remove())
})
.then(() => done_was_called)
.then(result => assert_equals(result, 'PASS'));
errorConstructor = frame.contentWindow.TypeError;
});
await promise_rejects_js(t, errorConstructor, reader_promise,
"read() should be rejected");
// Fetch should complete properly, because the reader error is caught in
// the subframe. That is, there should be no errors _other_ than the
// reader!
return fetch_promise;
}, 'Response with a ReadableStream having non-Uint8Array chunks should be transferred as errored');
</script>

Просмотреть файл

@ -5,10 +5,11 @@
<script>
'use strict';
fetch('body-stream-with-invalid-chunk').then(resp => {
parent.set_fetch_promise(fetch('body-stream-with-invalid-chunk').then(resp => {
const reader = resp.body.getReader();
return reader.read().then(
() => parent.done('FAIL: read() should be rejected'),
() => parent.done('PASS'));
}).catch(e => parent.done('FAIL: ' + e));
const reader_promise = reader.read();
parent.set_reader_promise(reader_promise);
// Suppress our expected error.
return reader_promise.catch(() => {});
}));
</script>