Skip to content

Commit 18438db

Browse files
committed
feat: add error handling
1 parent 07577fc commit 18438db

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

client/src/components/DynamicJsonForm.tsx

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,23 +218,54 @@ const DynamicJsonForm = ({
218218
const updateArray = (array: JsonValue[], path: string[], value: JsonValue): JsonValue[] => {
219219
const [index, ...restPath] = path;
220220
const arrayIndex = Number(index);
221+
222+
// Validate array index
223+
if (isNaN(arrayIndex)) {
224+
console.error(`Invalid array index: ${index}`);
225+
return array;
226+
}
227+
228+
// Check array bounds
229+
if (arrayIndex < 0) {
230+
console.error(`Array index out of bounds: ${arrayIndex} < 0`);
231+
return array;
232+
}
233+
221234
const newArray = [...array];
222235

223236
if (restPath.length === 0) {
224237
newArray[arrayIndex] = value;
225238
} else {
239+
// Ensure index position exists
240+
if (arrayIndex >= array.length) {
241+
console.warn(`Extending array to index ${arrayIndex}`);
242+
newArray.length = arrayIndex + 1;
243+
newArray.fill(null, array.length, arrayIndex);
244+
}
226245
newArray[arrayIndex] = updateValue(newArray[arrayIndex], restPath, value);
227246
}
228247
return newArray;
229248
};
230249

231250
const updateObject = (obj: JsonObject, path: string[], value: JsonValue): JsonObject => {
232251
const [key, ...restPath] = path;
252+
253+
// Validate object key
254+
if (typeof key !== 'string') {
255+
console.error(`Invalid object key: ${key}`);
256+
return obj;
257+
}
258+
233259
const newObj = { ...obj };
234260

235261
if (restPath.length === 0) {
236262
newObj[key] = value;
237263
} else {
264+
// Ensure key exists
265+
if (!(key in newObj)) {
266+
console.warn(`Creating new key in object: ${key}`);
267+
newObj[key] = {};
268+
}
238269
newObj[key] = updateValue(newObj[key], restPath, value);
239270
}
240271
return newObj;
@@ -243,20 +274,34 @@ const DynamicJsonForm = ({
243274
const updateValue = (current: JsonValue, path: string[], value: JsonValue): JsonValue => {
244275
if (path.length === 0) return value;
245276

246-
if (!current) {
247-
current = !isNaN(Number(path[0])) ? [] : {};
248-
}
277+
try {
278+
if (!current) {
279+
current = !isNaN(Number(path[0])) ? [] : {};
280+
}
249281

250-
if (Array.isArray(current)) {
251-
return updateArray(current, path, value);
252-
} else if (typeof current === 'object' && current !== null) {
253-
return updateObject(current, path, value);
282+
// Type checking
283+
if (Array.isArray(current)) {
284+
return updateArray(current, path, value);
285+
} else if (typeof current === 'object' && current !== null) {
286+
return updateObject(current, path, value);
287+
} else {
288+
console.error(`Cannot update path ${path.join('.')} in non-object/array value:`, current);
289+
return current;
290+
}
291+
} catch (error) {
292+
console.error(`Error updating value at path ${path.join('.')}:`, error);
293+
return current;
254294
}
255-
256-
return current;
257295
};
258296

259-
onChange(updateValue(value, path, fieldValue));
297+
try {
298+
const newValue = updateValue(value, path, fieldValue);
299+
onChange(newValue);
300+
} catch (error) {
301+
console.error('Failed to update form value:', error);
302+
// Keep the original value unchanged
303+
onChange(value);
304+
}
260305
};
261306

262307
return (

0 commit comments

Comments
 (0)