Page MenuHomePhabricator

onload hook handling shouldn't break when one hook fails
Closed, DeclinedPublic

Description

Author: mike.lifeguard+bugs

Description:
Copied from http://commons.wikimedia.org/w/index.php?title=Commons:Administrators%27_noticeboard&oldid=19396943#Javascript:

Our current setup with Javascripts has the problem that errors in one script may prevent other scripts from working. Most of our scripts (gadgets and others) work by registering a function that is to be executed once the main part of the page has been loaded. If there are several scripts (gadgets or others) active, they each will register their own function. These functions will then be run in the order they were registered once the page is ready.

The problem with this is that if one of these registered functions has an error and crashes (raises an exception), those functions that were registered later won't be run at all. That can have hard-to-track and usually unwanted effects.

Lupo has added to Commons' MediaWiki:Common.js such that exceptions from these "onload" functions get caught. That code catches such exceptions and logs them, thereby making sure that all registered functions get called even if some of them fail.

The question now is where to log these errors, and who should see them. Currently, that code tries to log them to the browser's "error console", if possible, and displays the errors on the page only if that fails or if the user viewing the page is a sysop. (That's based on the assumption that sysops are typically the only ones who can do something about JS errors, since most of our scripts reside in protected pages anyway.) Non-sysop users would see these errors only if their browser had no "error console". But I could just as well suppress error messages for non-sysop users completely, or suppress them for anyone by default and show them only for users who put a certain flag in their monobook.js.

The code was tested on IE6 (alas, no source info), FF3.0.7 with Firebug 1.3.3, Safari 3.2.2 (Webkit 525.28.1), Opera 9.6.3. When people complain about strange things that might be connected with our JavaScript (such as gadgets not working or not having a category bar on the upload form), tell them to look at the top of the page whether there's some error message there.

Of course, ideally, one would fix this in wikibits.js directly. They should have written the onload hook handling from the very beginning such that it doesn't break when one hook fails. Wikibits could also give us a better jsMsg, and addPortletLink could be written such that it also works on the old skins (see e.g. function addToolLink in MediaWiki:Utilities.js). It could also offer a very minimal set of common JavaScript enhancements (such as String.trim or Function.bind, and other simple commonly used and often re-implemented stuff). But frankly said, I have no patience. Getting a change into wikibits means opening a bugzilla "enhancement" bug report, and these get low priority. If something like this ever goes into wikibits, great. But I wanted it now.


Version: 1.15.x
Severity: normal
URL: http://commons.wikimedia.org/w/index.php?title=MediaWiki:Common.js&diff=19380030&oldid=18916605

Details

Reference
bz18039

Event Timeline

bzimport raised the priority of this task from to Low.Nov 21 2014, 10:36 PM
bzimport set Reference to bz18039.
bzimport added a subscriber: Unknown Object (MLST).

Patch to wikibits.js for bug 18039

Attached is a patch for implementing the mechanism I used at the Commons directly in wikibits.js. Note that this is a slightly stripped-down version; I didn't want to include the "use window.console for logging if available" bit in the patch. If you want that, too, take a look at function Logger.log_exception in
http://commons.wikimedia.org/wiki/MediaWiki:Common.js

Attached:

Didn't actually read what Mike had copied from the Commons discussion. Note that patch 5940 is only for the onload hook wrapping and exception logging (done using jsMsg), not about any other stuff that was mentioned on-wiki.

*Bulk BZ Change: +Patch to open bugs with patches attached that are missing the keyword*

sumanah wrote:

I'm sorry for the wait, Lupo. Thanks for your patch. I'm adding keywords to ask for review.

I rather think that this whole issue has been long obsoleted by the ResourceLoader development and jQuery deployment. I don't think it needs review; just close it as resolved/obsoleted.

sumanah wrote:

OK. Thanks again, Lupo.