如果我在草坪上调用一个方法并且它出错,我可以收到错误回调吗?我正在实现一个适配器,并有一些已知的错误条件,我想提供给客户端来处理。但我无法找出API中如何返回错误。他们是节点风格吗?例如:回调(错误,结果),其中如果没有错误,则错误= null,或其他什么?
答案 0 :(得分:2)
简短回答:Lawnchair适配器的方法没有做任何重大的错误处理。如果适配器中发生任何特别糟糕的情况,客户端将收到异常。
下面是DOM适配器的保存方法的代码 - 很明显,如果发生任何不好的事情,根本不会调用回调;应用程序的代码必须处理异常。
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
},
当然,您可以在适配器中执行内部错误处理,并将其他参数传递给回调。这样做会使适配器的行为与其他适配器不一致,这可能是一个缺点。
我建议您的适配器会像其他适配器那样抛出异常,除非可以从适配器内部的错误中恢复。