How to handle chrome.debugger.attach errors?

1k Views Asked by At

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 ?

1

There are 1 best solutions below

0
Thomas Mueller On

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