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:
parent
2e58e088b0
commit
3cc861cdca
2 changed files with 26 additions and 2 deletions
|
@ -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;
|
||||
|
|
|
@ -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",
|
||||
);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue