Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 823c7ed

Browse files
Kerry McCulloughgkalpak
Kerry McCullough
authored andcommitted
feat($resource): add resource to response for error interceptors
Closes #16109
1 parent 631076a commit 823c7ed

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/ngResource/resource.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,12 @@ function shallowClearAndCopy(src, dst) {
192192
* [requestType](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType).
193193
* - **`interceptor`** - `{Object=}` - The interceptor object has two optional methods -
194194
* `response` and `responseError`. Both `response` and `responseError` interceptors get called
195-
* with `http response` object. See {@link ng.$http $http interceptors}.
195+
* with `http response` object. See {@link ng.$http $http interceptors}. In addition, the
196+
* resource instance or array object is accessible by the `resource` property of the
197+
* `http response` object.
198+
* Keep in mind that the associated promise will be resolved with the value returned by the
199+
* response interceptor, if one is specified. The default response interceptor returns
200+
* `response.resource` (i.e. the resource instance or array).
196201
* - **`hasBody`** - `{boolean}` - allows to specify if a request body should be included or not.
197202
* If not specified only POST, PUT and PATCH requests will have a body.
198203
*
@@ -267,8 +272,7 @@ function shallowClearAndCopy(src, dst) {
267272
* {@link ngRoute.$routeProvider resolve section of $routeProvider.when()} to defer view
268273
* rendering until the resource(s) are loaded.
269274
*
270-
* On failure, the promise is rejected with the {@link ng.$http http response} object, without
271-
* the `resource` property.
275+
* On failure, the promise is rejected with the {@link ng.$http http response} object.
272276
*
273277
* If an interceptor object was provided, the promise will instead be resolved with the value
274278
* returned by the interceptor.
@@ -776,6 +780,9 @@ angular.module('ngResource', ['ng']).
776780
response.resource = value;
777781

778782
return response;
783+
}, function(response) {
784+
response.resource = value;
785+
return $q.reject(response);
779786
});
780787

781788
promise = promise['finally'](function() {

test/ngResource/resourceSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,7 @@ describe('basic usage', function() {
12051205
expect(callback).toHaveBeenCalledOnce();
12061206

12071207
var response = callback.calls.mostRecent().args[0];
1208+
expect(response.resource).toBe(ccs);
12081209
expect(response.status).toBe(404);
12091210
expect(response.config).toBeDefined();
12101211
});

0 commit comments

Comments
 (0)