Skip to content

Commit

Permalink
errors,process: migrate to use internal/errors.js
Browse files Browse the repository at this point in the history
PR-URL: #13285
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
  • Loading branch information
sreepurnajasti authored and mhdawson committed Jun 5, 2017
1 parent 7024c5a commit 062071a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 32 deletions.
3 changes: 3 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
E('ERR_ASSERTION', (msg) => msg);
E('ERR_CONSOLE_WRITABLE_STREAM',
(name) => `Console expects a writable stream instance for ${name}`);
E('ERR_CPU_USAGE', (errMsg) => `Unable to obtain cpu usage ${errMsg}`);
E('ERR_HTTP_HEADERS_SENT',
'Cannot render headers after they are sent to the client');
E('ERR_HTTP_INVALID_CHAR', 'Invalid character in statusMessage.');
Expand Down Expand Up @@ -160,6 +161,8 @@ E('ERR_SOCKET_BAD_TYPE',
E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data');
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
E('ERR_V8BREAKITERATOR', 'full ICU data not installed. ' +
'See https://github.com/nodejs/node/wiki/Intl');
// Add new errors from here...

function invalidArgType(name, expected, actual) {
Expand Down
22 changes: 12 additions & 10 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const errors = require('internal/errors');
var _lazyConstants = null;

function lazyConstants() {
Expand All @@ -10,7 +11,7 @@ function lazyConstants() {
}

const assert = process.assert = function(x, msg) {
if (!x) throw new Error(msg || 'assertion error');
if (!x) throw new errors.Error('ERR_ASSERTION', msg || 'assertion error');
};


Expand All @@ -28,18 +29,20 @@ function setup_cpuUsage() {
// If a previous value was passed in, ensure it has the correct shape.
if (prevValue) {
if (!previousValueIsValid(prevValue.user)) {
throw new TypeError('value of user property of argument is invalid');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'preValue.user', 'Number');
}

if (!previousValueIsValid(prevValue.system)) {
throw new TypeError('value of system property of argument is invalid');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'preValue.system', 'Number');
}
}

// Call the native function to get the current values.
const errmsg = _cpuUsage(cpuValues);
if (errmsg) {
throw new Error('unable to obtain CPU usage: ' + errmsg);
throw new errors.Error('ERR_CPU_USAGE', errmsg);
}

// If a previous value was passed in, return diff of current from previous.
Expand Down Expand Up @@ -81,8 +84,8 @@ function setup_hrtime() {
const needsBorrow = nsec < 0;
return [needsBorrow ? sec - 1 : sec, needsBorrow ? nsec + 1e9 : nsec];
}

throw new TypeError('process.hrtime() only accepts an Array tuple');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'process.hrtime()', 'Array');
}

return [
Expand Down Expand Up @@ -132,8 +135,7 @@ function setupConfig(_source) {
des.value = require('internal/util').deprecate(function v8BreakIterator() {
if (processConfig.hasSmallICU && !processConfig.icuDataDir) {
// Intl.v8BreakIterator() would crash w/ fatal error, so throw instead.
throw new Error('v8BreakIterator: full ICU data not installed. ' +
'See https://github.com/nodejs/node/wiki/Intl');
throw new errors.Error('ERR_V8BREAKITERATOR');
}
return Reflect.construct(oldV8BreakIterator, arguments);
}, 'Intl.v8BreakIterator is deprecated and will be removed soon.',
Expand Down Expand Up @@ -161,7 +163,7 @@ function setupKillAndExit() {

// eslint-disable-next-line eqeqeq
if (pid != (pid | 0)) {
throw new TypeError('invalid pid');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pid', 'Number');
}

// preserve null signal
Expand All @@ -172,7 +174,7 @@ function setupKillAndExit() {
if (lazyConstants()[sig]) {
err = process._kill(pid, lazyConstants()[sig]);
} else {
throw new Error(`Unknown signal: ${sig}`);
throw new errors.Error('ERR_UNKNOWN_SIGNAL', `${sig}`);
}
}

Expand Down
16 changes: 13 additions & 3 deletions test/parallel/test-process-assert.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

assert.strictEqual(process.assert(1, 'error'), undefined);
assert.throws(() => {
process.assert(undefined, 'errorMessage');
}, /^Error: errorMessage$/);
}, common.expectsError({
code: 'ERR_ASSERTION',
type: Error,
message: 'errorMessage'
})
);
assert.throws(() => {
process.assert(false);
}, /^Error: assertion error$/);
}, common.expectsError({
code: 'ERR_ASSERTION',
type: Error,
message: 'assertion error'
})
);
18 changes: 12 additions & 6 deletions test/parallel/test-process-cpuUsage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
require('../common');
const assert = require('assert');

