Silence irrelevant browser.runtime.sendMessage error#131
Silence irrelevant browser.runtime.sendMessage error#131lydell wants to merge 1 commit intomozilla:masterfrom
Conversation
|
@lydell Thanks for reporting and looking into this tricky issue, I'm a bit "on the fence" about silencing the errors (because it may theoretically make harder to find real issues that may trigger the same error message, and also on the other side if the error message changes we will not notice it and we will start to produce noise again), but I agree that these errors are going to be confusing given that the developers are not going to be in charge of passing the callbacks explicitly. I'm going to give a try to your test extension asap, so that I can form a more precise opinion on the issue (and it may be my initial concerns are not really motivated). In the meantime, as an initial feedback on the change:
|
|
@rpl I wouldn't worry about false positives. This error message is only generated when the In Chrome, Without |
rpl
left a comment
There was a problem hiding this comment.
So, I've been finally able to give this a deeper look (also thanks @Rob--W for the links to the relevant Chrome sources,
it has been very helpful) and it turned out that this actually fix an incompatibility with the behavior that the same scenario has on Firefox: when none of the listeners sent any response, the promise returned by sendMessage is resolved to undefined.
and so it is not only a matter of how annoying or irrelevant is the error message,
it is actually a fix for an incompatibility :-)
@lydell Thanks a lot for catching this, this was a very very subtle incompatibility.
Anyway this looks good to me and there are only some very small nits and tweaks that it would be nice to apply before merging this pull request.
Besides that, as an additional test and proof of the compatibility of this behavior on both Firefox and Chrome,
it would be great to land this change with also a test case for it in the integration tests.
Something like the following diff should be enough to add this the existing messaging test cases:
diff --git a/test/fixtures/runtime-messaging-extension/background.js b/test/fixtures/runtime-messaging-extension/background.js
index c090a8e..52a485b 100644
--- a/test/fixtures/runtime-messaging-extension/background.js
+++ b/test/fixtures/runtime-messaging-extension/background.js
@@ -45,6 +45,9 @@ browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
case "test - sendMessage with listener callback throws":
throw new Error("listener throws");
+ case "test - sendMessage and no listener answers":
+ return undefined;
+
default:
return Promise.resolve(
`Unxpected message received by the background page: ${JSON.stringify(msg)}\n`);
@@ -52,6 +55,10 @@ browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
});
browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
+ if (msg === "test - sendMessage and no listener answers") {
+ return undefined;
+ }
+
setTimeout(() => {
sendResponse("second listener reply");
}, 100);
diff --git a/test/fixtures/runtime-messaging-extension/content.js b/test/fixtures/runtime-messaging-extension/content.js
index 5773af2..64d5d6c 100644
--- a/test/fixtures/runtime-messaging-extension/content.js
+++ b/test/fixtures/runtime-messaging-extension/content.js
@@ -75,3 +75,8 @@ test("sendMessage with listener callback throws", async (t) => {
t.equal(err.message, "listener throws", "Got an error with the expected message");
}
});
+
+test("sendMessage and no listener answers", async (t) => {
+ const reply = await browser.runtime.sendMessage("test - sendMessage and no listener answers");
+ t.equal(reply, undefined, "Got undefined as a reply a expected");
+});
| removed from the specs (See | ||
| https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/onMessage) | ||
| `.replace(/\s+/g, " ").trim(); | ||
| const CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE = |
There was a problem hiding this comment.
Nit, do you mind to move this one at line 10 (so that two const are going to be in alphabetic order).
| const wrappedSendMessageCallback = ({reject, resolve}, reply) => { | ||
| if (chrome.runtime.lastError) { | ||
| reject(chrome.runtime.lastError); | ||
| // Passing a callback to `chrome.runtime.sendMessage` |
There was a problem hiding this comment.
Let's trim this inline comment a bit, and we should actually mention that this is needed to match the behavior on Firefox, e.g. something like the following could be enough:
// Detect when none of the listener replied to the sendMessage call and resolve
// the promise to undefined as in Firefox.
// See https://github.com/mozilla/webextension-polyfill/issues/130 | }); | ||
| }); | ||
|
|
||
| it("ignores a certain chrome.runtime.lastError", () => { |
There was a problem hiding this comment.
How about it("resolves to undefined when no listeners reply"?
| const fakeChrome = { | ||
| runtime: { | ||
| lastError: { | ||
| message: "The message port closed before a response was received.", |
There was a problem hiding this comment.
let's add a 'single line' inline comment which mention that this is the error defined as CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE in the polyfill internals.
| }; | ||
|
|
||
| return setupTestDOMWindow(fakeChrome).then(window => { | ||
| fakeChrome.runtime.sendMessage |
There was a problem hiding this comment.
Nit, do you mind to move this out of this callback? right after const fakeChrome = {...}; at line 252.
| .onFirstCall().callsArgWith(1, ["res1", "res2"]); | ||
|
|
||
| return window.browser.runtime.sendMessage("some_message").then( | ||
| (...args) => deepEqual(args, [undefined], "The error was ignored") |
There was a problem hiding this comment.
Nit, some small tweak to the assertions:
const promise = window.browser.runtime.sendMessage("some_message");
ok(fakeChrome.runtime.sendMessage.calledOnce, "sendMessage has been called once");
return promise.then(reply => {
deepEqual(reply, undefined, "sendMessage promise should be resolved to undefined");
});|
Thanks for confirming/explaining what’s going and for the review, I appreciate it! I think it’ll be much easier if you do this yourself (when you get the time). |
Fixes #130.
See the comments in the code.
You can test here (by changing to
browser-polyfill-fork.jsinmanifest.json): https://github.com/lydell/webextension-polyfill-messaging-issue