Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js errors fail silently

I am jumping onto a node.js project where some errors fail silently and the app doesn't throw any errors unless I put it in a try..catch block. For example:

console.log(ok) // doesn't throw any errors during runtime; the api just fails silently.

try {
  console.log(ok)
} catch(e) {
  console.log(e)// throws ReferenceError: ok is not defined
}

Does anyone know why this might be?

Thank you in advance!

like image 803
Trung Tran Avatar asked Oct 20 '25 10:10

Trung Tran


1 Answers

Does anyone know why this might be?

Clearly, code in a containing scope is catching and suppressing errors. Which is an unfortunate, but common, practice.

Sadly you'll just have to root out that code, there's no real shortcut. You could run with --inspect (on recent versions of Node) and tell the debugger to stop on all exceptions, even caught ones, which may help you track them down some. But it's going to be tedious, I'm afraid.

One possible further thought: On only-slightly-older versions of Node, unhandled Promise rejections are not reported. So if that code is inside (say) a Promise executor (the function you pass new Promise) or a then or catch handler (or an async function), the throw is being converted to a promise rejection. The latest versions of Node report unhandled Promise rejections. (In the not-distant-future, they'll also terminate the Node process.)

like image 141
T.J. Crowder Avatar answered Oct 21 '25 22:10

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!