diff --git a/integration/bunjs-only-snippets/fetch.test.js b/integration/bunjs-only-snippets/fetch.test.js
index b1ab57366d..2f976fd832 100644
--- a/integration/bunjs-only-snippets/fetch.test.js
+++ b/integration/bunjs-only-snippets/fetch.test.js
@@ -376,6 +376,21 @@ describe("Response", () => {
expect(await clone.text()).toBe("
hello
");
gc();
});
+ it("invalid json", async () => {
+ gc();
+ var body = new Response("hello
", {
+ headers: {
+ "content-type": "text/html; charset=utf-8",
+ },
+ });
+ try {
+ await body.json();
+ expect(false).toBe(true);
+ } catch (exception) {
+ expect(exception instanceof SyntaxError);
+ }
+ });
+
testBlobInterface((data) => new Response(data), true);
});
diff --git a/src/javascript/jsc/bindings/bindings.cpp b/src/javascript/jsc/bindings/bindings.cpp
index aaff3102cd..112ca81b7c 100644
--- a/src/javascript/jsc/bindings/bindings.cpp
+++ b/src/javascript/jsc/bindings/bindings.cpp
@@ -249,7 +249,14 @@ unsigned char JSC__JSValue__jsType(JSC__JSValue JSValue0)
JSC__JSValue JSC__JSValue__parseJSON(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1)
{
JSC::JSValue jsValue = JSC::JSValue::decode(JSValue0);
- return JSC::JSValue::encode(JSC::JSONParse(arg1, jsValue.toWTFString(arg1)));
+
+ JSC::JSValue result = JSC::JSONParse(arg1, jsValue.toWTFString(arg1));
+
+ if (!result) {
+ result = JSC::JSValue(JSC::createSyntaxError(arg1->globalObject(), "Failed to parse JSON"));
+ }
+
+ return JSC::JSValue::encode(result);
}
void JSC__JSGlobalObject__deleteModuleRegistryEntry(JSC__JSGlobalObject* global, ZigString* arg1)
diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig
index 3b31828094..88ecb6bfeb 100644
--- a/src/javascript/jsc/bindings/bindings.zig
+++ b/src/javascript/jsc/bindings/bindings.zig
@@ -2007,6 +2007,13 @@ pub const JSValue = enum(u64) {
else => false,
};
}
+ /// Empty as in "JSValue {}" rather than an empty string
+ pub fn isEmpty(this: JSValue) bool {
+ return switch (@enumToInt(this)) {
+ 0 => true,
+ else => false,
+ };
+ }
pub fn isBoolean(this: JSValue) bool {
return cppFn("isBoolean", .{this});
}
diff --git a/src/javascript/jsc/webcore/response.zig b/src/javascript/jsc/webcore/response.zig
index 721ffc5cad..c907c4b0f6 100644
--- a/src/javascript/jsc/webcore/response.zig
+++ b/src/javascript/jsc/webcore/response.zig
@@ -1708,6 +1708,9 @@ pub const Blob = struct {
value: JSC.JSValue,
global: *JSGlobalObject,
) JSC.JSValue {
+ if (value.isError()) {
+ return JSC.JSPromise.rejectedPromiseValue(global, value);
+ }
return JSC.JSPromise.resolvedPromiseValue(global, value);
}