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

Commit 731e1f6

Browse files
fix($http): throw error if success and error methods do not receive a function
Closes #11330 Closes #11333
1 parent 634e467 commit 731e1f6

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/ng/http.js

+4
Original file line numberDiff line numberDiff line change
@@ -806,13 +806,17 @@ function $HttpProvider() {
806806
}
807807

808808
promise.success = function(fn) {
809+
assertArgFn(fn, 'fn');
810+
809811
promise.then(function(response) {
810812
fn(response.data, response.status, response.headers, config);
811813
});
812814
return promise;
813815
};
814816

815817
promise.error = function(fn) {
818+
assertArgFn(fn, 'fn');
819+
816820
promise.then(null, function(response) {
817821
fn(response.data, response.status, response.headers, config);
818822
});

test/ng/httpSpec.js

+56
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,34 @@ describe('$http', function() {
432432
var httpPromise = $http({url: '/url', method: 'GET'});
433433
expect(httpPromise.success(callback)).toBe(httpPromise);
434434
});
435+
436+
437+
it('should error if the callback is not a function', function() {
438+
expect(function() {
439+
$http({url: '/url', method: 'GET'}).success();
440+
}).toThrowMinErr('ng', 'areq');
441+
442+
expect(function() {
443+
$http({url: '/url', method: 'GET'}).success(undefined);
444+
}).toThrowMinErr('ng', 'areq');
445+
446+
expect(function() {
447+
$http({url: '/url', method: 'GET'}).success(null);
448+
}).toThrowMinErr('ng', 'areq');
449+
450+
451+
expect(function() {
452+
$http({url: '/url', method: 'GET'}).success({});
453+
}).toThrowMinErr('ng', 'areq');
454+
455+
expect(function() {
456+
$http({url: '/url', method: 'GET'}).success([]);
457+
}).toThrowMinErr('ng', 'areq');
458+
459+
expect(function() {
460+
$http({url: '/url', method: 'GET'}).success('error');
461+
}).toThrowMinErr('ng', 'areq');
462+
});
435463
});
436464

437465

@@ -456,6 +484,34 @@ describe('$http', function() {
456484
var httpPromise = $http({url: '/url', method: 'GET'});
457485
expect(httpPromise.error(callback)).toBe(httpPromise);
458486
});
487+
488+
489+
it('should error if the callback is not a function', function() {
490+
expect(function() {
491+
$http({url: '/url', method: 'GET'}).error();
492+
}).toThrowMinErr('ng', 'areq');
493+
494+
expect(function() {
495+
$http({url: '/url', method: 'GET'}).error(undefined);
496+
}).toThrowMinErr('ng', 'areq');
497+
498+
expect(function() {
499+
$http({url: '/url', method: 'GET'}).error(null);
500+
}).toThrowMinErr('ng', 'areq');
501+
502+
503+
expect(function() {
504+
$http({url: '/url', method: 'GET'}).error({});
505+
}).toThrowMinErr('ng', 'areq');
506+
507+
expect(function() {
508+
$http({url: '/url', method: 'GET'}).error([]);
509+
}).toThrowMinErr('ng', 'areq');
510+
511+
expect(function() {
512+
$http({url: '/url', method: 'GET'}).error('error');
513+
}).toThrowMinErr('ng', 'areq');
514+
});
459515
});
460516
});
461517

0 commit comments

Comments
 (0)