chore(stream/conversion.ts): handle errors in toTransformStream correctly (#2314)

This commit is contained in:
ayame113 2022-06-07 18:25:31 +09:00 committed by GitHub
parent f415da4c62
commit 4ab49091e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -243,7 +243,7 @@ export function toTransformStream<I, O>(
} catch (error) { } catch (error) {
// Propagate error to stream from iterator // Propagate error to stream from iterator
// If the stream status is "errored", it will be thrown, but ignore. // If the stream status is "errored", it will be thrown, but ignore.
await readable.cancel(error).catch(); await readable.cancel(error).catch(() => {});
controller.error(error); controller.error(error);
return; return;
} }

View File

@ -1,6 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "../testing/asserts.ts"; import { assert, assertEquals, assertRejects } from "../testing/asserts.ts";
import { import {
copy, copy,
iterateReader, iterateReader,
@ -350,7 +350,7 @@ Deno.test({
}); });
Deno.test({ Deno.test({
name: "[streams] toTransformStream() - iterable, not asynciterable", name: "[streams] toTransformStream() Pass iterable instead of asyncIterable",
async fn() { async fn() {
const readable = readableStreamFromIterable([0, 1, 2]) const readable = readableStreamFromIterable([0, 1, 2])
.pipeThrough(toTransformStream(function* (_src) { .pipeThrough(toTransformStream(function* (_src) {
@ -523,6 +523,27 @@ Deno.test({
}, },
}); });
Deno.test({
name:
"[streams] toTransformStream() Cancel streams with the correct error message",
async fn() {
const src = readableStreamFromIterable([0, 1, 2]);
// deno-lint-ignore require-yield
const transform = toTransformStream(function* (src) {
src.getReader(); // lock the source stream to cause error at cancel
throw new Error("foo");
});
await assertRejects(
async () => {
for await (const _ of src.pipeThrough(transform));
},
Error,
"foo",
);
},
});
class MockReaderCloser implements Deno.Reader, Deno.Closer { class MockReaderCloser implements Deno.Reader, Deno.Closer {
chunks: Uint8Array[] = []; chunks: Uint8Array[] = [];
closeCall = 0; closeCall = 0;