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 3 commits
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
73 changes: 73 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,79 @@ describe('handling rejections', function() {
expect($exceptionHandler.errors[0]).toMatch(/^Possibly unhandled rejection/);
})
);


it('should throw exception in success callback when error callback provided and no responseErrorInterceptor', function() {
Copy link
Member

Choose a reason for hiding this comment

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

Change the tests descriptions to something like:

should not swallow exception in success callback...

$httpBackend.expect('GET', '/CreditCard/123').respond({ id: 123, number: '9876' });
var CreditCard = $resource('/CreditCard/:id:verb', { id: '@id.key' });
Copy link
Member

Choose a reason for hiding this comment

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

Remove the unnecessary bits (here and below). E.g.:

$httpBackend.expect('GET', '/CreditCard/123').respond(null);
var CreditCard = $resource('/CreditCard/:id');

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

$httpBackend.flush();
expect($exceptionHandler.errors.length).toBe(1);
expect($exceptionHandler.errors[0]).toMatch(/^Error: should be caught/);
});


it('should throw exception in success callback when error callback not provided and no responseErrorInterceptor', function() {
$httpBackend.expect('GET', '/CreditCard/123').respond({ id: 123, number: '9876' });
var CreditCard = $resource('/CreditCard/:id:verb', { id: '@id.key' });
var cc = CreditCard.get({ id: 123 },
function(res) {
throw new Error('should be caught');
});

$httpBackend.flush();
expect($exceptionHandler.errors.length).toBe(1);
expect($exceptionHandler.errors[0]).toMatch(/^Error: should be caught/);
});


it('should throw exception in success callback when error callback provided and has responseErrorInterceptor', function() {
inject(function($q) {

$httpBackend.expect('GET', '/CreditCard/123').respond({ id: 123, number: '9876' });
var CreditCard = $resource('/CreditCard/:id:verb', { id: '@id.key' }, {
'get': {
method: 'GET',
interceptor: { responseError: function() { return $q.reject({}); } }
Copy link
Member

Choose a reason for hiding this comment

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

Make this a non-rejecting interceptor.

}
});

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

$httpBackend.flush();
expect($exceptionHandler.errors.length).toBe(1);
expect($exceptionHandler.errors[0]).toMatch(/^Error: should be caught/);
});
});


it('should throw exception in success callback when error callback not provided and has responseErrorInterceptor', function() {
inject(function($q) {

$httpBackend.expect('GET', '/CreditCard/123').respond({ id: 123, number: '9876' });
var CreditCard = $resource('/CreditCard/:id:verb', { id: '@id.key' }, {
'get': {
method: 'GET',
interceptor: { responseError: function() { return $q.reject({}); } }
Copy link
Member

Choose a reason for hiding this comment

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

Make this a non-rejecting interceptor.

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

$httpBackend.flush();
expect($exceptionHandler.errors.length).toBe(1);
expect($exceptionHandler.errors[0]).toMatch(/^Error: should be caught/);
});
});
});

describe('cancelling requests', function() {
Expand Down