Catching Fatal Errors with Node.js child_process

By  on  

I'm relatively new to hardcore Node.js hacking so I'm seeing all sorts of lovely new errors that I have no clue how to solve when I initially see them.  To this point I've managed to keep a smile on my face while trying to fix these errors, a quality I quite enjoy about myself.  One of the recent errors I encountered was with child_process, whereby an error would occur within an execSync command and the entire app would brick; not even  a try/catch would save me.  I did find a solution, however.

The JavaScript

The best way to catch errors without letting your app brick is by using the process' spawn (or in this case spawnSync) method:

var childProcess = require('child_process');

var commitMessage = (function() {
	var spawn = childProcess.spawnSync('git', ['log', '--format=%B', '-n', '1']);
	var errorText = spawn.stderr.toString().trim();

	if (errorText) {
	  console.log('Fatal error from `git log`.  You must have one commit before deploying.');
	  throw new Error(errorText);
	}
	else {
	  return spawn.stdout.toString().trim();
	}
})();

With this method you can check the stderr buffer first; if there's a String from it you know it errored out, if no error text then the process was clean!

Recent Features

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

Incredible Demos

  • By
    Web Notifications API

    Every UI framework has the same set of widgets which have become almost essential to modern sites: modals, tooltips, button varieties, and notifications.  One problem I find is each site having their own widget colors, styles, and more -- users don't get a consistent experience.  Apparently the...

  • By
    Making the Firefox Logo from HTML

    When each new t-shirt means staving off laundry for yet another day, swag quickly becomes the most coveted perk at any tech company. Mozilla WebDev had pretty much everything going for it: brilliant people, interesting problems, awesome office. Everything except a t-shirt. That had to change. The basic...

Discussion

  1. there is also a spawn.status property that holds the exit code. A check for spawn.status !== 0 might be more reliable in cases when there no error message…

  2. eliranmal

    also, spawn.stderr.toString() is not safe, as stderr may be undefined.
    you’re better off just wrapping it with the String constructor:

    String(spawn.stderr)
  3. caoyy

    Perhaps it is more elegant to use status to judge whether the execution was successful or not.

    const childProcess = require('child_process');
    
    var result = (() => {
      const { stderr, stdout, status } = childProcess.spawnSync('npm', ['install']);
    
      if (status !== 0) {
        const errorText = stderr.toString();
        console.log('Fatal error from npm install.');
    
        throw new Error(errorText);
      }
      return stdout.toString();
    })();
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!