4

I am trying to open a window by Chrome extension by browser action.

var wid = null;
chrome.windows.get(wid, function(chromeWin) {
    chrome.windows.create({'url': 'https://google.com'}, function(chromeWin) {
            wid = chromeWin.id;
        }
    );
});

To prevent multiple copies of the Window, I am trying to check window id. But if used first time, understandably, it throws error because wid is null.

Error: Invocation of form windows.get(null, function) doesn't match definition windows.get(integer windowId, optional object getInfo, function callback)

I tried to use try..catch block and in catch I am handling "wid is null" case.

try {
    var wid = null;
    chrome.windows.get(wid, function(chromeWin) {
        chrome.windows.create({'url': 'https://google.com'}, function(chromeWin) {
                wid = chromeWin.id;
            }
        );
    });
}
catch(error) {
    chrome.windows.create({'url': 'https://google.com'}, function(chromeWin) {
            wid = chromeWin.id;
        }
    );      
}

But try..catch is not catching the "wid is null" case. I know if clause may help for my experiment but I want to learn why try behaves this way.

Why is try..catch not caching the error and how can I open windows without copies in Chrome?

Xan
  • 74,770
  • 16
  • 179
  • 206
Nico Neill
  • 113
  • 2
  • 8
  • 1. You can check for `null` **before** invoking the function 2. You have two variables `wid` and `winId`, so either the code is incomplete or you have a typo. – wOxxOm Dec 14 '15 at 00:44
  • thanks. it was indeed typo. if clauses help but it makes the code complicated. try and catch seems to be neat but somehow does not work for this code listing. – Nico Neill Dec 14 '15 at 02:05
  • There's nothing in your code that would stop a duplicate appearing either. – Xan Dec 14 '15 at 10:42
  • Okay, now that I read your question, it actually makes sense. Edited to highlight / fix typo, upvoted and gonna answer. – Xan Dec 14 '15 at 10:46

1 Answers1

3

The answer is actually going to be rather boring: it does work for call-validation performed by Chrome API, since that happens synchronously:

try {
  var wid = null;
  chrome.windows.get(wid, function(chromeWin) {
    console.log("This should never be visible.");
  });
} catch(e) {
  console.log("Error caught!");
  console.warn(e);
}

This catches the error (and shows it as a warning instead). So your original problem must be elsewhere (another part of code, not reloading the code properly, something like that).


My original point was that some errors happen inside Chrome's async processing. This is not this error, but rather errors reported with chrome.runtime.lastError. If you do not check that value, and there is an error, it raises an exception that cannot be caught, because it happens after the original invocation and outside your callback.

More information about that here, but again, that does not apply in your case.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206