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

fix(ngResource): do not eat exceptions in success callback #15628

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,18 +784,18 @@ angular.module('ngResource', ['ng']).
return value;
},
(hasError || hasResponseErrorInterceptor) ?
function(response) {
if (hasError) error(response);
return hasResponseErrorInterceptor ?
function(response) {
if (hasError && !hasResponseErrorInterceptor) {
// Avoid `Possibly Unhandled Rejection` error,
// but still fulfill the returned promise with a rejection
promise.catch(noop);
}
if (hasError) error(response);
return hasResponseErrorInterceptor ?
responseErrorInterceptor(response) :
$q.reject(response);
} :
undefined);
if (hasError && !hasResponseErrorInterceptor) {
// Avoid `Possibly Unhandled Rejection` error,
// but still fulfill the returned promise with a rejection
promise.catch(noop);
}
} :
undefined);

if (!isInstanceCall) {
// we are creating instance / collection
Expand Down
35 changes: 35 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1978,4 +1978,39 @@ describe('configuring `cancellable` on the provider', function() {
expect(creditCard3.$cancelRequest).toBeDefined();
});
});

describe('exception handling in callbacks', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests should be incorporated into the errors block. Also, can you add tests for all 4 combinations (no errorCallback/no responseErrorInterceptor, no errorCallback/yes responseErrorInterceptor, yes errorCallback/no responseErrorInterceptor, yes errorCallback/yes responseErrorInterceptor).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks I made the changes requested!

var $resource;
var $httpBackend;
var CreditCard;
beforeEach(module('ngResource'));

beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
$resource = $injector.get('$resource');
CreditCard = $resource('/CreditCard/:id:verb', { id: '@id.key' });
}));

it('should throw exception in success callback when error callback provided', function() {
$httpBackend.expect('GET', '/CreditCard/123').respond({ id: 123, number: '9876' });

var cc = CreditCard.get({ id: 123 },
function(res) {
throw new Error('should be caught');
}, function() {});

expect($httpBackend.flush).toThrow(new Error('should be caught'));
});

it('should throw exception in success callback when error callback not provided', function() {
$httpBackend.expect('GET', '/CreditCard/123').respond({ id: 123, number: '9876' });

var cc = CreditCard.get({ id: 123 },
function(res) {
throw new Error('should be caught');
});

expect($httpBackend.flush).toThrow(new Error('should be caught'));
});
});
});