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:
Unchecked
runtime.lastError
while runningfileSystem.chooseEntry
: User cancelled
How to fix this error?
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:
Unchecked
runtime.lastError
while runningfileSystem.chooseEntry
: User cancelled
How to fix this error?
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 ofI used
void
evaluates its operand and then returnsundefined
. 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 multiplechrome
API calls, or the nameignore_error
could be omitted.Update In ES6, you can use the shorter arrow function syntax:
Tested in Chrome 64
This error is not important in this case, but I'll explain it and how to get rid of it.
What is this error?
Chrome APIs are mostly asynchronous: you have a callback that is called when the operation completes.
In case of
chrome.fileSystem.chooseEntry
, the chosen entry (or entries) will be passed to a callback:However, the API is not guaranteed to yield a result. For example, as in your case, the user may refuse to provide access by clicking "Cancel". Then there is no entry to work with, and you might want some explanation as to why that happened. How to raise an error without polluting all callbacks with an extra "error" argument?
Usually, Chrome deals with this by setting a global variable,
chrome.runtime.lastError
, at the time the callback is called. It is uniform across Chrome async APIs to use this instead of an error argument. In fact, quoting thechrome.fileSystem
docs:undefined
.chrome.runtime.lastError.message
will explain what's wrong.However, some callbacks did not check for this error variable. This may indicate a programming error, and Chrome added checks that
chrome.runtime.lastError
is actually checked (evaluated) in a callback. If not, it considers this to be an unhandled exception, and throws this error.Why do I say it's not important?
While it's an error, it won't break execution of your program (it's thrown at the end of an async task), and won't really be displayed to your users.
And while I say it's not important, you should check you program's logic. It may or may not be all right - this is intended to be a (stern) warning.
Why does it exist?
To warn, you, the developer, that your code probably tries to use a result that does not exist, because something went wrong.
It's possible you're already checking for an error, e.g.
Chrome does not employ complex heuristics to see if your code expects this possibility. As noted, errors are reported via
chrome.runtime.lastError
, and you're expected to check it.Note that this error is only raised when something bad does occur, and not when the API call finishes normally.
Can I catch it?
Not really; it's not raised by your code, but the cleanup code that handles async tasks in Chrome API; therefore using
try ... catch
in your callback won't help. Since it's asynchronous, usingtry
around the original API call won't help either.What to do with it?
You should add logic to your callback that checks for problems the way Chrome expects it, and probably react to it.
As long as Chrome sees that you checked the value when there is an error (that is, evaluated it within your callback), the error will not be thrown.
For this type of errors
try catch
won't be helpful. Instead you can access the global variablechrome.runtime.lastError
as @xan mentioned in his awesome response above(by the way you should read it and upvote it!).Now, here I provide an example that can be used: