espruino / Espruino

The Espruino JavaScript interpreter - Official Repo
http://www.espruino.com/
Other
2.73k stars 741 forks source link

Loss of errors when executing require() #2508

Closed i-terezie closed 1 month ago

i-terezie commented 1 month ago

During debugging, I found that errors are ignored when executing code run via require(). More precisely, they simply lead to the termination of code execution without any indication of this. This problem was still in version 2.21. In version 2.22 it is still there. Below is an example file Test1.js . There are 3 places in it where errors are generated. Of these, error1 and error3 cause execution to stop without any messages, and error2 outputs a message via process.on('uncaughtException'. The launch is performed via the require("Test1.js ").start() A commented try allows you to push an error into the log.

exports.start = function () {
  console.log("Start Test1.10")
  EXT0.pinOn(0)// error1: undefine EXT0

  console.log("Start Test1.10(1)")
};
//try{
console.log("Start Test1.10(2)")

setInterval(function() {
  console.log("Start Test1.10(5)")
  on = !on;// error2: undefine on
  //LED1.write(on);
  console.log("Start Test1.10(6)")

  //console.message({testMsg:"test"})
}, 3500);

EXT0.pinOn(0)// error3: undefine EXT0
console.log("Start Test1.10(3)")
//}catch(e)
//{
// console.exception(e)
//}
mariusGundersen commented 1 month ago

You can't out a setTimeout inside a try catch and expect it to catch. Try putting the try catch inside the setTimeout

gfwilliams commented 1 month ago

How are you trying to execute this?

I just tried here - both with:

Modules.addCached("mod",function() {
exports.start = function () {
  console.log("Start Test1.10")
  EXT0.pinOn(0)// error1: undefine EXT0

  console.log("Start Test1.10(1)")
};
//try{
console.log("Start Test1.10(2)")

setInterval(function() {
  console.log("Start Test1.10(5)")
  on = !on;// error2: undefine on
  //LED1.write(on);
  console.log("Start Test1.10(6)")

  //console.message({testMsg:"test"})
}, 3500);

EXT0.pinOn(0)// error3: undefine EXT0
console.log("Start Test1.10(3)")
//}catch(e)
//{
// console.exception(e)
//}
});

require("mod").start();

which is what the IDE does, and by writing a library to a Bangle and then requiring it, and it all seems to get reported ok and work as expected:

Start Test1.10(2)
Uncaught ReferenceError: "EXT0" is not defined
 at line 4 col 5 in mod
EXT0.pinOn(1)
    ^
Start Test1.10(5)
Uncaught ReferenceError: "on" is not defined
 at line 3 col 53 in mod
...og("Start Test1.10(5)")on=!on;console.log("Start Test1.10(6)"...
                              ^
in function called from system
Start Test1.10(5)
Uncaught ReferenceError: "on" is not defined
 at line 3 col 53 in mod
...og("Start Test1.10(5)")on=!on;console.log("Start Test1.10(6)"...
                              ^
in function called from system
>require("mod").start()
Start Test1.10
Uncaught ReferenceError: "EXT0" is not defined
 at line 1 col 52 in mod
...e.log("Start Test1.10")EXT0.pinOn(0)console.log("Start Test1.10(1)"...
                              ^
in function "start" called from line 1 col 22
require("mod").start()
                     ^
gfwilliams commented 1 month ago

I changed EXT0.pinOn(0) to EXT0.pinOn(1) so it was obvious which line was causing the error

But to me this seems like it's working fine

i-terezie commented 1 month ago

I found an error in my code. She ended up in process.on('uncaughtException', function(e). Thanks for the quick response.