<commit_before>myApp.service('inventoryService', function() {
    this.newInventory = function(name, coin, items) {
        var inv = Inventory.create(name, coin, items);
        this.inventories[inv.id] = inv;

        this.store();
        return inv;
    };

    this.newItem = function(id, name, value, desc) {
        var inventory = this.inventories[id];
        var item = inventory.newItem(name, value, desc);

        this.store();
        return item;
    };

    this.removeInventory = function(id) {
        delete this.inventories[id];
        this.store();
    };

    this.removeItem = function(id, itemId) {
        this.inventories[id].removeItem(itemId);
        this.store();
    };

    this.getInventory = function(id) {
        try {
            return this.inventories[id];
        }
        catch(err) {
            return undefined;
        }
    };

    this.getItem = function(id, itemId) {
        try {
            var inventory = this.inventories[id];
            var item = inventory.getItem(itemId);
            return item;
        }
        catch(err) {
            return undefined;
        }
    };

    this.getInventories = function() {
        if(!this.inventories) {
            var invStr = localStorage.inventories;
            this.inventories = invStr ? JSON.parse(invStr) : this.getDefaults();
        }

        return this.inventories;
    };

    this.storeInventories = function() {
        localStorage.inventories = JSON.stringify(this.inventories);
    };

    this.store = function() {
        var inventories = this.inventories;
        return new Promise(function(resolve, reject) {
            var json = JSON.stringify(inventories);
            localStorage.inventories = json;
            resolve(json);
        });
    };
<commit_msg>Add try/catch blocks to add prototypes to inventory objects<commit_after>myApp.service('inventoryService', function() {
    this.inventories = this.getInventories();

    this.newInventory = function(name, coin, items, id) {
        var inv = Inventory.create(name, coin, items, id);
        this.inventories[inv.id] = inv;

        this.store();
        return inv;
    };

    this.newItem = function(id, name, value, desc) {
        var inventory = this.inventories[id], item;

        try {
            item = inventory.newItem(name, value, desc);
        }
        catch(err) {
            if(err instanceof TypeError) {
                var upgradedInventory = this.upgradeInventory(inventory);
                item = upgradedInventory.newItem(name, value, desc);
            }
        }

        this.store();
        return item;
    };

    this.removeInventory = function(id) {
        delete this.inventories[id];
        this.store();
    };

    this.removeItem = function(id, itemId) {
        var inventory = this.inventories[id];

        try {
            inventory.removeItem(itemId);
        }
        catch(err) {
            if(err instanceof TypeError) {
                var upgradedInventory = this.upgradeInventory(inventory);
                upgradedInventory.removeItem(itemId);
            }
        }

        this.store();
    };

    this.getInventory = function(id) {
        try {
            return this.inventories[id];
        }
        catch(err) {
            return undefined;
        }
    };

    this.getItem = function(id, itemId) {
        var item;

        try {
            var inventory = this.inventories[id];
            item = inventory.getItem(itemId);
        }
        catch(err) {
            if(err instanceof TypeError) {
                var upgradedInventory = this.upgradeInventory(this.inventories[id]);
                item = upgradedInventory.getItem(itemId);
            }
        }

        return item;
    };

    this.getInventories = function() {
        if(!this.inventories) {
            var invStr = localStorage.inventories;
            this.inventories = invStr ? JSON.parse(invStr) : this.getDefaults();
        }

        return this.inventories;
    };

    this.storeInventories = function() {
        localStorage.inventories = JSON.stringify(this.inventories);
    };

    this.store = function() {
        var inventories = this.inventories;
        return new Promise(function(resolve, reject) {
            var json = JSON.stringify(inventories);
            localStorage.inventories = json;
            resolve(json);
        });
    };

    this.upgradeInventory = function(inventory) {
        return Inventory.create(inventory.name, inventory.coin, inventory.items, inventory.id);
    };

{'fraction_non_alphanumeric': 0.11344721857205431, 'fraction_numerical': 0.0, 'mean_word_length': 1.8651191969887078, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7112429', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1279, 'n_words': 242}