question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Method console.warn invoked with wrong context

See original GitHub issue

Hi,

We’re running Autobahn JS on various terminal types, including Samsung and LG Smart TVs (Chromium-based browsers). With these Smart TVs, when the established connection is closed (for example after stopping the Crossbar server), we get JS errors like:

Uncaught TypeError: Illegal invocation

TypeError: Type error

In this case the AB connection’s onclose handler is not called, and thereby our code is not aware that reconnection must be attempted.

This issue appeared from ABJS v19.10.1. Connection handler self._transport.onclose calls:

log.warn("connection closed", reason, details);

https://github.com/crossbario/autobahn-js/blob/v19.10.1/packages/autobahn/lib/connection.js#L343

The log module defines its warn method as a direct reference to DOM console.warn:

var warn = console.warn;
exports.warn = warn;

https://github.com/crossbario/autobahn-js/blob/master/packages/autobahn/lib/log.js#L23

Execution of AB’s log.warn does not cause issue on desktop browsers (Chrome, Firefox, Edge, IE11). However it fails on the affected Smart TVs because the warn Function is executed with a this context which is not the console object. The Smart TV browsers may have a specific behaviour requiring this context (like redirecting the browser logs to an internal debugging system…).

To work on such platforms, Autobahn log module code should be something like:

var warn = console.warn.bind(console);

Note: AB’s log.debug method should not be affected by this issue, as its code uses the console context.

Beyond the specific case of these Smart TV platforms, from a more general OOP point of view, the method of an object’s instance should not be called outside of the context of this instance.

Thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
om26ercommented, Apr 14, 2020

@vla74 @hmistry-lz I have released autobahn-js 20.4.1, with contains the fix for this bug as well. Kindly try that.

1reaction
vla74commented, Dec 19, 2019

You’re welcome.

  1. After enabling the AB debug mode, I confirm that log.debug does not cause any exception on the affected Smart TVs, while log.warn does.
  2. Yes. In more details, I tested the 2 following fixes with the affected terminals, and both work:
  • Compact version with bind:
var warn = console.warn.bind(console);
  • Closure similar to the declaration of the debug function:
var warn = function () {
    console.warn.apply(console, arguments);
};

Function.prototype.bind normally requires an ECMAScript 5 compatible browser (http://kangax.github.io/compat-table/es5/#test-Function.prototype.bind), however I think that there’s a polyfill embedded in ABJS (https://github.com/crossbario/autobahn-js/blob/master/packages/autobahn/lib/polyfill/function.js).

BTW, for the sake of consistency, let me suggest that debug and warn functions are declared the same way (so, probably using the bind method for both).

Read more comments on GitHub >

github_iconTop Results From Across the Web

A proper wrapper for console.log with correct line number?
You can create console.log wrapper that can have side effects, call other functions, etc, and still retain the line number that called the...
Read more >
Keeping it simple with the JavaScript console - LogRocket Blog
The console.log method, and its friends console.warn and console.error , lets you dump objects in the console. The only difference between ...
Read more >
The 10 Most Common JavaScript Issues Developers Face
If you need help figuring out why your JavaScript isn't working, consult this list of the 10 most common JavaScript issues from a...
Read more >
console.warn() - Web APIs | MDN
The console.warn() method outputs a warning message to the Web console.
Read more >
Console | Node.js v19.3.0 Documentation
Warning : The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found