Unchecked runtime.lastError when using Chrome API

后端 未结 3 2237
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 10:40

I use chrome.fileSystem API in my app to open a file. When I click the Cancel button of the file chooser dialog, an error occurs:

3条回答
  •  旧巷少年郎
    2020-11-28 10:59

    Late to the party, but here's how I suppressed the error in a chrome.windows.remove() call, in case it helps anyone else. Instead of

    chrome.windows.remove(foo);  // unconditional; runtime.lastError not checked
    

    I used

    chrome.windows.remove(
            foo,
            function ignore_error() { void chrome.runtime.lastError; }
    );
    

    void evaluates its operand and then returns undefined. I think this code fairly effectively documents that its purpose is to ignore errors :) .

    For brevity, ignore_error() could be pulled out of this call and used for multiple chrome API calls, or the name ignore_error could be omitted.

    Update In ES6, you can use the shorter arrow function syntax:

    chrome.windows.remove( foo, ()=>void chrome.runtime.lastError );
    

    Tested in Chrome 64

提交回复
热议问题