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

fix($resource): fulfill promise with the correct value on error #14839

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: 14 additions & 6 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ angular.module('ngResource', ['ng']).
defaultResponseInterceptor;
var responseErrorInterceptor = action.interceptor && action.interceptor.responseError ||
undefined;
var hasError = !!error;
var hasResponseErrorInterceptor = !!responseErrorInterceptor;
var timeoutDeferred;
var numericTimeoutPromise;

Expand Down Expand Up @@ -776,13 +778,19 @@ angular.module('ngResource', ['ng']).
(success || noop)(value, response.headers);
return value;
},
responseErrorInterceptor || error ?
(hasError || hasResponseErrorInterceptor) ?
Copy link
Contributor

@Narretz Narretz Jun 30, 2016

Choose a reason for hiding this comment

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

Can we use a real if here? The whole block is pretty hard to read (for me)

No, we can't, because it's the error fn of the promise

function(response) {
(error || noop)(response);
(responseErrorInterceptor || noop)(response);
return response;
}
: undefined);
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);
}

if (!isInstanceCall) {
// we are creating instance / collection
Expand Down
140 changes: 139 additions & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("basic usage", function() {
}

});
callback = jasmine.createSpy();
callback = jasmine.createSpy('callback');
}));


Expand Down Expand Up @@ -907,6 +907,7 @@ describe("basic usage", function() {
expect(cc.url).toBe('/new-id');
});


it('should pass the same transformed value to success callbacks and to promises', function() {
$httpBackend.expect('GET', '/CreditCard').respond(200, { value: 'original' });

Expand Down Expand Up @@ -1024,6 +1025,7 @@ describe("basic usage", function() {
});
});


it('should allow per action response interceptor that gets full response', function() {
CreditCard = $resource('/CreditCard', {}, {
query: {
Expand Down Expand Up @@ -1079,6 +1081,46 @@ describe("basic usage", function() {
expect(response.status).toBe(404);
expect(response.config).toBeDefined();
});


it('should fulfill the promise with the value returned by the responseError interceptor',
inject(function($q) {
CreditCard = $resource('/CreditCard', {}, {
test1: {
method: 'GET',
interceptor: {responseError: function() { return 'foo'; }}
},
test2: {
method: 'GET',
interceptor: {responseError: function() { return $q.resolve('bar'); }}
},
test3: {
method: 'GET',
interceptor: {responseError: function() { return $q.reject('baz'); }}
}
});

$httpBackend.whenGET('/CreditCard').respond(404);

callback.calls.reset();
CreditCard.test1().$promise.then(callback);
$httpBackend.flush();

expect(callback).toHaveBeenCalledOnceWith('foo');

callback.calls.reset();
CreditCard.test2().$promise.then(callback);
$httpBackend.flush();

expect(callback).toHaveBeenCalledOnceWith('bar');

callback.calls.reset();
CreditCard.test3().$promise.then(null, callback);
$httpBackend.flush();

expect(callback).toHaveBeenCalledOnceWith('baz');
})
);
});


Expand Down Expand Up @@ -1414,6 +1456,102 @@ describe('errors', function() {
});
});

describe('handling rejections', function() {
var $exceptionHandler;
var $httpBackend;
var $resource;

beforeEach(module('ngResource'));
beforeEach(module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
}));

beforeEach(inject(function(_$exceptionHandler_, _$httpBackend_, _$resource_) {
$exceptionHandler = _$exceptionHandler_;
$httpBackend = _$httpBackend_;
$resource = _$resource_;

$httpBackend.whenGET('/CreditCard').respond(404);
}));


it('should reject the promise even when there is an error callback', function() {
var errorCb1 = jasmine.createSpy('errorCb1');
var errorCb2 = jasmine.createSpy('errorCb2');
var CreditCard = $resource('/CreditCard');

CreditCard.get(noop, errorCb1).$promise.catch(errorCb2);
$httpBackend.flush();

expect(errorCb1).toHaveBeenCalledOnce();
expect(errorCb2).toHaveBeenCalledOnce();
});


it('should report a PUR when no error callback or responseError interceptor is provided',
function() {
var CreditCard = $resource('/CreditCard');

CreditCard.get();
$httpBackend.flush();

expect($exceptionHandler.errors.length).toBe(1);
expect($exceptionHandler.errors[0]).toMatch(/^Possibly unhandled rejection/);
}
);


it('should not report a PUR when an error callback or responseError interceptor is provided',
function() {
var CreditCard = $resource('/CreditCard', {}, {
test1: {
method: 'GET'
},
test2: {
method: 'GET',
interceptor: {responseError: function() { return {}; }}
}
});

// With error callback
CreditCard.test1(noop, noop);
$httpBackend.flush();

expect($exceptionHandler.errors.length).toBe(0);

// With responseError interceptor
CreditCard.test2();
$httpBackend.flush();

expect($exceptionHandler.errors.length).toBe(0);

// With error callback and responseError interceptor
CreditCard.test2(noop, noop);
$httpBackend.flush();

expect($exceptionHandler.errors.length).toBe(0);
}
);


it('should report a PUR when the responseError interceptor returns a rejected promise',
inject(function($q) {
var CreditCard = $resource('/CreditCard', {}, {
test: {
method: 'GET',
interceptor: {responseError: function() { return $q.reject({}); }}
}
});

CreditCard.test();
$httpBackend.flush();

expect($exceptionHandler.errors.length).toBe(1);
expect($exceptionHandler.errors[0]).toMatch(/^Possibly unhandled rejection/);
})
);
});

describe('cancelling requests', function() {
var httpSpy;
var $httpBackend;
Expand Down