From e3fde6e360146cc0d145a859cfdb55efbdaf20fa Mon Sep 17 00:00:00 2001 From: Baha Date: Mon, 3 Apr 2017 19:37:52 -0400 Subject: [PATCH] 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 --- resources/dist/index.js | Bin 119103 -> 119553 bytes resources/dist/index.js.map | Bin 152683 -> 153395 bytes src/gateway/rest.js | 35 ++++++++++++++++++++++---- tests/node-qunit/gateway/rest.test.js | 17 ++++++++++--- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/resources/dist/index.js b/resources/dist/index.js index c1ba196a2b1b240cc6cb91ad3e692c1be6eff220..5fea1bdce2221365e3cf71ac6187dafaf6b404f6 100644 GIT binary patch delta 476 zcmdnLh`n(c`-ae$)8kSYc{hi?>|_de$ydnBFHtBdN=+8X=@eWa&|l9E}3Dv(-HQk0ln z0uqEeoU>L(!9YP1%7Z$dvsNKBr#KbnWrRDjQj<$G;EEs~1(GtHKrtX)lv+|+l&1g< WqJpA)U~s2SH&kO3*?j*^!V>`NB!Pec delta 48 zcmV-00MGw{rU$>G2e4J?vtsFq0+Vm!n3C-m1#)F{b#jv~=Ngkn?HLC<3JD1beIT=r G?sMcT_7#Hw diff --git a/resources/dist/index.js.map b/resources/dist/index.js.map index f595699e4dd2614ba5109af13e69e30a42c69693..7fdcf3bb920d7c57aa803c7f987855137d93bead 100644 GIT binary patch delta 692 zcmaJ;&ubGw6lMYnDJ?FTD6zplsM1ZTOEzrNUj!m}C_+J^hZYCRxOs^i)|ogvNfZh7 z*rPb$g5X^*McG3=S^orYo;?eS;-N=(EpgMLGKYEdzVE&7eQ&%UE7^kOf_q{RgKgv}J(qH-U>>>>`yLLUB_!PUg23Z# z=(09yu#Tgn42R&a|@g@8|nkk;J!dXh}9Vo4( z>ES{ew|d-Fp5S0Y=fUQB14{5XMXB;yBg6_6Q@sFcwc2l&Cy4(Y<8m^ID|jEJx)D3O zB{oD8ee<|h_;Hs^?~Ke*2ZL+K3 z%E|S-Y?J3pa82el5}o|Ohi~$OD7MKDJXj}}Nyr1$MNF1=WSy*OVl=scn`?8Nu>vDg zmB(a5Vdu#TZqkz*xTPj5glcS7Fby#g#$w~<_!51_&D*jf-))zRWIW9@dES1G>1%@+ z=T4W3Wt5p-zm$=E`sOf3iRrnqjFLRoF?lg1AXuxQIenrJqww?w87B7W3xXMiw}-|v H-un&!b$?4X diff --git a/src/gateway/rest.js b/src/gateway/rest.js index 5974f78ef..2ad9f611a 100644 --- a/src/gateway/rest.js +++ b/src/gateway/rest.js @@ -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} */ 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 { diff --git a/tests/node-qunit/gateway/rest.test.js b/tests/node-qunit/gateway/rest.test.js index f24000ac6..83f832133 100644 --- a/tests/node-qunit/gateway/rest.test.js +++ b/tests/node-qunit/gateway/rest.test.js @@ -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 );