diff --git a/cli/js/buffer.ts b/cli/js/buffer.ts index 8389db4f95..473affe3dc 100644 --- a/cli/js/buffer.ts +++ b/cli/js/buffer.ts @@ -158,38 +158,30 @@ export class Buffer implements Reader, ReaderSync, Writer, WriterSync { async readFrom(r: Reader): Promise { let n = 0; while (true) { - try { - const i = this.#grow(MIN_READ); - this.#reslice(i); - const fub = new Uint8Array(this.#buf.buffer, i); - const nread = await r.read(fub); - if (nread === null) { - return n; - } - this.#reslice(i + nread); - n += nread; - } catch (e) { + const i = this.#grow(MIN_READ); + this.#reslice(i); + const fub = new Uint8Array(this.#buf.buffer, i); + const nread = await r.read(fub); + if (nread === null) { return n; } + this.#reslice(i + nread); + n += nread; } } readFromSync(r: ReaderSync): number { let n = 0; while (true) { - try { - const i = this.#grow(MIN_READ); - this.#reslice(i); - const fub = new Uint8Array(this.#buf.buffer, i); - const nread = r.readSync(fub); - if (nread === null) { - return n; - } - this.#reslice(i + nread); - n += nread; - } catch (e) { + const i = this.#grow(MIN_READ); + this.#reslice(i); + const fub = new Uint8Array(this.#buf.buffer, i); + const nread = r.readSync(fub); + if (nread === null) { return n; } + this.#reslice(i + nread); + n += nread; } } } diff --git a/cli/tests/unit/buffer_test.ts b/cli/tests/unit/buffer_test.ts index a5a0147abd..23e655a05b 100644 --- a/cli/tests/unit/buffer_test.ts +++ b/cli/tests/unit/buffer_test.ts @@ -7,6 +7,8 @@ import { assertEquals, assert, assertStringContains, + assertThrows, + assertThrowsAsync, unitTest, } from "./test_util.ts"; @@ -202,6 +204,9 @@ unitTest(async function bufferReadFrom(): Promise { const fub = new Uint8Array(testString.length); await empty(b, s, fub); } + assertThrowsAsync(async function () { + await new Buffer().readFrom(null!); + }); }); unitTest(async function bufferReadFromSync(): Promise { @@ -221,6 +226,9 @@ unitTest(async function bufferReadFromSync(): Promise { const fub = new Uint8Array(testString.length); await empty(b, s, fub); } + assertThrows(function () { + new Buffer().readFromSync(null!); + }); }); unitTest(async function bufferTestGrow(): Promise { diff --git a/cli/tests/unit/test_util.ts b/cli/tests/unit/test_util.ts index d5010fc630..8b690505a9 100644 --- a/cli/tests/unit/test_util.ts +++ b/cli/tests/unit/test_util.ts @@ -4,6 +4,7 @@ import { assert, assertEquals } from "../../../std/testing/asserts.ts"; export { assert, assertThrows, + assertThrowsAsync, assertEquals, assertMatch, assertNotEquals,