From c7e60349bf41020e9974d5199872da1fdbf5212e Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Sat, 20 May 2017 12:09:20 +0200 Subject: [PATCH] ApiSandbox: Fix HTTP error handling Since bf69459, ApiSandbox seems to stall out when an API request results in an HTTP error (e.g. due to a PHP fatal error). Before that revision, it displayed the 'apisandbox-results-error' message in this situation. Apparently the jQuery 3 changes to Deferred behavior caused it to be impossible to have a then() filter return `this` (or anything else) in order to avoid replacing the existing promise that's being resolved or rejected. Bug: T165857 Change-Id: I3f646cdfe7fe8987437980790788821f51e728d1 --- .../mediawiki.special.apisandbox.js | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/resources/src/mediawiki.special/mediawiki.special.apisandbox.js b/resources/src/mediawiki.special/mediawiki.special.apisandbox.js index f53850a545..da8bdcda19 100644 --- a/resources/src/mediawiki.special/mediawiki.special.apisandbox.js +++ b/resources/src/mediawiki.special/mediawiki.special.apisandbox.js @@ -1100,25 +1100,18 @@ } } ) .then( null, function ( code, data, result, jqXHR ) { + var deferred = $.Deferred(); + if ( code !== 'http' ) { // Not really an error, work around mw.Api thinking it is. - return $.Deferred() - .resolve( result, jqXHR ) - .promise(); + deferred.resolve( result, jqXHR ); + } else { + // Just forward it. + deferred.reject.apply( deferred, arguments ); } - return this; + return deferred.promise(); } ) - .fail( function ( code, data ) { - var details = 'HTTP error: ' + data.exception; - $result.empty() - .append( - new OO.ui.LabelWidget( { - label: mw.message( 'apisandbox-results-error', details ).text(), - classes: [ 'error' ] - } ).$element - ); - } ) - .done( function ( data, jqXHR ) { + .then( function ( data, jqXHR ) { var m, loadTime, button, clear, ct = jqXHR.getResponseHeader( 'Content-Type' ); @@ -1195,6 +1188,15 @@ .on( 'click', button.setDisabled, [ true ], button ) .$element.appendTo( $result ); } + }, function ( code, data ) { + var details = 'HTTP error: ' + data.exception; + $result.empty() + .append( + new OO.ui.LabelWidget( { + label: mw.message( 'apisandbox-results-error', details ).text(), + classes: [ 'error' ] + } ).$element + ); } ); } ); }, -- 2.20.1