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

Commit 5f6949f

Browse files
gkalpakpetebacondarwin
authored andcommitted
fix($resource): fulfill promise with the correct value on error
This fixes a regression introduced with 71cf28c. See #14837 for more info. Fixes #14837 Closes #14839
1 parent 4487cff commit 5f6949f

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

src/ngResource/resource.js

+14-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,19 @@ 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 `Possibly Unhandled Rejection` error,
791+
// but still fulfill the returned promise with a rejection
792+
promise.catch(noop);
793+
}
786794

787795
if (!isInstanceCall) {
788796
// we are creating instance / collection

test/ngResource/resourceSpec.js

+70-1
Original file line numberDiff line numberDiff line change
@@ -1500,12 +1500,17 @@ describe('errors', function() {
15001500
});
15011501

15021502
describe('handling rejections', function() {
1503+
var $exceptionHandler;
15031504
var $httpBackend;
15041505
var $resource;
15051506

15061507
beforeEach(module('ngResource'));
1508+
beforeEach(module(function($exceptionHandlerProvider) {
1509+
$exceptionHandlerProvider.mode('log');
1510+
}));
15071511

1508-
beforeEach(inject(function(_$httpBackend_, _$resource_) {
1512+
beforeEach(inject(function(_$exceptionHandler_, _$httpBackend_, _$resource_) {
1513+
$exceptionHandler = _$exceptionHandler_;
15091514
$httpBackend = _$httpBackend_;
15101515
$resource = _$resource_;
15111516

@@ -1524,6 +1529,70 @@ describe('handling rejections', function() {
15241529
expect(errorCb1).toHaveBeenCalledOnce();
15251530
expect(errorCb2).toHaveBeenCalledOnce();
15261531
});
1532+
1533+
1534+
it('should report a PUR when no error callback or responseError interceptor is provided',
1535+
function() {
1536+
var CreditCard = $resource('/CreditCard');
1537+
1538+
CreditCard.get();
1539+
$httpBackend.flush();
1540+
1541+
expect($exceptionHandler.errors.length).toBe(1);
1542+
expect($exceptionHandler.errors[0]).toMatch(/^Possibly unhandled rejection/);
1543+
}
1544+
);
1545+
1546+
1547+
it('should not report a PUR when an error callback or responseError interceptor is provided',
1548+
function() {
1549+
var CreditCard = $resource('/CreditCard', {}, {
1550+
test1: {
1551+
method: 'GET'
1552+
},
1553+
test2: {
1554+
method: 'GET',
1555+
interceptor: {responseError: function() { return {}; }}
1556+
}
1557+
});
1558+
1559+
// With error callback
1560+
CreditCard.test1(noop, noop);
1561+
$httpBackend.flush();
1562+
1563+
expect($exceptionHandler.errors.length).toBe(0);
1564+
1565+
// With responseError interceptor
1566+
CreditCard.test2();
1567+
$httpBackend.flush();
1568+
1569+
expect($exceptionHandler.errors.length).toBe(0);
1570+
1571+
// With error callback and responseError interceptor
1572+
CreditCard.test2(noop, noop);
1573+
$httpBackend.flush();
1574+
1575+
expect($exceptionHandler.errors.length).toBe(0);
1576+
}
1577+
);
1578+
1579+
1580+
it('should report a PUR when the responseError interceptor returns a rejected promise',
1581+
inject(function($q) {
1582+
var CreditCard = $resource('/CreditCard', {}, {
1583+
test: {
1584+
method: 'GET',
1585+
interceptor: {responseError: function() { return $q.reject({}); }}
1586+
}
1587+
});
1588+
1589+
CreditCard.test();
1590+
$httpBackend.flush();
1591+
1592+
expect($exceptionHandler.errors.length).toBe(1);
1593+
expect($exceptionHandler.errors[0]).toMatch(/^Possibly unhandled rejection/);
1594+
})
1595+
);
15271596
});
15281597

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

0 commit comments

Comments
 (0)