Skip to content

Commit e7ce2be

Browse files
Claude Botclaude
andcommitted
fix: use JSONParseWithException for proper error handling
- SQLClient.cpp: Fix bug where RETURN_IF_EXCEPTION after JSONParse would never trigger on JSON parse failure since JSONParse doesn't throw - BunString.cpp: Simplify by using JSONParseWithException instead of manually checking for empty result and throwing JSONParse returns an empty JSValue on failure without throwing, so RETURN_IF_EXCEPTION will not catch parsing errors. JSONParseWithException properly throws a SyntaxError which RETURN_IF_EXCEPTION can catch. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 27ff6aa commit e7ce2be

File tree

2 files changed

+2
-7
lines changed

2 files changed

+2
-7
lines changed

src/bun.js/bindings/BunString.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,7 @@ extern "C" [[ZIG_EXPORT(zero_is_throw)]] JSC::EncodedJSValue BunString__toJSON(
473473
BunString* bunString)
474474
{
475475
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
476-
JSC::JSValue result = JSC::JSONParse(globalObject, bunString->toWTFString());
477-
478-
if (!result && !scope.exception()) {
479-
scope.throwException(globalObject, createSyntaxError(globalObject, "Failed to parse JSON"_s));
480-
}
481-
476+
JSC::JSValue result = JSC::JSONParseWithException(globalObject, bunString->toWTFString());
482477
RETURN_IF_EXCEPTION(scope, {});
483478

484479
return JSC::JSValue::encode(result);

src/bun.js/bindings/SQLClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static JSC::JSValue toJS(JSC::VM& vm, JSC::JSGlobalObject* globalObject, DataCel
188188
case DataCellTag::Json: {
189189
if (cell.value.json) {
190190
auto str = WTF::String(cell.value.json);
191-
JSC::JSValue json = JSC::JSONParse(globalObject, str);
191+
JSC::JSValue json = JSC::JSONParseWithException(globalObject, str);
192192
RETURN_IF_EXCEPTION(scope, {});
193193
return json;
194194
}

0 commit comments

Comments
 (0)