const common = require('../common');
const result = process.cpuUsage();

// Validate the result of calling with no previous value argument.
Expand Down Expand Up @@ -32,11 +31,18 @@ for (let i = 0; i < 10; i++) {
assert(diffUsage.user >= 0);
assert(diffUsage.system >= 0);
}
const invalidUserArgument = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "preValue.user" argument must be of type Number'
});

const invalidSystemArgument = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "preValue.system" argument must be of type Number'
});

const invalidUserArgument =
/^TypeError: value of user property of argument is invalid$/;
const invalidSystemArgument =
/^TypeError: value of system property of argument is invalid$/;

// Ensure that an invalid shape for the previous value argument throws an error.
assert.throws(() => {
Expand Down
16 changes: 11 additions & 5 deletions test/parallel/test-process-hrtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

// the default behavior, return an Array "tuple" of numbers
Expand All @@ -32,19 +32,25 @@ validateTuple(tuple);
// validate that passing an existing tuple returns another valid tuple
validateTuple(process.hrtime(tuple));

const invalidHrtimeArgument = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "process.hrtime()" argument must be of type Array'
});

// test that only an Array may be passed to process.hrtime()
assert.throws(() => {
process.hrtime(1);
}, /^TypeError: process\.hrtime\(\) only accepts an Array tuple$/);
}, invalidHrtimeArgument);
assert.throws(() => {
process.hrtime([]);
}, /^TypeError: process\.hrtime\(\) only accepts an Array tuple$/);
}, invalidHrtimeArgument);
assert.throws(() => {
process.hrtime([1]);
}, /^TypeError: process\.hrtime\(\) only accepts an Array tuple$/);
}, invalidHrtimeArgument);
assert.throws(() => {
process.hrtime([1, 2, 3]);
}, /^TypeError: process\.hrtime\(\) only accepts an Array tuple$/);
}, invalidHrtimeArgument);

function validateTuple(tuple) {
assert(Array.isArray(tuple));
Expand Down
31 changes: 23 additions & 8 deletions test/parallel/test-process-kill-pid.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

// test variants of pid
Expand All @@ -38,20 +38,35 @@ const assert = require('assert');
//
// process.pid, String(process.pid): ourself

const invalidPidArgument = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "pid" argument must be of type Number'
});

assert.throws(function() { process.kill('SIGTERM'); },
/^TypeError: invalid pid$/);
assert.throws(function() { process.kill(null); }, /^TypeError: invalid pid$/);
invalidPidArgument);
assert.throws(function() { process.kill(null); },
invalidPidArgument);
assert.throws(function() { process.kill(undefined); },
/^TypeError: invalid pid$/);
invalidPidArgument);
assert.throws(function() { process.kill(+'not a number'); },
/^TypeError: invalid pid$/);
assert.throws(function() { process.kill(1 / 0); }, /^TypeError: invalid pid$/);
assert.throws(function() { process.kill(-1 / 0); }, /^TypeError: invalid pid$/);
invalidPidArgument);
assert.throws(function() { process.kill(1 / 0); },
invalidPidArgument);
assert.throws(function() { process.kill(-1 / 0); },
invalidPidArgument);

// Test that kill throws an error for invalid signal
const unknownSignal = common.expectsError({
code: 'ERR_UNKNOWN_SIGNAL',
type: Error,
message: 'Unknown signal: test'
});


assert.throws(function() { process.kill(1, 'test'); },
/^Error: Unknown signal: test$/);
unknownSignal);

// Test kill argument processing in valid cases.
//
Expand Down

0 comments on commit 062071a

Please sign in to comment.