diff options
author | 2023-09-21 05:48:40 -0700 | |
---|---|---|
committer | 2023-09-21 05:48:40 -0700 | |
commit | e1cf08b3a6fb33f0c90837178fc36caeacb346e5 (patch) | |
tree | b7c5a6a83d37799b0c24af737dc2d19cc9d8cba3 | |
parent | 28f345346650dc94330b4a2cdcd1297df2916a39 (diff) | |
download | bun-e1cf08b3a6fb33f0c90837178fc36caeacb346e5.tar.gz bun-e1cf08b3a6fb33f0c90837178fc36caeacb346e5.tar.zst bun-e1cf08b3a6fb33f0c90837178fc36caeacb346e5.zip |
Fixes #5859jarred/5859
-rw-r--r-- | src/bun.js/bindings/bindings.cpp | 19 | ||||
-rw-r--r-- | test/js/bun/http/serve.test.ts | 42 |
2 files changed, 55 insertions, 6 deletions
diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index 47ef36aea..bff5640ef 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -1396,15 +1396,22 @@ BunString WebCore__DOMURL__fileSystemPath(WebCore__DOMURL* arg0) extern "C" JSC__JSValue ZigString__toJSONObject(const ZigString* strPtr, JSC::JSGlobalObject* globalObject) { auto str = Zig::toString(*strPtr); - auto throwScope = DECLARE_THROW_SCOPE(globalObject->vm()); - auto scope = DECLARE_CATCH_SCOPE(globalObject->vm()); - JSValue result = JSONParseWithException(globalObject, str); - if (auto* exception = scope.exception()) { + auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); + + // JSONParseWithException does not propagate exceptions as expected. See #5859 + JSValue result = JSONParse(globalObject, str); + + if (!result && !scope.exception()) { + scope.throwException(globalObject, createSyntaxError(globalObject, "Failed to parse JSON"_s)); + } + + if (scope.exception()) { + auto* exception = scope.exception(); scope.clearException(); - RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(exception->value())); + return JSC::JSValue::encode(exception); } - RELEASE_AND_RETURN(throwScope, JSValue::encode(result)); + return JSValue::encode(result); } JSC__JSValue SystemError__toErrorInstance(const SystemError* arg0, diff --git a/test/js/bun/http/serve.test.ts b/test/js/bun/http/serve.test.ts index b6ef75b54..8bae503b4 100644 --- a/test/js/bun/http/serve.test.ts +++ b/test/js/bun/http/serve.test.ts @@ -1165,3 +1165,45 @@ it("unix socket connection throws an error on a bad domain without crashing", as }); }).toThrow(); }); + +it("#5859 text", async () => { + const server = Bun.serve({ + port: 0, + development: false, + async fetch(req) { + return new Response(await req.text(), {}); + }, + }); + + const response = await fetch(`http://${server.hostname}:${server.port}`, { + method: "POST", + body: new Uint8Array([0xfd]), + }); + + expect(await response.text()).toBe("�"); + await server.stop(true); +}); + +it("#5859 json", async () => { + const server = Bun.serve({ + port: 0, + async fetch(req) { + try { + await req.json(); + } catch (e) { + return new Response("FAIL", { status: 500 }); + } + + return new Response("SHOULD'VE FAILED", {}); + }, + }); + + const response = await fetch(`http://${server.hostname}:${server.port}`, { + method: "POST", + body: new Uint8Array([0xfd]), + }); + + expect(response.ok).toBeFalse(); + expect(await response.text()).toBe("FAIL"); + await server.stop(true); +}); |