From a0a69ee146b39b9ee80dffaaa9ad79cf87f7dff8 Mon Sep 17 00:00:00 2001 From: Felipe Cardozo Date: Mon, 27 Oct 2025 22:31:33 -0300 Subject: [PATCH] fix: body already used error to throw TypeError (#24114) Should fix https://github.com/oven-sh/bun/issues/24104 ### What does this PR do? This PR is changing `ERR_BODY_ALREADY_USED` to be TypeError instead of Error. ### How did you verify your code works? A test case added to verify that request call correctly throws a TypeError after another request call on the same Request, confirming the fix addresses the issue. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/bun.js/bindings/ErrorCode.ts | 2 +- test/js/web/fetch/body-mixin-errors.test.ts | 25 +++++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/bun.js/bindings/ErrorCode.ts b/src/bun.js/bindings/ErrorCode.ts index 31a3c28bac..e87e171e6e 100644 --- a/src/bun.js/bindings/ErrorCode.ts +++ b/src/bun.js/bindings/ErrorCode.ts @@ -20,7 +20,7 @@ const errors: ErrorCodeMapping = [ ["ERR_ASSERTION", Error], ["ERR_ASYNC_CALLBACK", TypeError], ["ERR_ASYNC_TYPE", TypeError], - ["ERR_BODY_ALREADY_USED", Error], + ["ERR_BODY_ALREADY_USED", TypeError], ["ERR_BORINGSSL", Error], ["ERR_ZSTD", Error], ["ERR_BROTLI_INVALID_PARAM", RangeError], diff --git a/test/js/web/fetch/body-mixin-errors.test.ts b/test/js/web/fetch/body-mixin-errors.test.ts index b7568e4dc4..5fce3d4a6c 100644 --- a/test/js/web/fetch/body-mixin-errors.test.ts +++ b/test/js/web/fetch/body-mixin-errors.test.ts @@ -1,17 +1,24 @@ import { describe, expect, it } from "bun:test"; describe("body-mixin-errors", () => { - it("should fail when bodyUsed", async () => { - var res = new Response("a"); - expect(res.bodyUsed).toBe(false); - await res.text(); - expect(res.bodyUsed).toBe(true); + it.concurrent.each([ + ["Response", () => new Response("a"), (b: Response | Request) => b.text()], + [ + "Request", + () => new Request("https://example.com", { body: "{}", method: "POST" }), + (b: Response | Request) => b.json(), + ], + ])("should throw TypeError when body already used on %s", async (type, createBody, secondCall) => { + const body = createBody(); + await body.text(); try { - await res.text(); - throw new Error("should not get here"); - } catch (e: any) { - expect(e.message).toBe("Body already used"); + await secondCall(body); + expect.unreachable("body is already used"); + } catch (err: any) { + expect(err.name).toBe("TypeError"); + expect(err.message).toBe("Body already used"); + expect(err instanceof TypeError).toBe(true); } }); });