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/http): improve error message when underlying resource of request body unavailable #27463

Merged
merged 2 commits into from
Jan 6, 2025
Merged
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
20 changes: 19 additions & 1 deletion ext/http/00_serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,25 @@ class InnerRequest {
return null;
}
this.#streamRid = op_http_read_request_body(this.#external);
this.#body = new InnerBody(readableStreamForRid(this.#streamRid, false));
this.#body = new InnerBody(
readableStreamForRid(
this.#streamRid,
false,
undefined,
(controller, error) => {
if (ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error)) {
// TODO(kt3k): We would like to pass `error` as `cause` when BadResource supports it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, shouldn't this be doable already?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look so as they are implemented like:

  class BadResource extends Error {
    constructor(msg) {
      super(msg);
      this.name = "BadResource";
    }
  }

https://github.com/denoland/deno_core/blob/8d7bfea48ebfcaee989f8d72b243fb09302014f9/core/01_core.js#L302-L305

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue in deno_core denoland/deno_core#1031

controller.error(
new error.constructor(
`Cannot read request body as underlying resource unavailable`,
),
);
} else {
controller.error(error);
}
},
),
);
return this.#body;
}

Expand Down
8 changes: 6 additions & 2 deletions ext/web/06_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ const _original = Symbol("[[original]]");
* @param {boolean=} autoClose If the resource should be auto-closed when the stream closes. Defaults to true.
* @returns {ReadableStream<Uint8Array>}
*/
function readableStreamForRid(rid, autoClose = true, Super) {
function readableStreamForRid(rid, autoClose = true, Super, onError) {
const stream = new (Super ?? ReadableStream)(_brand);
stream[_resourceBacking] = { rid, autoClose };

Expand Down Expand Up @@ -947,7 +947,11 @@ function readableStreamForRid(rid, autoClose = true, Super) {
controller.byobRequest.respond(bytesRead);
}
} catch (e) {
controller.error(e);
if (onError) {
onError(controller, e);
} else {
controller.error(e);
}
tryClose();
}
},
Expand Down
45 changes: 44 additions & 1 deletion tests/unit/serve_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// deno-lint-ignore-file no-console

import { assertMatch, assertRejects } from "@std/assert";
import { assertIsError, assertMatch, assertRejects } from "@std/assert";
import { Buffer, BufReader, BufWriter, type Reader } from "@std/io";
import { TextProtoReader } from "../testdata/run/textproto.ts";
import {
Expand Down Expand Up @@ -4379,3 +4379,46 @@ Deno.test(
await server.finished;
},
);

Deno.test({
name:
"req.body.getReader().read() throws the error with reasonable error message",
}, async () => {
const { promise, resolve, reject } = Promise.withResolvers<Error>();
const server = Deno.serve({ onListen, port: 0 }, async (req) => {
const reader = req.body!.getReader();

try {
while (true) {
const { done } = await reader.read();
if (done) break;
}
} catch (e) {
// deno-lint-ignore no-explicit-any
resolve(e as any);
}

reject(new Error("Should not reach here"));
server.shutdown();
return new Response();
});

async function onListen({ port }: { port: number }) {
const body = "a".repeat(1000);
const request = `POST / HTTP/1.1\r\n` +
`Host: 127.0.0.1:${port}\r\n` +
`Content-Length: 1000\r\n` +
"\r\n" + body;

const connection = await Deno.connect({ hostname: "127.0.0.1", port });
await connection.write(new TextEncoder().encode(request));
connection.close();
}
await server.finished;
const e = await promise;
assertIsError(
e,
Deno.errors.BadResource,
"Cannot read request body as underlying resource unavailable",
);
});
Loading