Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS-Code won't pause debugger on exception inside Promise

Please consider the following code:

new Promise((resolve, reject) => {
   breaksomething() //won't pause
})
breaksomething() //pause as expected!

I am expecting my debugger to halt execution - because of an undefined function - at the line breaksomething() inside the promise... However I am only getting the following error output:

"ReferenceError: breaksomething is not defined"

(without pausing). Everywhere else the debugger is pausing as expected when an exception is encountered, the problem is only inside a Promise scope. I do have both All Exceptions and Uncaught Exceptions ticked under breakpoints.

I am using:
Visual Studio Code 1.17.2
Node 8.8.1
Inspector debugger

like image 755
Pierre Avatar asked Oct 27 '17 12:10

Pierre


3 Answers

UPDATE Per latest update on a known issue from Microsoft team it was a known issue in VS code.

You have to UNCHECK All Exceptions and Uncaught Exceptions in breakpoints settings in VS code in order for that to work

debugger works without exceptions

like image 166
Tatarin Avatar answered Sep 21 '22 14:09

Tatarin


After debugging I found that this is happening because Promise library is implement in Native code

So when you create a Promise, the function inside is actually called by the Native code and the exception goes back to it. This might be one of the reasons that it is not caught by VS debugger.

Is it impossible to do so? Well WebStorm from JetBrains is able to break on such exceptions also. Now how they do it and why VS Code is not able to do it, is beyond my understanding. It would be best to open a issue against VS code and refer that WebStorm does it. So it is technically possible to break on such exceptions

like image 30
Tarun Lalwani Avatar answered Sep 22 '22 14:09

Tarun Lalwani


I am still unclear why this issue is happening but I found a workaround. Just install bluebird and add the following in the initialisation code of your app:

global.Promise = require("bluebird");  

...this will override the default nodejs Promise and break as expected when "All exceptions" is ticked

like image 44
Pierre Avatar answered Sep 25 '22 14:09

Pierre