Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/fetch): better error message when body resource is unavailable #27429

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion ext/fetch/22_body.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import { core, primordials } from "ext:core/mod.js";
const {
BadResourcePrototype,
isAnyArrayBuffer,
isArrayBuffer,
isStringObject,
Expand Down Expand Up @@ -160,7 +161,15 @@ class InnerBody {
)
) {
readableStreamThrowIfErrored(this.stream);
return readableStreamCollectIntoUint8Array(this.stream);
return readableStreamCollectIntoUint8Array(this.stream).catch((e) => {
if (ObjectPrototypeIsPrototypeOf(BadResourcePrototype, e)) {
// TODO(kt3k): We probably like to pass e as `cause` if BadResource supports it.
throw new e.constructor(
"Cannot read body as underlying resource unavailable",
);
}
throw e;
});
} else {
this.streamOrStatic.consumed = true;
return this.streamOrStatic.body;
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/body_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "./test_util.ts";
import { assert, assertEquals, assertRejects } from "./test_util.ts";

// just a hack to get a body object
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -187,3 +187,14 @@ Deno.test(
assertEquals(file.size, 1);
},
);

Deno.test(async function bodyBadResourceError() {
const file = await Deno.open("README.md");
file.close();
const body = buildBody(file.readable);
await assertRejects(
() => body.arrayBuffer(),
Deno.errors.BadResource,
"Cannot read body as underlying resource unavailable",
);
});
Loading