Javascript 使用 Chrome API 时未选中的 runtime.lastError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28431505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:46:13  来源:igfitidea点击:

Unchecked runtime.lastError when using Chrome API

javascriptgoogle-chrome-extensiongoogle-chrome-app

提问by zcfrank1st

I use chrome.fileSystemAPI in my app to open a file. When I click the Cancelbutton of the file chooser dialog, an error occurs:

chrome.fileSystem在我的应用程序中使用API 打开一个文件。当我单击Cancel文件选择器对话框的按钮时,发生错误:

Unchecked runtime.lastErrorwhile running fileSystem.chooseEntry: User cancelled

runtime.lastError运行时未选中fileSystem.chooseEntry:用户取消

How to fix this error?

如何修复此错误?

回答by Xan

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.

Chrome API 大多是异步的:您有一个在操作完成时调用的回调。

In case of chrome.fileSystem.chooseEntry, the chosen entry (or entries) will be passed to a callback:

在 的情况下chrome.fileSystem.chooseEntry,所选条目(或多个条目)将传递给回调:

chrome.fileSystem.chooseEntry(
  {/* options */},
  function(entry) {
    // This is the callback for the API call; you can do something with entry
  }
);

However, the API is not guaranteedto 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?

但是,不保证API 会产生结果。例如,在您的情况下,用户可能会通过单击“取消”拒绝提供访问权限。然后就没有可使用的条目,您可能需要一些关于为什么会发生这种情况的解释。如何在不使用额外的“错误”参数污染所有回调的情况下引发错误?

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 the chrome.fileSystemdocs:

通常,Chrome 通过chrome.runtime.lastError在调用回调时设置全局变量来处理此问题。在 Chrome 异步 API 中使用 this 而不是错误参数是统一的。事实上,引用chrome.fileSystem文档:

All failures are notified via chrome.runtime.lastError.

所有失败都通过 chrome.runtime.lastError 通知。

  • If everything is all right, it will be undefined.
  • If there is a problem, it will be non-empty, and chrome.runtime.lastError.messagewill explain what's wrong.
  • 如果一切顺利,那就是undefined
  • 如果有问题,它将是非空的,并chrome.runtime.lastError.message会解释什么是错误的。

However, some callbacks did not check for this error variable. This may indicate a programming error, and Chrome added checks that chrome.runtime.lastErroris actually checked (evaluated) in a callback. If not, it considers this to be an unhandled exception, and throws this error.

然而,一些回调没有检查这个错误变量。这可能表示编程错误,Chrome 添加了chrome.runtime.lastError在回调中实际检查(评估)的检查。如果不是,它认为这是一个未处理的异常,并抛出这个错误。

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 probablytries to use a result that does not exist, because something went wrong.

警告,的开发人员,你的代码可能会尝试使用不存在的结果,因为出事了。

It's possible you're already checking for an error, e.g.

您可能已经在检查错误,例如

if (entry) {
  // Process entry
} else {
  // Something went wrong (but what?)
}

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.

Chrome 不会使用复杂的启发式方法来查看您的代码是否需要这种可能性。如前所述,错误是通过 报告的chrome.runtime.lastError,您应该检查它。

Note that this error is only raised when something bad doesoccur, and not when the API call finishes normally.

请注意,此错误仅在确实发生错误时才会引发,而不会在 API 调用正常完成时引发。

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 ... catchin your callback won't help. Since it's asynchronous, using tryaround the original API call won't help either.

并不真地; 它不是由您的代码引发的,而是由 Chrome API 中处理异步任务的清理代码引发的;因此try ... catch在您的回调中使用将无济于事。由于它是异步的,因此try围绕原始 API 调用使用也无济于事。

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.

您应该向回调添加逻辑,以按照 Chrome 期望的方式检查问题,并可能对其做出反应。

function(entry) {
  if(chrome.runtime.lastError) {
    // Something went wrong
    console.warn("Whoops.. " + chrome.runtime.lastError.message);
    // Maybe explain that to the user too?
  } else {
    // No errors, you can use entry
  }
}

As long as Chrome sees that you checkedthe value when there is an error (that is, evaluated it within your callback), the error will not be thrown.

只要 Chrome 发现您在出现错误时检查了该值(即在您的回调中对其进行评估),就不会抛出该错误。

回答by cxw

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()通话中抑制错误的方法,以防它对其他人有所帮助。代替

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

I used

我用了

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

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

void计算其操作数,然后返回undefined。我认为这段代码相当有效地记录了它的目的是忽略错误:)。

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

为简洁起见,ignore_error()可以从该调用中拉出并用于多个chromeAPI 调用,或者ignore_error可以省略名称。

UpdateIn ES6, you can use the shorter arrow functionsyntax:

更新在 ES6 中,您可以使用较短的箭头函数语法:

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

Tested in Chrome 64

在 Chrome 64 中测试

回答by ismnoiet

For this type of errors try catchwon't be helpful. Instead you can access the global variable chrome.runtime.lastErroras @xan mentioned in his awesome response above(by the way you should read it and upvote it!).

对于这种类型的错误try catch不会有帮助。相反,您可以chrome.runtime.lastError像@xan 在上面的精彩回复中提到的那样访问全局变量(顺便说一下,您应该阅读它并投票!)。

Now, here I provide an example that can be used:

现在,我在这里提供一个可以使用的示例:

function catchLastError(){
  if(chrome.runtime.lastError){
    console.log("error: ", chrome.runtime.lastError);
  }else{
    // some code goes here.
  }
}

chrome.fileSystem.chooseEntry(yourEntry,catchLastError);