Page MenuHomePhabricator

CX1: "TypeError: Cannot read property 'indexOf' of null" when translating a template
Closed, ResolvedPublic

Description

  1. Go to Special:ContentTranslation&page=Mogwai&from=es&to=ca&targettitle=Mogwai&debug=true
  2. Open console, enable pause on exceptions (also uncaught)

In ext.cx.tools.template.js

		cachedTemplateDataAPIRequests[ cacheKey ] =
			this.getNamespaceTranslation( this.language ).then( function ( namespace ) {
				var targetName;

				if ( self.title.indexOf( namespace + ':' ) !== 0 ) { // here
					targetName = namespace + ':' + self.title;
				} else {
					targetName = self.title;
				}

This seems to only happen when $wgUsejQueryThree = true; (which is now the default in master).

Very simplified example of the code:

promise = $.Deferred().reject().promise();

promise = promise.then(
	function () { console.log( 'We have a success' ); },
	function () { console.log( 'We have a failure' ); }
);

promise = promise.then(
	function () { console.log( 'We have a success' ); },
	function () { console.log( 'We have a failure' ); }
);

Before jQuery3 it prints "We have a failure" twice. In jQuery3 it prints "We have a failure" followed by "We have a success".

Related Objects

Event Timeline

Major changes have been made to the .then() method. In particular, any exception thrown within a .then() callback is now caught and converted into a rejection value, and any non-thenable value returned from a rejection handler becomes a fulfillment value. We strongly recommend that you add a .catch() method (new in 3.0) to the end of your promise chain to avoid difficult debugging issues.

as per migration guide

Change 360837 had a related patch set uploaded (by Nikerabbit; owner: Nikerabbit):
[mediawiki/extensions/ContentTranslation@master] Fix JavaScript error in template adaptation in jQuery 3

https://gerrit.wikimedia.org/r/360837

You can manually return a rejected promise if you require this behavior. This works with both jQuery 1 and 3.

promise = $.Deferred().reject().promise();

promise = promise.then(
	function () { console.log( 'We have a success' ); },
	function () { console.log( 'We have a failure' ); return $.Deferred().reject().promise(); }
);

promise = promise.then(
	function () { console.log( 'We have a success' ); },
	function () { console.log( 'We have a failure' ); }
);

Oh sorry, I see you found this out, I didn't refresh the page to see your patch :)

Change 360837 merged by jenkins-bot:
[mediawiki/extensions/ContentTranslation@master] Fix JavaScript error in template adaptation in jQuery 3

https://gerrit.wikimedia.org/r/360837

santhosh claimed this task.