From ef8c30626362ffb083cde78f1a439f86eb37e58f Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Thu, 5 Jan 2017 03:05:57 +0000 Subject: [PATCH] REST API: Add error handling for fetch error in `buildModelGetter` of wp-api.js. When a call to a model getter method fails, reject the returned deferred object. Enables better handling of fetch errors. Props westonruter, adamsilverstein. Merges [39680] to the 4.7 branch. Fixes #39314. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@39682 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/js/wp-api.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/js/wp-api.js b/src/wp-includes/js/wp-api.js index 717ebf8e06..ccda6a0e8b 100644 --- a/src/wp-includes/js/wp-api.js +++ b/src/wp-includes/js/wp-api.js @@ -321,12 +321,17 @@ // Create the new getModel model. getModel = new wp.api.models[ modelName ]( attributes ); - // If we didn’t have an embedded getModel, fetch the getModel data. if ( ! getModel.get( embedCheckField ) ) { - getModel.fetch( { success: function( getModel ) { - deferred.resolve( getModel ); - } } ); + getModel.fetch( { + success: function( getModel ) { + deferred.resolve( getModel ); + }, + error: function( getModel, response ) { + deferred.reject( response ); + } + } ); } else { + // Resolve with the embedded model. deferred.resolve( getModel ); } @@ -392,12 +397,17 @@ // If we didn’t have embedded getObjects, fetch the getObjects data. if ( _.isUndefined( getObjects.models[0] ) ) { - getObjects.fetch( { success: function( getObjects ) { + getObjects.fetch( { + success: function( getObjects ) { - // Add a helper 'parent_post' attribute onto the model. - setHelperParentPost( getObjects, postId ); - deferred.resolve( getObjects ); - } } ); + // Add a helper 'parent_post' attribute onto the model. + setHelperParentPost( getObjects, postId ); + deferred.resolve( getObjects ); + }, + error: function( getModel, response ) { + deferred.reject( response ); + } + } ); } else { // Add a helper 'parent_post' attribute onto the model.