Skip to content

Commit 7ff936d

Browse files
committed
fix(ngCookie): log warning when attempting to store a non-string value
Previously, non-string values stored in $cookies would be removed, without warning the user, and causing difficulty debugging. Now, a warning is emitted, so that there is some idea of where the error is occurring. Closes angular#6151
1 parent 748a6c8 commit 7ff936d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/ngCookies/cookies.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ angular.module('ngCookies', ['ng']).
4545
</file>
4646
</example>
4747
*/
48-
factory('$cookies', ['$rootScope', '$browser', function ($rootScope, $browser) {
48+
factory('$cookies', ['$rootScope', '$browser', '$log', function ($rootScope, $browser, $log) {
4949
var cookies = {},
5050
lastCookies = {},
5151
lastBrowserCookies,
@@ -98,6 +98,7 @@ angular.module('ngCookies', ['ng']).
9898
if (angular.isDefined(lastCookies[name])) {
9999
cookies[name] = lastCookies[name];
100100
} else {
101+
$log.log('$cookies["' + name + '"] must be "string", but value is "' + typeof value + '"');
101102
delete cookies[name];
102103
}
103104
} else if (value !== lastCookies[name]) {

test/ngCookies/cookiesSpec.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,25 @@ describe('$cookies', function() {
4646

4747

4848
it('should drop or reset any cookie that was set to a non-string value',
49-
inject(function($cookies, $browser, $rootScope) {
49+
inject(function($cookies, $browser, $rootScope, $log) {
5050
$cookies.nonString = [1, 2, 3];
5151
$cookies.nullVal = null;
5252
$cookies.undefVal = undefined;
5353
$cookies.preexisting = function() {};
5454
$rootScope.$digest();
5555
expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'});
5656
expect($cookies).toEqual({'preexisting': 'oldCookie'});
57+
$log.reset();
58+
}));
59+
60+
61+
it('should log an error when a cookie is set to a non-string value',
62+
inject(function($cookies, $browser, $rootScope, $log) {
63+
spyOn($log, 'log');
64+
$cookies.number = 123;
65+
$rootScope.$digest();
66+
expect($log.log).toHaveBeenCalledWith('$cookies["number"] must be "string", but value is "number"');
67+
$log.reset();
5768
}));
5869

5970

0 commit comments

Comments
 (0)