1

I think I dealt with this before while developing my Chrome extension, so now during the routine maintenance the same issue seems to come up again.

Can someone let me know why this piece of code:

    try
    {
        chrome.tabs.get(nTabID, function(tab)   //this is line 484 where the error happens
        {
            var tabUrl = '';

            try
            {
                tabUrl = tab.url;
            }
            catch(e)
            {
                //Failed to get tab URL -- mute it
            }

            if(tabUrl)
            {
                //Process it
            }
        });
    }
    catch(e)
    {
        //Failed to get tab for 'nTabID' -- mute it
    }

cannot prevent this error in the console:

enter image description here

Unchecked runtime.lastError while running tabs.get: No tab with ID: N

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • 1
    The error message is misleading because it was written by a C++ developer :-). All you need is to access `chrome.runtime.lastError` in the callback, see [Unchecked runtime.lastError when using Chrome API](//stackoverflow.com/a/28432087) – wOxxOm Apr 09 '19 at 09:37
  • @wOxxOm: Thanks. I'll try it. But that's a very weird way of handling exceptions. I'm just curious, how does C++ come into picture here? – c00000fd Apr 09 '19 at 09:42
  • You should not use `try...catch` with async Chrome API methods. Instead of this, check `chrome.runtime.lastError` in a method's callback. – hindmost Apr 09 '19 at 09:45
  • 3
    Possible duplicate of [Unchecked runtime.lastError when using Chrome API](https://stackoverflow.com/questions/28431505/unchecked-runtime-lasterror-when-using-chrome-api) – hindmost Apr 09 '19 at 09:46
  • C++ programmers implemented the extensions API and they obviously thought the error message is sufficiently self-explanatory, as it probably is for someone who knows the internals. – wOxxOm Apr 09 '19 at 09:54

1 Answers1

-2

This happens because of asynchronous code. Here is a repl that will help you understand. The below is standalone code, so you can play with it to understand. Uncommenting each of the errors bellow will trigger an exception differently.

// Mock of chrome.tabs.get, for illustration purposes
function chromeTabsGet(tabId, callback) {
    // v----------------- This error will be caught
    // throw Error();
    setTimeout(() => {
        // v------------- This error will not (just like the one you see)
        throw Error()
        callback({url: 2})
    }, 0)
}


try
{
    chromeTabsGet(0, function(tab)   //this is line 484 where exception happens
    {
        var tabUrl = '';

        try
        {
            tabUrl = tab.url;
        }
        catch(e)
        {
            // Failed to get tab URL -- mute it
        }

        if(tabUrl)
        {
            // Process it
        }
    });
}
catch(e)
{
    // Failed to get tab for 'nTabID' -- mute it
}

As you see, chrome.tabs.get (illustrated as chromeTabsGet here) can throw in two ways. If it throws inside the function itself, then you will catch it right away in your outer-most try-catch. However, if the function dispatches one (or more) asynchronous events, then those events go out of the main control flow, are put inside the event loop queue and are dispatched later. For this reason, they are not run inside your try-catch anymore, because that code already finished executing.

One way to go around this is to use await and async instead of callbacks, but browsers do not support it yet (can do that in Node or by using Babel though)

As @wOxxOm suggested, for your particular problem, have a look at Unchecked runtime.lastError when using Chrome API

Edit: I am in fact a bit outdated on my browser knowledge. Most modern browsers do in fact support await and async, and in your case, you already use chrome, so you're good.

Andrei Cioara
  • 3,404
  • 5
  • 34
  • 62
  • This answer isn't applicable here because the problem is not an exception (an incorrect term was used in the description), but an internal mechanism of chrome extensions API as explained in the duplicate topic linked above. – wOxxOm Apr 09 '19 at 10:04
  • The question is "Why I can't catch exceptions". The context is irrelevant. My answer gives an example where exceptions cannot be caught. – Andrei Cioara Apr 09 '19 at 11:27
  • Well, that's certainly an interesting approach: answering based on words out of context. – wOxxOm Apr 09 '19 at 11:30