Skip to content

Commit

Permalink
reimplement errname lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Apr 30, 2016
1 parent 1d8f3a4 commit 9c3a87e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var npmRunPath = require('npm-run-path');
var isStream = require('is-stream');
var _getStream = require('get-stream');
var pathKey = require('path-key')();
var errname = require('./lib/errname');
var TEN_MEBIBYTE = 1024 * 1024 * 10;

function handleArgs(cmd, args, opts) {
Expand Down Expand Up @@ -149,10 +150,7 @@ module.exports = function (cmd, args, opts) {
// err.killed = spawned.killed || killed;
err.killed = spawned.killed;

// TODO: child_process applies the following logic for resolving the code:
// var uv = process.bind('uv');
// ex.code = code < 0 ? uv.errname(code) : code;
err.code = code;
err.code = code < 0 ? errname(code) : code;
}

err.stdout = stdout;
Expand Down
38 changes: 38 additions & 0 deletions lib/errname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
// The Node team wants to deprecate `process.bind(...)`.
// https://github.com/nodejs/node/pull/2768
//
// However, we need the 'uv' binding for errname support.
// This is a defensive wrapper around it so `execa` will not fail entirely if it stops working someday.
//
// If this ever stops working. See: https://github.com/sindresorhus/execa/issues/31#issuecomment-215939939 for another possible solution.
var uv;

try {
uv = process.binding('uv');
if (typeof uv.errname !== 'function') {
throw new Error('uv.errname is not a function');
}
} catch (e) {
console.error('execa/lib/errname: unable to establish process.binding("uv")', e);
uv = null;
}

function errname(uv, code) {
if (uv) {
return uv.errname(code);
}

if (!(code < 0)) {
throw new Error('err >= 0');
}

return 'UNKNOWN CODE: ' + code;
}

module.exports = function getErrname(code) {
return errname(uv, code);
};

// used for testing the fallback behavior.
module.exports._test = errname;
23 changes: 23 additions & 0 deletions test/errname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import test from 'ava';
import errname from '../lib/errname';

// simulates failure to capture process.binding('uv');
function fallback(code) {
return errname._test(null, code);
}

function makeTests(name, m, expected) {
test(name, t => {
// throws >= 0
t.throws(() => m(0), /err >= 0/);
t.throws(() => m(1), /err >= 0/);
t.throws(() => m('2'), /err >= 0/);
t.throws(() => m('foo'), /err >= 0/);

t.is(m(-2), expected);
t.is(m('-2'), expected);
});
}

makeTests('native', errname, 'ENOENT');
makeTests('fallback', fallback, 'UNKNOWN CODE: -2');

0 comments on commit 9c3a87e

Please sign in to comment.