Handle RESTBase 404

Treat these responses not as an API failure. Show a generic
preview whenever the server responds with a 404.

Bug: T160744
Change-Id: Id6169d9d4c7493f5b6511cc78fe65d448cdadc03
This commit is contained in:
Baha 2017-04-03 19:37:52 -04:00 committed by joakin
parent f80acb978b
commit e3fde6e360
4 changed files with 44 additions and 8 deletions

Binary file not shown.

Binary file not shown.

View file

@ -1,7 +1,8 @@
var RESTBASE_ENDPOINT = '/api/rest_v1/page/summary/',
RESTBASE_PROFILE = 'https://www.mediawiki.org/wiki/Specs/Summary/1.0.0',
createModel = require( '../preview/model' ).createModel,
mw = window.mediaWiki;
mw = window.mediaWiki,
$ = jQuery;
/**
* RESTBase gateway factory
@ -31,14 +32,38 @@ function createRESTBaseGateway( ajax, config ) {
/**
* Get the page summary from the api and transform the data
*
* Do not treat 404 as a failure as we want to show a generic
* preview for missing pages.
*
* @param {String} title
* @returns {jQuery.Promise<ext.popups.PreviewModel>}
*/
function getPageSummary( title ) {
return fetch( title )
.then( function( page ) {
return convertPageToModel( page, config.THUMBNAIL_SIZE );
} );
var result = $.Deferred();
fetch( title )
.then(
function( page ) {
result.resolve(
convertPageToModel( page, config.THUMBNAIL_SIZE ) );
},
function ( jqXHR ) {
if ( jqXHR.status === 404 ) {
result.resolve(
convertPageToModel( {
title: title,
lang: '',
dir: '',
extract: ''
}, 0 )
);
} else {
result.reject();
}
}
);
return result.promise();
}
return {

View file

@ -219,7 +219,7 @@ QUnit.test( 'RESTBase gateway stretches SVGs', function ( assert ) {
QUnit.test( 'RESTBase gateway handles the API failure', function ( assert ) {
var deferred = $.Deferred(),
api = this.sandbox.stub().returns( deferred.promise() ),
api = this.sandbox.stub().returns( deferred.reject( { status: 500 } ).promise() ),
gateway = createRESTBaseGateway( api ),
done = assert.async( 1 );
@ -228,7 +228,18 @@ QUnit.test( 'RESTBase gateway handles the API failure', function ( assert ) {
done();
} );
deferred.reject();
} );
QUnit.test( 'RESTBase gateway does not treat a 404 as a failure', function ( assert ) {
var deferred = $.Deferred(),
api = this.sandbox.stub().returns( deferred.reject( { status: 404 } ).promise() ),
gateway = createRESTBaseGateway( api ),
done = assert.async( 1 );
gateway.getPageSummary( 'Test Title' ).done( function () {
assert.ok( true );
done();
} );
} );
QUnit.test( 'RESTBase gateway returns the correct data ', function ( assert ) {
@ -265,7 +276,7 @@ QUnit.test( 'RESTBase gateway handles missing pages ', function ( assert ) {
uri: '/en.wikipedia.org/v1/page/summary/Missing_page'
},
api = this.sandbox.stub().returns(
$.Deferred().rejectWith( response ).promise()
$.Deferred().reject( response ).promise()
),
gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS ),
done = assert.async( 1 );