deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

`serve` swallows errors when `Response` object body has been consumed already

Open aslilac opened this issue 3 years ago • 1 comments

Describe the bug

Steps to Reproduce

await serve(async (req: Request) => {
	const res = await handler(req);
	// Consumes the body! Deno won't be able to read it afterwards!
	console.log("%c <= ", "color: magenta", res.status, await res.text());
	return res;
});

Expected behavior

Should see some error output that the Request object didn't have any body content available to reply with

Environment

  • OS: [e.g. Ubuntu 20.04, MacOS 11] macOS 12
  • deno version: 1.22.0
  • std version: 0.140.0

aslilac avatar May 25 '22 21:05 aslilac

Hi @aslilac, just clarifying. Are you saying that handler somehow consumes req.body but calling req.text() thereafter doesn’t throw?

iuioiua avatar Aug 23 '22 21:08 iuioiua

Sorry @aslilac, I didn't read the title. @kt3k, this should be an easy fix - something like:

  if (res.body instanceof ReadableStream && res.bodyUsed) {
    throw new Error("Response body has already been consumed.");
  }

Are there any cases where this check is not desirable? If not, I'll submit the PR.

iuioiua avatar Sep 25 '22 22:09 iuioiua

sounds worth trying to me.

kt3k avatar Sep 26 '22 04:09 kt3k