diff --git a/index.js b/index.js index 6261c3abfd..8ee4d97cb0 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict'; const path = require('path'); const os = require('os'); +const util = require('util'); const childProcess = require('child_process'); const crossSpawn = require('cross-spawn'); const stripFinalNewline = require('strip-final-newline'); @@ -10,7 +11,6 @@ const _getStream = require('get-stream'); const mergeStream = require('merge-stream'); const pFinally = require('p-finally'); const onExit = require('signal-exit'); -const errname = require('./lib/errname'); const stdio = require('./lib/stdio'); const TEN_MEGABYTES = 1000 * 1000 * 10; @@ -220,7 +220,7 @@ function getCode({error = {}}, code) { } if (Number.isInteger(code)) { - return [errname(-Math.abs(code)), Math.abs(code)]; + return [util.getSystemErrorName(-Math.abs(code)), Math.abs(code)]; } return []; diff --git a/lib/errname.js b/lib/errname.js deleted file mode 100644 index 151c53fea3..0000000000 --- a/lib/errname.js +++ /dev/null @@ -1,39 +0,0 @@ -'use strict'; -// Older verions of Node.js might not have `util.getSystemErrorName()`. -// In that case, fall back to a deprecated internal. -const util = require('util'); - -let uv; - -if (typeof util.getSystemErrorName === 'function') { - module.exports = util.getSystemErrorName; -} else { - try { - uv = process.binding('uv'); // eslint-disable-line node/no-deprecated-api - - if (typeof uv.errname !== 'function') { - throw new TypeError('uv.errname is not a function'); - } - } catch (error) { - console.error('execa/lib/errname: unable to establish process.binding(\'uv\')', error); - uv = undefined; - } - - module.exports = code => errname(uv, code); -} - -// Used for testing the fallback behavior -module.exports.__test__ = errname; - -function errname(uv, code) { - if (uv !== undefined) { - return uv.errname(code); - } - - if (!(code < 0)) { - throw new Error('err >= 0'); - } - - return `Unknown system error ${code}`; -} - diff --git a/test/errname.js b/test/errname.js deleted file mode 100644 index 001c148359..0000000000 --- a/test/errname.js +++ /dev/null @@ -1,26 +0,0 @@ -import test from 'ava'; -import errname from '../lib/errname'; - -const isWindows = process.platform === 'win32'; - -// Simulates failure to capture `process.binding('uv');` -const fallback = code => errname.__test__(undefined, code); - -function makeTests(name, m, expected) { - test(`${name}: >=0 exit codes`, t => { - // Throws >= 0 - t.throws(() => m(0), /err >= 0|It must be a negative integer|must be of type negative number/); - t.throws(() => m(1), /err >= 0|It must be a negative integer|must be of type negative number/); - t.throws(() => m('2'), /err >= 0|must be of type number|must be of type negative number/); - t.throws(() => m('foo'), /err >= 0|must be of type number|must be of type negative number/); - }); - - test(`${name}: negative exit codes`, t => { - t.is(m(-2), expected); - }); -} - -const unknown = 'Unknown system error -2'; - -makeTests('native', errname, isWindows ? unknown : 'ENOENT'); -makeTests('fallback', fallback, unknown);