Skip to content

Commit

Permalink
Wrap console calls into a check (facebook#2301)
Browse files Browse the repository at this point in the history
* Wrap console calls into a check

* Add another check
  • Loading branch information
BrodaNoel authored and romaindso committed Jul 10, 2017
1 parent 5a88fbb commit 0681ffe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
10 changes: 5 additions & 5 deletions packages/react-dev-utils/webpackHotDevClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ var connection = new SockJS(
// to avoid spamming the console. Disconnect usually happens
// when developer stops the server.
connection.onclose = function() {
if (typeof console !== 'undefined') {
if (typeof console !== 'undefined' && typeof console.info === 'function') {
console.info(
'The development server has disconnected.\nRefresh the page if necessary.'
);
Expand All @@ -186,8 +186,8 @@ var hasCompileErrors = false;

function clearOutdatedErrors() {
// Clean up outdated compile errors, if any.
if (typeof console !== 'undefined') {
if (hasCompileErrors && typeof console.clear === 'function') {
if (typeof console !== 'undefined' && typeof console.clear === 'function') {
if (hasCompileErrors) {
console.clear();
}
}
Expand Down Expand Up @@ -226,7 +226,7 @@ function handleWarnings(warnings) {
errors: [],
});

if (typeof console !== 'undefined') {
if (typeof console !== 'undefined' && typeof console.warn === 'function') {
for (var i = 0; i < formatted.warnings.length; i++) {
console.warn(stripAnsi(formatted.warnings[i]));
}
Expand Down Expand Up @@ -266,7 +266,7 @@ function handleErrors(errors) {
showErrorOverlay(formatted.errors[0]);

// Also log them to the console.
if (typeof console !== 'undefined') {
if (typeof console !== 'undefined' && typeof console.error === 'function') {
for (var i = 0; i < formatted.errors.length; i++) {
console.error(stripAnsi(formatted.errors[i]));
}
Expand Down
28 changes: 15 additions & 13 deletions packages/react-error-overlay/src/effects/proxyConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,22 @@ const permanentRegister = function proxyConsole(
) {
if (typeof console !== 'undefined') {
const orig = console[type];
console[type] = function __stack_frame_overlay_proxy_console__() {
try {
const message = arguments[0];
if (typeof message === 'string' && reactFrameStack.length > 0) {
callback(message, reactFrameStack[reactFrameStack.length - 1]);
if (typeof orig === 'function') {
console[type] = function __stack_frame_overlay_proxy_console__() {
try {
const message = arguments[0];
if (typeof message === 'string' && reactFrameStack.length > 0) {
callback(message, reactFrameStack[reactFrameStack.length - 1]);
}
} catch (err) {
// Warnings must never crash. Rethrow with a clean stack.
setTimeout(function() {
throw err;
});
}
} catch (err) {
// Warnings must never crash. Rethrow with a clean stack.
setTimeout(function() {
throw err;
});
}
return orig.apply(this, arguments);
};
return orig.apply(this, arguments);
};
}
}
};

Expand Down

0 comments on commit 0681ffe

Please sign in to comment.