1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 04:48:52 -05:00

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

fixes #27132

When the body resource is unavailable when start reading it, the error
message is `Bad Resource ID` and that doesn't tell what's wrong very
well.

This PR changes that error message to `Cannot read body as underlying
resource unavailable`
This commit is contained in:
Yoshiya Hinosawa 2024-12-23 13:59:04 +09:00 committed by GitHub
parent 2e58e088b0
commit 3cc861cdca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 2 deletions

View file

@ -13,6 +13,7 @@
import { core, primordials } from "ext:core/mod.js";
const {
BadResourcePrototype,
isAnyArrayBuffer,
isArrayBuffer,
isStringObject,
@ -26,6 +27,7 @@ const {
JSONParse,
ObjectDefineProperties,
ObjectPrototypeIsPrototypeOf,
PromisePrototypeCatch,
TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetByteLength,
TypedArrayPrototypeGetByteOffset,
@ -160,7 +162,18 @@ class InnerBody {
)
) {
readableStreamThrowIfErrored(this.stream);
return readableStreamCollectIntoUint8Array(this.stream);
return PromisePrototypeCatch(
readableStreamCollectIntoUint8Array(this.stream),
(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;

View file

@ -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
@ -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",
);
});