4

I have a Node application which defines some modules then starts an Express server. The problem is, sometimes the Express server doesn't start and I don't see any errors.

When this happens, I scan my git diff for potential syntax errors and fix them. This resolves the problem, and my server starts again.

The thing is, I'm not sure why these Syntax errors are failing silently.

I'll give an example:

  module.exports = function(server){
    this.register = function (params) {
      return this.Models.User.register(user)
      } // SYNTAX ERROR - extra closing bracket
    }
    return this
  }

When I removed the extra bracket, the server started again, but I would have thought I'd see a Syntax error raised instead of having to blindly hunt for bugs.

Some more context: My main.js file defines a function which returns a promise. The promise is invoked by running the script directly (nodejs main.js).

A lot of the code in this app is written with Promises. Would this have the effect of squelching errors? How can I ensure that errors are logged regardless of the scope they occur in?

max pleaner
  • 26,189
  • 9
  • 66
  • 118
  • Hi Max, I would recommend Bluebird.js for a promise library with awesome error support. If you're using es6 Promises, on the other hand, then you will have silent errors unless you explicitly call .catch on that promise chain. I suspect you aren't catching any of these errors, and as such, they are getting lost in the promise chain. – Dustin Stiles Jun 06 '16 at 22:52
  • @dustin-stiles, I am calling "catch" but these aren't intentional errors - they are syntax errors, so I'm not explicitly calling `throw` with them or passing them to a `reject` function. Thanks for the recommendation to try Bluebird. – max pleaner Jun 06 '16 at 23:14
  • Hmm, that's odd. Regardless of the error type, it should be caught in the next .catch in the chain, even syntax errors. I hope you figure it out! – Dustin Stiles Jun 07 '16 at 00:18
  • 2
    Seeing syntax errors has NOTHING to do with promises. A syntax error like the one you show should be thrown when the module is first loaded and parsed before any code is actually run and should show on stderr unless you've suppressed stderr or suppressed general process exceptions somehow. So, as your question is currently written, you are looking in the wrong place. – jfriend00 Jun 07 '16 at 00:18
  • Do you have anything like this is the start function of your app, max? `process.on('uncaughtException', handleError);` – Josh Beam Jun 07 '16 at 00:19
  • 1
    Also, it sounds like you would benefit from some basic unit tests before trying to run your server. Unit tests will uncover syntax errors immediately since the code won't even parse, much less run. – jfriend00 Jun 07 '16 at 00:21
  • @JoshBeam no i dont – max pleaner Jun 07 '16 at 02:01
  • Are you using an auto-reload mechanism? I suspect it may be culprit here. – Quentin Roy Jun 07 '16 at 02:08
  • @Roque no i am not – max pleaner Jun 07 '16 at 02:10
  • I think a little bit more info/code is required to solve your case.. Your problem should not happen. – Quentin Roy Jun 07 '16 at 02:14
  • 1
    There is no syntax error in the line where you pointed it out. This bracket closes the `register` callback, the next one closes the `module.exports = { ...` and then you `return this` and then you put unnecessary `}` at the end of the file :) This ain't no Python, sir, indentation doesn't matter. – Azamantes Jun 07 '16 at 12:40

1 Answers1

2

You can start logging unhandled promise rejections with:

process.on("unhandledRejection", (err) => {
   console.error(err); 
});

We added this hook about a year ago. I have plans to make logging or throwing the default behavior, I apologize for taking so long.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504