From 462cb127901f8d37bd7a3cc97997d1ed3b2cc4ce Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Mon, 19 Sep 2016 18:26:25 +0300 Subject: [PATCH] feat($http): remove deprecated callback methods: `success()/error()` BREAKING CHANGE: `$http`'s deprecated custom callback methods - `success()` and `error()` - have been removed. You can use the standard `then()`/`catch()` promise methods instead, but note that the method signatures and return values are different. `success(fn)` can be replaced with `then(fn)`, and `error(fn)` can be replaced with either `then(null, fn)` or `catch(fn)`. Before: ```js $http(...). success(function onSuccess(data, status, headers, config) { // Handle success ... }). error(function onError(data, status, headers, config) { // Handle error ... }); ``` After: ```js $http(...). then(function onSuccess(response) { // Handle success var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; ... }, function onError(response) { // Handle error var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; ... }); // or $http(...). then(function onSuccess(response) { // Handle success var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; ... }). catch(function onError(response) { // Handle error var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; ... }); ``` **Note:** There is a subtle difference between the variations showed above. When using `$http(...).success(onSuccess).error(onError)` or `$http(...).then(onSuccess, onError)`, the `onError()` callback will only handle errors/rejections produced by the `$http()` call. If the `onSuccess()` callback produces an error/rejection, it won't be handled by `onError()` and might go unnoticed. In contrast, when using `$http(...).then(onSuccess).catch(onError)`, `onError()` will handle errors/rejections produced by both `$http()` _and_ `onSuccess()`. --- docs/content/error/$http/legacy.ngdoc | 45 ---- src/ng/http.js | 61 ----- src/ng/sce.js | 4 +- test/ng/httpSpec.js | 317 ++++++++------------------ 4 files changed, 95 insertions(+), 332 deletions(-) delete mode 100644 docs/content/error/$http/legacy.ngdoc diff --git a/docs/content/error/$http/legacy.ngdoc b/docs/content/error/$http/legacy.ngdoc deleted file mode 100644 index 1ff4282fc607..000000000000 --- a/docs/content/error/$http/legacy.ngdoc +++ /dev/null @@ -1,45 +0,0 @@ -@ngdoc error -@name $http:legacy -@fullName The `success` and `error` methods on the promise returned from `$http` have been disabled. -@description - -This error occurs when the legacy promise extensions (`success` and `error`) -{@link $httpProvider#useLegacyPromiseExtensions legacy `$http` promise extensions} have been disabled. - -To resolve this error, either turn on the legacy extensions by adding -`$httpProvider.useLegacyPromiseExtensions(true);` to your application's configuration; or refactor you -use of `$http` to use `.then()` rather than `.success()` and `.error()`. - -For example if you code looked like this: - -```js -// Simple GET request example : -$http.get('/someUrl'). - success(function(data, status, headers, config) { - // This callback will be called asynchronously - // when the response is available - }). - error(function(data, status, headers, config) { - // called asynchronously if an error occurs - // or server returns response with an error status. - }); -``` - -then you would change it to look like: - -```js -// Simple GET request example : -$http.get('/someUrl'). - then(function(response) { - // (The response object contains the data, status, headers and config properties) - // This callback will be called asynchronously - // when the response is available. - }, function(response) { - // called asynchronously if an error occurs - // or server returns response with an error status. - }); -``` - -For more information, see the -{@link $httpProvider#useLegacyPromiseExtensions `$httpProvider.useLegacyPromiseExtensions`} -documentation. diff --git a/src/ng/http.js b/src/ng/http.js index 0cbc72571a32..a8f35aaac972 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -8,12 +8,6 @@ var JSON_ENDS = { '{': /}$/ }; var JSON_PROTECTION_PREFIX = /^\)\]\}',?\n/; -var $httpMinErr = minErr('$http'); -var $httpMinErrLegacyFn = function(method) { - return function() { - throw $httpMinErr('legacy', 'The method `{0}` on the promise returned from `$http` has been disabled.', method); - }; -}; function serializeValue(v) { if (isObject(v)) { @@ -340,30 +334,6 @@ function $HttpProvider() { return useApplyAsync; }; - var useLegacyPromise = true; - /** - * @ngdoc method - * @name $httpProvider#useLegacyPromiseExtensions - * @description - * - * Configure `$http` service to return promises without the shorthand methods `success` and `error`. - * This should be used to make sure that applications work without these methods. - * - * Defaults to true. If no value is specified, returns the current configured value. - * - * @param {boolean=} value If true, `$http` will return a promise with the deprecated legacy `success` and `error` methods. - * - * @returns {boolean|Object} If a value is specified, returns the $httpProvider for chaining. - * otherwise, returns the current configured value. - **/ - this.useLegacyPromiseExtensions = function(value) { - if (isDefined(value)) { - useLegacyPromise = !!value; - return this; - } - return useLegacyPromise; - }; - /** * @ngdoc property * @name $httpProvider#interceptors @@ -497,14 +467,6 @@ function $HttpProvider() { * $httpBackend.flush(); * ``` * - * ## Deprecation Notice - *
- * The `$http` legacy promise methods `success` and `error` have been deprecated. - * Use the standard `then` method instead. - * If {@link $httpProvider#useLegacyPromiseExtensions `$httpProvider.useLegacyPromiseExtensions`} is set to - * `false` then these methods will throw {@link $http:legacy `$http/legacy`} error. - *
- * * ## Setting HTTP Headers * * The $http service will automatically add certain HTTP headers to all requests. These defaults @@ -985,29 +947,6 @@ function $HttpProvider() { promise = chainInterceptors(promise, responseInterceptors); promise = promise.finally(completeOutstandingRequest); - if (useLegacyPromise) { - promise.success = function(fn) { - assertArgFn(fn, 'fn'); - - promise.then(function(response) { - fn(response.data, response.status, response.headers, config); - }); - return promise; - }; - - promise.error = function(fn) { - assertArgFn(fn, 'fn'); - - promise.then(null, function(response) { - fn(response.data, response.status, response.headers, config); - }); - return promise; - }; - } else { - promise.success = $httpMinErrLegacyFn('success'); - promise.error = $httpMinErrLegacyFn('error'); - } - return promise; diff --git a/src/ng/sce.js b/src/ng/sce.js index 78d21981619b..074c13d84579 100644 --- a/src/ng/sce.js +++ b/src/ng/sce.js @@ -612,8 +612,8 @@ function $SceDelegateProvider() { * .controller('AppController', ['$http', '$templateCache', '$sce', * function AppController($http, $templateCache, $sce) { * var self = this; - * $http.get('test_data.json', {cache: $templateCache}).success(function(userComments) { - * self.userComments = userComments; + * $http.get('test_data.json', {cache: $templateCache}).then(function(response) { + * self.userComments = response.data; * }); * self.explicitlyTrustedHtml = $sce.trustAsHtml( * '