Capture error when adding chrome.webRequest.onBeforeRequest.addListener?

940 views
Skip to first unread message

David Hirmes

unread,
Jan 14, 2017, 5:21:09 PM1/14/17
to Chromium-Extensions-Announce
I'm blocking certain sites using:

chrome.webRequest.onBeforeRequest.addListener( blockSites, { urls: blockedSitesArray }, ['blocking'] );

but if there is a syntax error in the blockedSitesArray (for instance if an item is included without a trailing slash, as in "https://www.google.com" instead of "https://www.google.com/") an error occurs and the blocking fails with this message:

"Unchecked runtime.lastError while running webRequestInternal.addEventListener: 'http://www.cnn.com' is not a valid URL pattern."

Since this array of URL patterns is created from user input, I can't guarantee it will be valid.  Is there a way to at least capture the "Unchecked runtime.lastError" so I can warn the user of the error?

PhistucK

unread,
Jan 15, 2017, 12:28:00 AM1/15/17
to David Hirmes, Chromium-Extensions-Announce
Yep, I think you can check chrome.runtime.lastError right after the addListener call.


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/bfcf2d6c-0226-477f-a5ab-cc9f0b4ac320%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

David Hirmes

unread,
Jan 17, 2017, 7:42:55 PM1/17/17
to Chromium-Extensions-Announce, hir...@gmail.com
Unfortunately, that doesn't seem to work, perhaps because the addListener is done asynchronously?

When I do:

 chrome.webRequest.onBeforeRequest.addListener( blockSites, { urls: blockedSitesArray }, ['blocking'] );

 console
.log("error?");
 console
.log(chrome.runtime.lastError);

I get:

error?
undefined
Unchecked runtime.lastError while running webRequestInternal.addEventListener: '*://*.cnn.com' is not a valid URL pattern.



On Sunday, January 15, 2017 at 12:28:00 AM UTC-5, PhistucK wrote:
Yep, I think you can check chrome.runtime.lastError right after the addListener call.


PhistucK

On Sun, Jan 15, 2017 at 12:21 AM, David Hirmes <hir...@gmail.com> wrote:
I'm blocking certain sites using:

chrome.webRequest.onBeforeRequest.addListener( blockSites, { urls: blockedSitesArray }, ['blocking'] );

but if there is a syntax error in the blockedSitesArray (for instance if an item is included without a trailing slash, as in "https://www.google.com" instead of "https://www.google.com/") an error occurs and the blocking fails with this message:

"Unchecked runtime.lastError while running webRequestInternal.addEventListener: 'http://www.cnn.com' is not a valid URL pattern."

Since this array of URL patterns is created from user input, I can't guarantee it will be valid.  Is there a way to at least capture the "Unchecked runtime.lastError" so I can warn the user of the error?

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.

PhistucK

unread,
Jan 18, 2017, 12:03:50 PM1/18/17
to David Hirmes, Chromium-Extensions-Announce
While it runs asynchronously in a way, I would still expect that to populate chrome.runtime.lastError, because those checks are supposed to be synchronous (exactly for this purpose).
This seems like a bug, to me.

"Error handling

If you try to register an event with invalid arguments, then a JavaScript error will be thrown, and the event handler will not be registered. If an error is thrown while an event is handled, or if an event handler returns an invalid blocking response, an error message is logged to your extension's console and the handler is ignored for that request."

While this means it is kind of by design, I think it is inconsistent with the rest of the extension system (which uses lastError for those).

Does onErrorOccurred fire in that case by any chance? If so, there is, at least, a workaround.

If not, you can search crbug.com for an existing issue and star it. If you cannot find one, file a new issue using the "New issue" link on the same page.
Please, do not add a "+1" or "Me too" or "Confirmed" (or similar) comment. It just wastes the time of Chrome engineers and sends unnecessary e-mails to all of the people who starred the issue.

You can reply with a link to the found or created issue and might get triaged (and fixed) faster.

Thank you.



PhistucK

On Wed, Jan 18, 2017 at 2:42 AM, David Hirmes <hir...@gmail.com> wrote:
Unfortunately, that doesn't seem to work, perhaps because the addListener is done asynchronously?

When I do:

 chrome.webRequest.onBeforeRequest.addListener( blockSites, { urls: blockedSitesArray }, ['blocking'] );
 console
.log("error?");
 console
.log(chrome.runtime.lastError);

I get:

error?
undefined
Unchecked runtime.lastError while running webRequestInternal.addEventListener: '*://*.cnn.com' is not a valid URL pattern.



On Sunday, January 15, 2017 at 12:28:00 AM UTC-5, PhistucK wrote:
Yep, I think you can check chrome.runtime.lastError right after the addListener call.


PhistucK

On Sun, Jan 15, 2017 at 12:21 AM, David Hirmes <hir...@gmail.com> wrote:
I'm blocking certain sites using:

chrome.webRequest.onBeforeRequest.addListener( blockSites, { urls: blockedSitesArray }, ['blocking'] );

but if there is a syntax error in the blockedSitesArray (for instance if an item is included without a trailing slash, as in "https://www.google.com" instead of "https://www.google.com/") an error occurs and the blocking fails with this message:

"Unchecked runtime.lastError while running webRequestInternal.addEventListener: 'http://www.cnn.com' is not a valid URL pattern."

Since this array of URL patterns is created from user input, I can't guarantee it will be valid.  Is there a way to at least capture the "Unchecked runtime.lastError" so I can warn the user of the error?

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.

Tamer

unread,
Feb 27, 2017, 5:30:32 AM2/27/17
to Chromium-Extensions-Announce, hir...@gmail.com
We have exact same issue with our extension, where we let users enter filter pattern for purpose of recording traffic for load testing.
Issue is when user enters wrong filter pattern, there is no way we can report this error to the user.

Console log throws following error:

Unchecked runtime.lastError while running webRequestInternal.addEventListener: '<bad url>' is not a valid URL pattern.



We tried to use onErrorOccurred but it never gets called for this specific error.
Also checking for chrome.runtime.lastError inside WebRequest callback doesn't supress the error (it even doesn't reach the callback since error happens before).

As a workaround I have created small JS code that validates Match Patterns per documentation: https://developer.chrome.com/extensions/match_patterns



PhistucK



PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

Tamer Zoubi

unread,
Feb 27, 2017, 6:34:23 AM2/27/17
to Chromium-Extensions-Announce, hir...@gmail.com
Reply all
Reply to author
Forward
0 new messages