Skip to content

Commit 3d40936

Browse files
committed
fix($resource): fulfill promise with the correct value on error
This fixes a regression introduced with 71cf28c. See angular#14837 for more info. Fixes angular#14837
1 parent 7ca3d80 commit 3d40936

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

src/ngResource/resource.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ angular.module('ngResource', ['ng']).
695695
defaultResponseInterceptor;
696696
var responseErrorInterceptor = action.interceptor && action.interceptor.responseError ||
697697
undefined;
698+
var hasError = !!error;
699+
var hasResponseErrorInterceptor = !!responseErrorInterceptor;
698700
var timeoutDeferred;
699701
var numericTimeoutPromise;
700702

@@ -776,13 +778,18 @@ angular.module('ngResource', ['ng']).
776778
(success || noop)(value, response.headers);
777779
return value;
778780
},
779-
responseErrorInterceptor || error ?
781+
(hasError || hasResponseErrorInterceptor) ?
780782
function(response) {
781-
(error || noop)(response);
782-
(responseErrorInterceptor || noop)(response);
783-
return response;
784-
}
785-
: undefined);
783+
if (hasError) error(response);
784+
return hasResponseErrorInterceptor ?
785+
responseErrorInterceptor(response) :
786+
$q.reject(response);
787+
} :
788+
undefined);
789+
if (hasError && !hasResponseErrorInterceptor) {
790+
// Avoid PUR, but still reject the returned promise
791+
promise.catch(noop);
792+
}
786793

787794
if (!isInstanceCall) {
788795
// we are creating instance / collection

test/ngResource/resourceSpec.js

+70-1
Original file line numberDiff line numberDiff line change
@@ -1457,12 +1457,17 @@ describe('errors', function() {
14571457
});
14581458

14591459
describe('handling rejections', function() {
1460+
var $exceptionHandler;
14601461
var $httpBackend;
14611462
var $resource;
14621463

14631464
beforeEach(module('ngResource'));
1465+
beforeEach(module(function($exceptionHandlerProvider) {
1466+
$exceptionHandlerProvider.mode('log');
1467+
}));
14641468

1465-
beforeEach(inject(function(_$httpBackend_, _$resource_) {
1469+
beforeEach(inject(function(_$exceptionHandler_, _$httpBackend_, _$resource_) {
1470+
$exceptionHandler = _$exceptionHandler_;
14661471
$httpBackend = _$httpBackend_;
14671472
$resource = _$resource_;
14681473

@@ -1481,6 +1486,70 @@ describe('handling rejections', function() {
14811486
expect(errorCb1).toHaveBeenCalledOnce();
14821487
expect(errorCb2).toHaveBeenCalledOnce();
14831488
});
1489+
1490+
1491+
it('should report a PUR when no error callback or responseError interceptor is provided',
1492+
function() {
1493+
var CreditCard = $resource('/CreditCard');
1494+
1495+
CreditCard.get();
1496+
$httpBackend.flush();
1497+
1498+
expect($exceptionHandler.errors.length).toBe(1);
1499+
expect($exceptionHandler.errors[0]).toMatch(/^Possibly unhandled rejection/);
1500+
}
1501+
);
1502+
1503+
1504+
it('should not report a PUR when an error callback or responseError interceptor is provided',
1505+
function() {
1506+
var CreditCard = $resource('/CreditCard', {}, {
1507+
test1: {
1508+
method: 'GET'
1509+
},
1510+
test2: {
1511+
method: 'GET',
1512+
interceptor: {responseError: function() { return {}; }}
1513+
}
1514+
});
1515+
1516+
// With error callback
1517+
CreditCard.test1(noop, noop);
1518+
$httpBackend.flush();
1519+
1520+
expect($exceptionHandler.errors.length).toBe(0);
1521+
1522+
// With responseError interceptor
1523+
CreditCard.test2();
1524+
$httpBackend.flush();
1525+
1526+
expect($exceptionHandler.errors.length).toBe(0);
1527+
1528+
// With error callback and responseError interceptor
1529+
CreditCard.test2(noop, noop);
1530+
$httpBackend.flush();
1531+
1532+
expect($exceptionHandler.errors.length).toBe(0);
1533+
}
1534+
);
1535+
1536+
1537+
it('should report a PUR when the responseError interceptor returns a rejected promise',
1538+
inject(function($q) {
1539+
var CreditCard = $resource('/CreditCard', {}, {
1540+
test: {
1541+
method: 'GET',
1542+
interceptor: {responseError: function() { return $q.reject({}); }}
1543+
}
1544+
});
1545+
1546+
CreditCard.test();
1547+
$httpBackend.flush();
1548+
1549+
expect($exceptionHandler.errors.length).toBe(1);
1550+
expect($exceptionHandler.errors[0]).toMatch(/^Possibly unhandled rejection/);
1551+
})
1552+
);
14841553
});
14851554

14861555
describe('cancelling requests', function() {

0 commit comments

Comments
 (0)