Better error handling for unexpected responses

Bug: T182639
Change-Id: Iea04fe41b4be8e15927e93f16cbb4bb44328374f
This commit is contained in:
jdlrobson 2017-12-12 13:25:30 -08:00
parent cd2b0baaf8
commit ff49e7627b
4 changed files with 26 additions and 2 deletions

Binary file not shown.

Binary file not shown.

View file

@ -26,8 +26,6 @@ var RESTBASE_ENDPOINT = '/api/rest_v1/page/summary/',
* @param {Function} ajax A function with the same signature as `jQuery.ajax`
* @param {Object} config Configuration that affects the major behavior of the
* gateway.
* @param {Number} config.THUMBNAIL_SIZE The length of the major dimension of
* the thumbnail.
* @param {Function} extractParser A function that takes response and returns parsed extract
* @return {RESTBaseGateway}
*/
@ -59,6 +57,14 @@ export default function createRESTBaseGateway( ajax, config, extractParser ) {
fetch( title )
.then(
function ( page ) {
// Endpoint response may be empty.
if ( !page ) {
page = { title: title };
}
// And extract may be omitted if empty string
if ( page.extract === undefined ) {
page.extract = '';
}
result.resolve(
convertPageToModel( page, config.THUMBNAIL_SIZE, extractParser ) );
},

View file

@ -272,6 +272,24 @@ QUnit.test( 'RESTBase gateway handles missing images ', function ( assert ) {
);
} );
QUnit.test( 'RESTBase gateway handles missing extracts', function ( assert ) {
var api = this.sandbox.stub().returns( $.Deferred().resolve( {} ).promise() ),
gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS, provideParsedExtract );
return gateway.getPageSummary( 'Test Title with missing extract' ).then( function ( result ) {
assert.equal( result.extract, '!!', 'Extract' );
} );
} );
QUnit.test( 'RESTBase gateway handles no content success responses', function ( assert ) {
var api = this.sandbox.stub().returns( $.Deferred().resolve( { status: 204 } ).promise() ),
gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS, provideParsedExtract );
return gateway.getPageSummary( 'Test Title with empty response' ).then( function ( result ) {
assert.equal( result.extract, '!!', 'Extract' );
} );
} );
QUnit.test( 'RESTBase gateway handles missing pages ', function ( assert ) {
var response = {
type: 'https://mediawiki.org/wiki/HyperSwitch/errors/not_found',