Skip to content

prevent our loggers from throwing their own errors in a few more cases #3137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/lib/loggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,24 @@ loggers.error = function() {
* apply like other functions do
*/
function apply(f, args) {
if(f.apply) {
f.apply(f, args);
if(f && f.apply) {
try {
// `this` should always be console, since here we're always
// applying a method of the console object.
f.apply(console, args);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In current browsers this works anyway, but console is really the right this here and some old browsers (Chrome 51 and presumably before) need it to be correct. See eg https://github.com/plotly/phoenix-integration/issues/223#issuecomment-431863801

return;
}
catch(e) { /* in case apply failed, fall back on the code below */ }
}
else {
for(var i = 0; i < args.length; i++) {

// no apply - just try calling the function on each arg independently
for(var i = 0; i < args.length; i++) {
try {
f(args[i]);
}
catch(e) {
// still fails - last resort simple console.log
console.log(args[i]);
}
}
}
32 changes: 25 additions & 7 deletions test/jasmine/tests/lib_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,7 @@ describe('Test lib.js:', function() {

function consoleFn(name, hasApply, messages) {
var out = function() {
if(hasApply) expect(this).toBe(window.console);
var args = [];
for(var i = 0; i < arguments.length; i++) args.push(arguments[i]);
messages.push([name, args]);
Expand All @@ -1664,12 +1665,13 @@ describe('Test lib.js:', function() {
return out;
}

function mockConsole(hasApply, hasTrace) {
function mockConsole(hasApply, hasTrace, hasError) {
var out = {
MESSAGES: []
};
out.log = consoleFn('log', hasApply, out.MESSAGES);
out.error = consoleFn('error', hasApply, out.MESSAGES);

if(hasError) out.error = consoleFn('error', hasApply, out.MESSAGES);

if(hasTrace) out.trace = consoleFn('trace', hasApply, out.MESSAGES);

Expand All @@ -1687,7 +1689,7 @@ describe('Test lib.js:', function() {
});

it('emits one console message if apply is available', function() {
var c = window.console = mockConsole(true, true);
var c = window.console = mockConsole(true, true, true);
config.logging = 2;

Lib.log('tick', 'tock', 'tick', 'tock', 1);
Expand All @@ -1702,7 +1704,7 @@ describe('Test lib.js:', function() {
});

it('falls back on console.log if no trace', function() {
var c = window.console = mockConsole(true, false);
var c = window.console = mockConsole(true, false, true);
config.logging = 2;

Lib.log('Hi');
Expand All @@ -1715,7 +1717,7 @@ describe('Test lib.js:', function() {
});

it('falls back on separate calls if no apply', function() {
var c = window.console = mockConsole(false, false);
var c = window.console = mockConsole(false, false, true);
config.logging = 2;

Lib.log('tick', 'tock', 'tick', 'tock', 1);
Expand Down Expand Up @@ -1744,7 +1746,7 @@ describe('Test lib.js:', function() {
});

it('omits .log at log level 1', function() {
var c = window.console = mockConsole(true, true);
var c = window.console = mockConsole(true, true, true);
config.logging = 1;

Lib.log(1);
Expand All @@ -1758,7 +1760,7 @@ describe('Test lib.js:', function() {
});

it('logs nothing at log level 0', function() {
var c = window.console = mockConsole(true, true);
var c = window.console = mockConsole(true, true, true);
config.logging = 0;

Lib.log(1);
Expand All @@ -1767,6 +1769,22 @@ describe('Test lib.js:', function() {

expect(c.MESSAGES).toEqual([]);
});

it('falls back on simple log if there is no console.error', function() {
// TODO

var c = window.console = mockConsole(true, true, false);
config.logging = 2;

Lib.error('who are you', 'who who... are you', {a: 1, b: 2});

expect(c.MESSAGES).toEqual([
['log', ['ERROR:']],
['log', ['who are you']],
['log', ['who who... are you']],
['log', [{a: 1, b: 2}]]
]);
});
});

describe('keyedContainer', function() {
Expand Down