diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index d1d20b04f483..45d146fcb209 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -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 diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index cae8d56fff5b..49d405dcc170 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -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() {