Skip to content

Commit

Permalink
feat($resource): add resource to response for error interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry McCullough authored and gkalpak committed Jul 18, 2017
1 parent 631076a commit 823c7ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,12 @@ function shallowClearAndCopy(src, dst) {
* [requestType](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType).
* - **`interceptor`** - `{Object=}` - The interceptor object has two optional methods -
* `response` and `responseError`. Both `response` and `responseError` interceptors get called
* with `http response` object. See {@link ng.$http $http interceptors}.
* with `http response` object. See {@link ng.$http $http interceptors}. In addition, the
* resource instance or array object is accessible by the `resource` property of the
* `http response` object.
* Keep in mind that the associated promise will be resolved with the value returned by the
* response interceptor, if one is specified. The default response interceptor returns
* `response.resource` (i.e. the resource instance or array).
* - **`hasBody`** - `{boolean}` - allows to specify if a request body should be included or not.
* If not specified only POST, PUT and PATCH requests will have a body.
*
Expand Down Expand Up @@ -267,8 +272,7 @@ function shallowClearAndCopy(src, dst) {
* {@link ngRoute.$routeProvider resolve section of $routeProvider.when()} to defer view
* rendering until the resource(s) are loaded.
*
* On failure, the promise is rejected with the {@link ng.$http http response} object, without
* the `resource` property.
* On failure, the promise is rejected with the {@link ng.$http http response} object.
*
* If an interceptor object was provided, the promise will instead be resolved with the value
* returned by the interceptor.
Expand Down Expand Up @@ -776,6 +780,9 @@ angular.module('ngResource', ['ng']).
response.resource = value;

return response;
}, function(response) {
response.resource = value;
return $q.reject(response);
});

promise = promise['finally'](function() {
Expand Down
1 change: 1 addition & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ describe('basic usage', function() {
expect(callback).toHaveBeenCalledOnce();

var response = callback.calls.mostRecent().args[0];
expect(response.resource).toBe(ccs);
expect(response.status).toBe(404);
expect(response.config).toBeDefined();
});
Expand Down

0 comments on commit 823c7ed

Please sign in to comment.