0

I'm developing a Express + PostgreSQL app and I want to implement a global error handler system. Now I want to caught all uncaught exceptions from my entire project. Server is running with nodemon.

A code like this console.log(x) should give a custom error that I created because x is not defined;

I'm putting process.on('uncaughtException', () => {}) in my server.js file, located in root directory and inserting error with console.log(x) in app.js that is also in root directory folder.

Here is my server.js code:

import './src/config/dotenv.js';
import app from './app.js';

process.on('uncaughtException', (err) => {
  console.log(err.name, err.message);
  console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
  process.exit(1);
});

const port = 3000;

const server = app.listen(port, () => {
  console.log(`Server running on port ${port}...`);
});

process.on('unhandledRejection', (err) => {
  console.log(err.name, err.message);
  console.log('UNHANDLED REJECTION! 💥 Shutting down...');
  server.close(() => {
    process.exit(1);
  });
});

This is what I tried:

  1. I already tried to put uncaughtException handler on top, before all imports, but that's not working;
  2. And problem I also not in nodemon;
  3. I don't have any others uncaughtExceptions handlers in other files;
  4. console.log(x) error is not located in asynchronous code, I understand that this handler only caught synchronous code;
1
  • Please, provide stackoverflow.com/help/mcve for the problem that can reproduce it. It's not shown where console.log(x) is. "I already tried to put uncaughtException handler on top, before all imports" - import statements are hoisted, so you can't put it on top this way, this could be a possible cause Commented Mar 31 at 12:55

2 Answers 2

0

looks like your uncaughtException handler isn't catching the error because nodemon restarts the server before the handler runs.

Try running your server with plain node: node server.js

If this works, then nodemon is the issue. You can prevent it from restarting on uncaught exceptions by adding this

to nodemon.json

{ "restartable": "rs", "ignore": ["node_modules/**"], "env": { "NODE_ENV": "development" }, "ext": "js,json" }

another thing to check is whether console.log(x) runs before process.on('uncaughtException') is set up. If app.js is imported before the handler, the error might occur before it's caught.

Try putting console.log(x) inside server.js after the handler and see if it works.

Sign up to request clarification or add additional context in comments.

3 Comments

If putting console.log(x) inside server.js after the handler, it is working, I'm getting my custom error, but inside app.js is not working, error is appearing in console, but not in the same way as in server.js. And also I already tried to run 'node server.js', but also not working, error is not custom.
That makes sense. since app.js is being imported before the uncaughtException handler in server.js, the error is happening before the handler is set up. try moving the process.on('uncaughtException', …) to the very top of server.js, before importing anything. process.on('uncaughtException', (err) => { console.log(err.name, err.message); console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...'); process.exit(1); }); import './src/config/dotenv.js'; import app from './app.js'; this ensures the handler is ready before anything else runs.
already tried it, but not working.
0

Here is the solution. Before that I was only using CommonJS syntax, but this project started with ES6 syntax. There are some difference, ES6 is not as global as CommonJS.

Solution: I removed uncaught exception from server.js file and created a separate uncaughtException.js file and just imported in my server.js like this:

uncaughtException.js

process.on('uncaughtException', (err) => {
  console.log(err.name, err.message);
  console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
  process.exit(1);
});

server.js

import './src/utils/uncaughtException.js';

And that's it! Do this when some variables are not global when using ES6.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.