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


it('should not swallow exception in success callback when error callback provided', function() {
$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 not swallow exception in success callback when error callback not provided', function() {
$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');
});

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


it('should not swallow exception in success callback when error callback provided and has responseError interceptor', function() {
$httpBackend.expect('GET', '/CreditCard/123').respond(null);
var CreditCard = $resource('/CreditCard/:id:verb', { id: '@id.key' }, {
'get': {
method: 'GET',
interceptor: { responseError: function() { return {}; }}
}
});

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 not swallow exception in success callback when error callback not provided and has responseError interceptor', function() {
$httpBackend.expect('GET', '/CreditCard/123').respond(null);
var CreditCard = $resource('/CreditCard/:id:verb', { id: '@id.key' }, {
'get': {
method: 'GET',
interceptor: { responseError: function() { return {}; }}
}
});

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