Skip to content

Commit

Permalink
Fall back to JSON.stringify in console.log if Symbol is unavailable
Browse files Browse the repository at this point in the history
Summary: Symbol is not available in older versions of JSON resulting in crashes in `prettyFormat` because we are using a clowny transform.

Reviewed By: sebmck

Differential Revision: D16501208

fbshipit-source-id: 9952bf4993ae05335707cd386f9aa4bbc14b7564
  • Loading branch information
cpojer authored and facebook-github-bot committed Jul 25, 2019
1 parent d550256 commit 1798897
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions Libraries/Utilities/HMRClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

const Platform = require('./Platform');
const invariant = require('invariant');
const prettyFormat = require('pretty-format');

const MetroHMRClient = require('metro/src/lib/bundle-modules/HMRClient');

Expand Down Expand Up @@ -104,23 +103,36 @@ const HMRClient: HMRClientNativeInterface = {
log(level: LogLevel, data: Array<mixed>) {
try {
if (hmrClient) {
hmrClient.send(
JSON.stringify({
let message;
if (global.Symbol) {
message = JSON.stringify({
type: 'log',
level,
data: data.map(message =>
typeof message === 'string'
? message
: prettyFormat(message, {
data: data.map(item =>
typeof item === 'string'
? item
: require('pretty-format')(item, {
escapeString: true,
highlight: true,
maxDepth: 3,
min: true,
plugins: [prettyFormat.plugins.ReactElement],
plugins: [require('pretty-format').plugins.ReactElement],
}),
),
}),
);
});
} else {
try {
message = JSON.stringify({type: 'log', level, data});
} catch (error) {
message = JSON.stringify({
type: 'log',
level,
data: [error.message],
});
}
}

hmrClient.send(message);
}
} catch (error) {
// If sending logs causes any failures we want to silently ignore them
Expand Down

0 comments on commit 1798897

Please sign in to comment.