Search code examples
javascriptlawnchair

Receiving errors in lawnchair callbacks


If I call a method in lawnchair and it errors, can I get an error callback? I'm implementing an adapter and have some known error conditions that I would like to provide back to the client to handle. But I can't find out how errors are returned in the API. Are they node style? eg: callback(error, result) where error=null in the case of no errors, or something else?


Solution

  • Short answer: Lawnchair adapter's methods are not doing any major error handling. Client will receive an exception if anything particularly bad happens in adapter.

    Below is the code of DOM adapter's save method - it is clear that if anything bad happens, the callback will not be invoked at all; application's code will have to deal with the exception.

    save: function (obj, callback) {
        var key = obj.key ? this.name + '.' + obj.key : this.name + '.' + this.uuid()
        // if the key is not in the index push it on
        if (this.indexer.find(key) === false) this.indexer.add(key)
        // now we kil the key and use it in the store colleciton    
        delete obj.key;
        storage.setItem(key, JSON.stringify(obj))
        obj.key = key.slice(this.name.length + 1)
        if (callback) {
            this.lambda(callback).call(this, obj)
        }
        return this
    },
    

    You can, of course, do internal error handling in your adapter, and pass additional parameter to the callback. Doing this will make your adapter's behaviour inconsistent with the rest of adapters, which may be a downside.

    I suggest that your adapter would throw an exception, as other adapters do, unless it is possible to recover from the error inside the adapter.