Skip to content

Commit

Permalink
Merge pull request #389 from cujojs/prevent-console-removal
Browse files Browse the repository at this point in the history
Prevent minifiers from silencing unhandled rejections
  • Loading branch information
briancavalier committed Oct 29, 2014
2 parents 3e41df8 + 1ccf20f commit 6eda35c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
19 changes: 12 additions & 7 deletions lib/decorators/unhandledRejection.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ define(function(require) {
return function unhandledRejection(Promise) {
var logError = noop;
var logInfo = noop;
var localConsole;

if(typeof console !== 'undefined') {
logError = typeof console.error !== 'undefined'
? function (e) { console.error(e); }
: function (e) { console.log(e); };

logInfo = typeof console.info !== 'undefined'
? function (e) { console.info(e); }
: function (e) { console.log(e); };
// Alias console to prevent things like uglify's drop_console option from
// removing console.log/error. Unhandled rejections fall into the same
// category as uncaught exceptions, and build tools shouldn't silence them.
localConsole = console;
logError = typeof localConsole.error !== 'undefined'
? function (e) { localConsole.error(e); }
: function (e) { localConsole.log(e); };

logInfo = typeof localConsole.info !== 'undefined'
? function (e) { localConsole.info(e); }
: function (e) { localConsole.log(e); };
}

Promise.onPotentiallyUnhandledRejection = function(rejection) {
Expand Down
22 changes: 13 additions & 9 deletions monitor/ConsoleReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,41 @@ define(function(require) {
if(typeof console === 'undefined') {
log = warn = consoleNotAvailable;
} else {
if(typeof console.error === 'function'
&& typeof console.dir === 'function') {
// Alias console to prevent things like uglify's drop_console option from
// removing console.log/error. Unhandled rejections fall into the same
// category as uncaught exceptions, and build tools shouldn't silence them.
var localConsole = console;
if(typeof localConsole.error === 'function'
&& typeof localConsole.dir === 'function') {
warn = function(s) {
console.error(s);
localConsole.error(s);
};

log = function(s) {
console.log(s);
localConsole.log(s);
};

if(typeof console.groupCollapsed === 'function') {
if(typeof localConsole.groupCollapsed === 'function') {
groupStart = function(s) {
console.groupCollapsed(s);
localConsole.groupCollapsed(s);
};
groupEnd = function() {
console.groupEnd();
localConsole.groupEnd();
};
}
} else {
// IE8 has console.log and JSON, so we can make a
// reasonably useful warn() from those.
// Credit to webpro (https://github.com/webpro) for this idea
if (typeof console.log ==='function'
if (typeof localConsole.log ==='function'
&& typeof JSON !== 'undefined') {
log = warn = function (x) {
if(typeof x !== 'string') {
try {
x = JSON.stringify(x);
} catch(e) {}
}
console.log(x);
localConsole.log(x);
};
}
}
Expand Down

0 comments on commit 6eda35c

Please sign in to comment.