Skip to content

Commit de0a0ce

Browse files
committed
Safely hydrate keys that throw
1 parent 60dfd2f commit de0a0ce

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

packages/react-devtools-shared/src/hydration.js

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,19 @@ export function dehydrate(
216216
if (level >= LEVEL_THRESHOLD && !isPathAllowedCheck) {
217217
return createDehydrated(type, true, data, cleaned, path);
218218
}
219-
return data.map((item, i) =>
220-
dehydrate(
221-
item,
219+
const arr = [];
220+
for (let i = 0; i < data.length; i++) {
221+
arr[i] = dehydrateKey(
222+
data,
223+
i,
222224
cleaned,
223225
unserializable,
224226
path.concat([i]),
225227
isPathAllowed,
226228
isPathAllowedCheck ? 1 : level + 1,
227-
),
228-
);
229+
);
230+
}
231+
return arr;
229232

230233
case 'html_all_collection':
231234
case 'typed_array':
@@ -311,8 +314,9 @@ export function dehydrate(
311314
} = {};
312315
getAllEnumerableKeys(data).forEach(key => {
313316
const name = key.toString();
314-
object[name] = dehydrate(
315-
data[key],
317+
object[name] = dehydrateKey(
318+
data,
319+
key,
316320
cleaned,
317321
unserializable,
318322
path.concat([name]),
@@ -373,6 +377,46 @@ export function dehydrate(
373377
}
374378
}
375379

380+
function dehydrateKey(
381+
parent: Object,
382+
key: number | string | symbol,
383+
cleaned: Array<Array<string | number>>,
384+
unserializable: Array<Array<string | number>>,
385+
path: Array<string | number>,
386+
isPathAllowed: (path: Array<string | number>) => boolean,
387+
level: number = 0,
388+
): $PropertyType<DehydratedData, 'data'> {
389+
try {
390+
return dehydrate(
391+
parent[key],
392+
cleaned,
393+
unserializable,
394+
path,
395+
isPathAllowed,
396+
level,
397+
);
398+
} catch (error) {
399+
let preview = '';
400+
if (
401+
typeof error === 'object' &&
402+
error !== null &&
403+
typeof error.stack === 'string'
404+
) {
405+
preview = error.stack;
406+
} else if (typeof error === 'string') {
407+
preview = error;
408+
}
409+
cleaned.push(path);
410+
return {
411+
inspectable: false,
412+
preview_short: '[Exception]',
413+
preview_long: preview ? '[Exception: ' + preview + ']' : '[Exception]',
414+
name: preview,
415+
type: 'unknown',
416+
};
417+
}
418+
}
419+
376420
export function fillInPath(
377421
object: Object,
378422
data: DehydratedData,

0 commit comments

Comments
 (0)