From deeb62441e39679275b9d4fc61fb422ac4f8b6a6 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 13 Jun 2026 17:40:30 +0000 Subject: [PATCH] Extract JSON parsing error handling in WasmDatabaseEngine Extracted the duplicated JSON parsing `try-catch` blocks and type checks from `updateCell` and `updateCells` inside `WasmDatabaseEngine.ts` into a new, reusable `parseJsonValueForPatching` helper method inside `json-utils.ts`. This simplifies the code and guarantees identical handling logic across both methods. --- src/core/engine/wasm/WasmDatabaseEngine.ts | 14 +++----------- src/core/json-utils.ts | 13 +++++++++++++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/core/engine/wasm/WasmDatabaseEngine.ts b/src/core/engine/wasm/WasmDatabaseEngine.ts index 68c5942..0d7f984 100644 --- a/src/core/engine/wasm/WasmDatabaseEngine.ts +++ b/src/core/engine/wasm/WasmDatabaseEngine.ts @@ -25,7 +25,7 @@ import type { import { escapeIdentifier, validateSqlType, validateRowId, validateRowIds } from '../../sql-utils'; import { crypto } from '../../../platform/cryptoShim'; import { buildSelectQuery, buildCountQuery } from '../../query-builder'; -import { applyMergePatch, computeJsonPatchUndo } from '../../json-utils'; +import { applyMergePatch, computeJsonPatchUndo, parseJsonValueForPatching } from '../../json-utils'; import { getNodeFs } from '../../platform/fs'; // ============================================================================ @@ -589,12 +589,7 @@ export class WasmDatabaseEngine implements DatabaseOperations { const currentResult = await this.executeQuery(`SELECT ${escapedCol} FROM ${escapedTbl} WHERE rowid = ?`, [rowIdNum]); let currentValue = currentResult[0]?.rows[0]?.[0]; - let currentObj = {}; - if (typeof currentValue === 'string') { - try { currentObj = JSON.parse(currentValue); } catch (e) { console.warn('Failed to parse current JSON value for patching (updateCell)', e); } - } else if (typeof currentValue === 'object' && currentValue !== null && !(currentValue instanceof Uint8Array)) { - currentObj = currentValue; - } + const currentObj = parseJsonValueForPatching(currentValue, 'updateCell'); const patchObj = typeof patch === 'string' ? JSON.parse(patch) : patch; const newValueObj = applyMergePatch(currentObj, patchObj); @@ -904,10 +899,7 @@ export class WasmDatabaseEngine implements DatabaseOperations { selectStmt.reset(); } - let currentObj = {}; - if (typeof currentValue === 'string') { - try { currentObj = JSON.parse(currentValue); } catch (e) { console.warn('Failed to parse current JSON value for patching (updateCells)', e); } - } + const currentObj = parseJsonValueForPatching(currentValue, 'updateCells'); let patchObj; if (typeof update.value === 'string') { diff --git a/src/core/json-utils.ts b/src/core/json-utils.ts index aaac9c6..ed5ec4a 100644 --- a/src/core/json-utils.ts +++ b/src/core/json-utils.ts @@ -108,6 +108,19 @@ export function applyMergePatch(target: unknown, patch: unknown, depth = 0): unk return targetObj; } +export function parseJsonValueForPatching(val: unknown, context: string): Record { + if (typeof val === 'string') { + try { + return JSON.parse(val); + } catch (e) { + console.warn(`Failed to parse current JSON value for patching (${context})`, e); + } + } else if (typeof val === 'object' && val !== null && !(val instanceof Uint8Array)) { + return val as Record; + } + return {}; +} + export type JsonUndoPlan = | { kind: 'restore'; value: string } | { kind: 'replace' };