Skip to content

Commit

Permalink
Safely hydrate keys that throw
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Oct 1, 2024
1 parent 60dfd2f commit a78d621
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions packages/react-devtools-shared/src/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,19 @@ export function dehydrate(
if (level >= LEVEL_THRESHOLD && !isPathAllowedCheck) {
return createDehydrated(type, true, data, cleaned, path);
}
return data.map((item, i) =>
dehydrate(
item,
const arr: Array<Object> = [];
for (let i = 0; i < data.length; i++) {
arr[i] = dehydrateKey(
data,
i,
cleaned,
unserializable,
path.concat([i]),
isPathAllowed,
isPathAllowedCheck ? 1 : level + 1,
),
);
);
}
return arr;

case 'html_all_collection':
case 'typed_array':
Expand Down Expand Up @@ -311,8 +314,9 @@ export function dehydrate(
} = {};
getAllEnumerableKeys(data).forEach(key => {
const name = key.toString();
object[name] = dehydrate(
data[key],
object[name] = dehydrateKey(
data,
key,
cleaned,
unserializable,
path.concat([name]),
Expand Down Expand Up @@ -373,6 +377,46 @@ export function dehydrate(
}
}

function dehydrateKey(
parent: Object,
key: number | string | symbol,
cleaned: Array<Array<string | number>>,
unserializable: Array<Array<string | number>>,
path: Array<string | number>,
isPathAllowed: (path: Array<string | number>) => boolean,
level: number = 0,
): $PropertyType<DehydratedData, 'data'> {
try {
return dehydrate(
parent[key],
cleaned,
unserializable,
path,
isPathAllowed,
level,
);
} catch (error) {
let preview = '';
if (
typeof error === 'object' &&
error !== null &&
typeof error.stack === 'string'
) {
preview = error.stack;
} else if (typeof error === 'string') {
preview = error;
}
cleaned.push(path);
return {
inspectable: false,
preview_short: '[Exception]',
preview_long: preview ? '[Exception: ' + preview + ']' : '[Exception]',
name: preview,
type: 'unknown',
};
}
}

export function fillInPath(
object: Object,
data: DehydratedData,
Expand Down

0 comments on commit a78d621

Please sign in to comment.