Errors raised in the content scripts context should be logged in the related tab's webconsole
Categories
(WebExtensions :: Developer Tools, defect, P3)
Tracking
(firefox110 fixed)
Tracking | Status | |
---|---|---|
firefox110 | --- | fixed |
People
(Reporter: rpl, Assigned: ochameau)
References
(Blocks 3 open bugs)
Details
(Whiteboard: [addon-debugging] [platform-rel-Wikipedia] )
Attachments
(5 files, 5 obsolete files)
Comment hidden (mozreview-request) |
Comment hidden (mozreview-request) |
Comment hidden (mozreview-request) |
Comment hidden (mozreview-request) |
Comment hidden (mozreview-request) |
Comment hidden (mozreview-request) |
Reporter | ||
Updated•7 years ago
|
Reporter | ||
Comment 8•7 years ago
|
||
Reporter | ||
Comment 9•7 years ago
|
||
Reporter | ||
Comment 10•7 years ago
|
||
Reporter | ||
Comment 11•7 years ago
|
||
Reporter | ||
Comment 12•7 years ago
|
||
Comment 13•7 years ago
|
||
mozreview-review |
Comment 14•7 years ago
|
||
Comment 15•7 years ago
|
||
mozreview-review |
Comment 16•7 years ago
|
||
mozreview-review |
Updated•7 years ago
|
Updated•7 years ago
|
Comment 17•7 years ago
|
||
mozreview-review |
Reporter | ||
Comment 18•7 years ago
|
||
Comment hidden (mozreview-request) |
Comment hidden (mozreview-request) |
Comment hidden (mozreview-request) |
Comment hidden (mozreview-request) |
Comment hidden (mozreview-request) |
Comment hidden (mozreview-request) |
Reporter | ||
Comment 25•7 years ago
|
||
mozreview-review-reply |
Reporter | ||
Comment 26•7 years ago
|
||
mozreview-review-reply |
Reporter | ||
Comment 27•7 years ago
|
||
mozreview-review-reply |
Reporter | ||
Comment 28•7 years ago
|
||
Updated•6 years ago
|
Reporter | ||
Updated•5 years ago
|
Reporter | ||
Comment 30•5 years ago
|
||
Comment 32•5 years ago
|
||
I think I am going to resurrect at least the patch in "Bug 1410932 - AutoJS::ReportException should report Content Script errors on the related DOM Window. ". That should cover a rather large amount of existing cases.
Reporter | ||
Comment 34•5 years ago
|
||
(In reply to Tom Schuster [:evilpie] from comment #32)
I think I am going to resurrect at least the patch in "Bug 1410932 - AutoJS::ReportException should report Content Script errors on the related DOM Window. ". That should cover a rather large amount of existing cases.
That would be great! Please feel free to steal the patch and change it as appropriate.
Comment 35•5 years ago
|
||
I might not get to this immediately, but I don't want to forget about it.
Comment 36•5 years ago
|
||
Comment 37•5 years ago
|
||
I rebased this patch, but I think the approach is not great. Something like throw "abc";
breaks it. It would be a lot better if this was actually handled by AutoJSAPI::ReportException
. I need to find out why that code is not used in this specific case.
Reporter | ||
Comment 38•5 years ago
|
||
(In reply to Tom Schuster [:evilpie] from comment #37)
I rebased this patch, but I think the approach is not great. Something like
throw "abc";
breaks it. It would be a lot better if this was actually handled byAutoJSAPI::ReportException
. I need to find out why that code is not used in this specific case.
yeah, I totally agree, changing AutoJSAPI::ReportException
would be way better and it should be easy enough.
e.g. we could likely check here in AutoJSAPI::ReportException
if the exception is coming from the script sandbox and report the error as coming from the inner window id of the window we set as the sandboxPrototype
.
how that sounds to you?
Comment 39•5 years ago
|
||
Yes that would basically be step two. Right now AutoJSAPI::ReportException
isn't invoked at all, we also need to fix mozilla::dom::Promise::ReportRejectedPromise
which is used instead when doing evalInSandbox
. We can probably use the same fix in both places.
Updated•5 years ago
|
Updated•5 years ago
|
Comment 40•5 years ago
|
||
Comment 41•5 years ago
|
||
I just submitted my approach for this. Right now we execute code through inject
in ExtensionContent.jsm. Because that is an async function at some point we will run Promise::ReportRejectedPromise
if executing e.g. evalInSandbox
threw an exception. This however means that the Promise is allocated in the BackstagePass global used to run the jsm. To find out the "correct" innerWindowID we look at the rejection value's global. This however only works if we threw some object, instead of a value like throw "abc";
. So this is sort-of a hack, but it does get is most of the way with very few changes in other code.
Updated•5 years ago
|
Updated•5 years ago
|
Comment 42•5 years ago
|
||
I updated the patch by using AutoEntryScript
in executeInGlobal
. This will report the pending exception when leaving the block containing the execute call. However this also means that executeInGlobal
now is infallible and doesn't propagate the error to the outside. I would need to make a similar change in evalInSandbox
.
I still feel like there must be a better way to do this :(
Updated•4 years ago
|
Updated•4 years ago
|
Comment 43•4 years ago
|
||
In bug 1696756 I added code to log uncaught promise rejections from content-scripts to the Webconsole.
Updated•3 years ago
|
Comment 45•3 years ago
|
||
Ran into this during development of the WikimediaDebug extension. There was an uncaught error in first line of a sendMessage callback in content_script.js, and this was not in any way accessible in DevTools.
Not via the "Inspect" context for the extension (which inspects the background.js process, as I understand it). And not via the main devtools console for one of the browser tabs.
I note that in Chrome these are reported to the console by default, and that if you try/catch the callback in its entirety that then calls console.log(error)
, it does show up, so general tracing and console messages do make their way through it seems.
Downstream issue: https://phabricator.wikimedia.org/T294335 (went unnoticed because it was only detectable in Chrome).
Comment 46•3 years ago
|
||
I always forget about this bug and then after trying everything and spending so much time debugging it I remember I should try to open it in Chrome and suddenly the console is full of errors...
I would really make content script development so much better if stupid mistakes were easy to spot :(
Comment 47•3 years ago
|
||
Bitten and lost much time as well. This makes for quite an unpleasant developer experience when working on WebExtensions.
I chased my tail assuming this was a React bug since it reported "The above error occurred..." when there was no actual exception reported above. Anecdotally, it doesn't look like everything gets to the Browser Console either.
The way I've been able to debug my content script is using "Pausing on exceptions" (which isn't always practical) and explicit debugger statements.
Comment 48•2 years ago
|
||
Same thing. I don't know how many times I've fallen into this bug trap. It makes developing with FF quite the chore, if your scripts silently die, you then waste half an hour to remember that FF doesn't show the errors, then you track down the offending line with countless "logs" and then you finally have to guess what the error in that line could actually be.
This bug has been reported 5 years ago and still has the status "NEW".
Comment 49•2 years ago
|
||
omg, I wish ff has bug sponsoring program, I could donate a bit for it been fixed.
Assignee | ||
Updated•2 years ago
|
Assignee | ||
Comment 50•2 years ago
|
||
So bug 1696756 fixed error report for all async code, like:
setTimeout(()=> throw Error("foo"));
(async () => {
throw Error("foo");
})();
Promise.reject("foo");
But, synchronous errors are still an issue.
What happens is that we evaluate the content script over there:
https://searchfox.org/mozilla-central/rev/9769b513e38ee4f5df9d5d1eff55ff7cdc8cbf81/toolkit/components/extensions/ExtensionContent.jsm#578-597
try {
for (let script of scripts) {
result = script.executeInGlobal(context.cloneScope); // <<========= THIS WILL THROW
}
if (this.matcher.jsCode) {
result = Cu.evalInSandbox(
this.matcher.jsCode,
context.cloneScope,
"latest",
"sandbox eval code",
1
);
}
} finally {
lazy.ExtensionTelemetry.contentScriptInjection.stopwatchFinish(
extension,
context
);
}
executeInGlobal
will throw. The try/finally won't handle the exception.
The exception will be considered as unhandled and will be processed by the following code:
https://searchfox.org/mozilla-central/rev/9769b513e38ee4f5df9d5d1eff55ff7cdc8cbf81/dom/promise/Promise.cpp#582-602
if (nsGlobalWindowInner* win = xpc::WindowGlobalOrNull(aPromise)) {
winForDispatch = win;
innerWindowID = win->WindowID();
} else if (nsGlobalWindowInner* win = xpc::SandboxWindowOrNull(
JS::GetNonCCWObjectGlobal(aPromise), aCx)) {
// Don't dispatch rejections from the sandbox to the associated DOM
// window.
innerWindowID = win->WindowID();
}
This is interesting as that's the new code introduced by bug 1696756, which fixes all promises created within the content script.
From there, unless we try to lookup for the original exception object's global, there isn't much we can do.
We should probably be doing something similar to comment 42.
It should probably be up to PrecompiledScript::executeInGlobal
to be throwing the exception right to the console with the right innerWindowID.
Assignee | ||
Updated•2 years ago
|
Assignee | ||
Comment 51•2 years ago
|
||
Assignee | ||
Comment 52•2 years ago
|
||
The attached patch is a rebase'n polished version of Tom's, with additional tests https://phabricator.services.mozilla.com/D70211
I made it so that executeInGlobal still throws when the content script throws.
So that we get two distinct error messages. One in the web console and another one in the browser console.
Now, I'm not really used to Spidermonkey codebase. I don't know what are the real consequences of using AutoEntryScript vs AutoRealm.
But I know that we want something exactly like its ReportException method:
https://searchfox.org/mozilla-central/rev/9769b513e38ee4f5df9d5d1eff55ff7cdc8cbf81/dom/script/ScriptSettings.cpp#480-557
Assignee | ||
Comment 53•2 years ago
|
||
Assignee | ||
Comment 54•2 years ago
|
||
While testing the first patch, I realized that syntax errors were following a completely different codepath, via ChromeUtils.compileScript
.
Now, in compileScript, there isn't much we can do to relate the exception to the web page.
It sounds like we ought to do something from ExtensionContent.inject and manually notify the web console.
I pushed another patch in that direction.
Comment 55•2 years ago
|
||
Similar bug on webRequest callback Extension console not logging error raised by webRequest callback function.
Not just content script.
Updated•2 years ago
|
Comment 56•2 years ago
|
||
For anyone who, like me, opened the Browser Console with Ctrl+Shift+J, and still didn't see any error messages there:
You have to switch the Browser Console Mode from "Parent process only" to "Multiprocess". Only there did I start seeing error messages from my content script (and also logs of event handlers of background script).
On Ubuntu Linux, I see the switch as a radio selector at the top of the browser console window.
Comment 57•2 years ago
|
||
The severity field for this bug is relatively low, S3. However, the bug has 5 duplicates.
:ochameau, could you consider increasing the bug severity?
For more information, please visit auto_nag documentation.
Comment 58•2 years ago
|
||
The last needinfo from me was triggered in error by recent activity on the bug. I'm clearing the needinfo since this is a very old bug and I don't know if it's still relevant.
Reporter | ||
Comment 59•2 years ago
|
||
Hey Kris,
would you mind to give the attached reviews another review pass? or if you are too busy at the moment, could you suggest us other peers that may take over the last review pass on the chrome-webidl and js/xpconnect parts of D157334?
Tomislav offered to cover the parts of the two reviews that can be reviewed and signed off from a WebExtensions peer.
Comment 60•2 years ago
|
||
Comment 61•2 years ago
|
||
bugherder |
https://hg.mozilla.org/mozilla-central/rev/91fcd1c0472a
https://hg.mozilla.org/mozilla-central/rev/8ef94baa9816
Updated•2 years ago
|
Description
•