How Promises Change Post‑Mortem Debugging in Node.js Servers

This article explains how using JavaScript Promises in a Node.js HTTP server affects error handling, crash behavior, and core‑dump generation, contrasting a traditional callback server with a Promise‑based one and offering practical debugging options for post‑mortem analysis.

Node Underground
Node Underground
Node Underground
How Promises Change Post‑Mortem Debugging in Node.js Servers

Promises enable reuse of client‑side and server‑side code, but error handling and debugging differ between the two environments.

On the client we usually hide errors from users, while on the server we prefer a fail‑fast approach, quickly restarting the process and preserving information for post‑mortem analysis.

What limitations do Promises introduce for post‑mortem debugging?

Server without Promise

'use strict';
const Http = require('http');
const server = Http.createServer((req, res) => {
  let result = '';
  req.on('data', (data) => {
    result += data;
  });
  req.once('end', () => {
    const obj = JSON.parse(result);
    res.writeHead(200);
    res.end(obj.foo.bar);
  });
});
server.listen(8080, () => {
  console.log('listening at http://localhost:8080');
});

This simple server crashes when receiving a request such as:

curl -X POST http://localhost:8080 -d '{"Hi": "world"}'

The crash log shows a TypeError because obj.foo.bar is undefined:

$ node server.js
listening at http://localhost:8080
/server.js:14
    res.end(obj.foo.bar);
                 ^

TypeError: Cannot read property 'bar' of undefined
    at IncomingMessage.req.once (/server.js:14:20)
    ...

Running Node with the --abort-on-uncaught-exception flag forces a core dump on uncaught exceptions:

$ node --abort-on-uncaught-exception server.js
listening at http://localhost:8080
Uncaught TypeError: Cannot read property 'bar' of undefined
FROM
IncomingMessage.req.once (/server.js:14:20)
...
Illegal instruction: 4 (core dumped)
Note: If no core file appears, check ulimit -c ; if it is 0, set ulimit -c unlimited . On macOS core files are stored in /cores/ as core.${PID} .

Server with Promise

'use strict';
const Http = require('http');
const server = Http.createServer((req, res) => {
  const promise = new Promise((resolve, reject) => {
    let result = '';
    req.on('data', (data) => {
      result += data;
    });
    req.once('end', () => {
      const obj = JSON.parse(result);
      resolve(obj);
    });
  });

  promise.then((obj) => {
    res.writeHead(200);
    res.end(obj.foo.bar);
  }).catch((reason) => {
    res.writeHead(500);
    res.end();
  });
});
server.listen(8080, () => {
  console.log('listening at http://localhost:8080');
});

The Promise version catches request‑parsing errors and returns a 500 response. An extended version logs the error and aborts the process:

'use strict';
const Http = require('http');
const server = Http.createServer((req, res) => {
  const promise = new Promise((resolve, reject) => {
    let result = '';
    req.on('data', (data) => {
      result += data;
    });
    req.once('end', () => {
      const obj = JSON.parse(result);
      resolve(obj);
    });
  });

  promise.then((obj) => {
    res.writeHead(200);
    res.end(obj.foo.bar);
  }).catch((reason) => {
    res.writeHead(500);
    res.end();
    console.error(reason);
    process.abort();
  });
});
server.listen(8080, () => {
  console.log('listening at http://localhost:8080');
});

Using Promises changes how crashes are reported: instead of an uncaught exception causing a core dump, the server can handle the error gracefully or deliberately abort, producing a core dump only when process.abort() is invoked.

For the full discussion, see the original article at https://www.joyent.com/blog/post-mortem-debugging-and-promises.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Node.jsError HandlingPost-mortem debugging
Node Underground
Written by

Node Underground

No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.