angularLocalStorage icon indicating copy to clipboard operation
angularLocalStorage copied to clipboard

Public get/set/remove methods will execute localStorage code if $cookieStore method cause exception

Open SmilingJoe opened this issue 10 years ago • 0 comments

For the public get(), set() and remove() methods, if supported == false and any type of exception occurs, the code specific to localStorage will attempt to run, even though supported == false.

For example, this is the current set():

set: function (key, value) {
    if (!supported) {
        try {
            $cookieStore.put(key, value);
            return value;
        } catch(e) {
            $log.log('Local Storage not supported, make sure you have angular-cookies enabled.');
        }
    }
    // THE FOLLOWING LINES RUN WHEN THE CATCH(E) ABOVE IS TRIGGERED
    var saver = angular.toJson(value);
    storage.setItem(key, saver);
    return privateMethods.parseValue(saver);
},

Suggest doing the following:

set: function (key, value) {
    if (!supported) {
        try {
            $cookieStore.put(key, value);
            return value;
        } catch(e) {
            $log.log('Local Storage not supported, make sure you have angular-cookies enabled.');
        }
    }
    else { // THIS CODE WILL NOW ONLY EVER EXECUTE IF (supported)
        var saver = angular.toJson(value);
        storage.setItem(key, saver);
        return privateMethods.parseValue(saver);
    }
},

SmilingJoe avatar Aug 06 '15 19:08 SmilingJoe