0

In my extension I'm trying to attach debugger for given tabId, Here is my code

if(!attachedTabIds.includes(tabId)) {
    chrome.debugger.attach({ tabId: tabId }, version, function(){
        ...
    })
}

So from time to time I'm getting this error

enter image description here

I tried to wrap it with try/catch block but it didn't work.

try {
    chrome.debugger.attach({ tabId: tabId }, version, function(){
        ...
    })
} catch(e){
    console.log('e', e)
}

SO how can I handle this error ?

Armen Stepanyan
  • 1,618
  • 13
  • 29

1 Answers1

1

You handle the error by checking the value of chrome.runtime.lastError in the callback function that you passed to chrome.debugger.attach()

if (!attachedTabIds.includes(tabId)) {
    chrome.debugger.attach(
        {tabId: tabId},
        version,
        function() {
            if (chrome.runtime.lastError) {
                console.log("Error");
                console.log(chrome.runtime.lastError);
            }
            else {
                console.log("Success");
            }
        }
    );
}

https://developer.chrome.com/docs/extensions/reference/runtime/#property-lastError

lastError

This will be defined during an API method callback if there was an error

Thomas Mueller
  • 514
  • 1
  • 4
  • 10