Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors,util: migrate to use internal/errors.js #13293

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const uv = process.binding('uv');
const Buffer = require('buffer').Buffer;
const internalUtil = require('internal/util');
const binding = process.binding('util');
const errors = require('internal/errors');

const isError = internalUtil.isError;

Expand Down Expand Up @@ -194,7 +195,7 @@ Object.defineProperty(inspect, 'defaultOptions', {
},
set: function(options) {
if (options === null || typeof options !== 'object') {
throw new TypeError('"options" must be an object');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
}
Object.assign(inspectDefaultOptions, options);
return inspectDefaultOptions;
Expand Down Expand Up @@ -946,17 +947,14 @@ exports.log = function() {
exports.inherits = function(ctor, superCtor) {

if (ctor === undefined || ctor === null)
throw new TypeError('The constructor to "inherits" must not be ' +
'null or undefined');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ctor', 'function');

if (superCtor === undefined || superCtor === null)
throw new TypeError('The super constructor to "inherits" must not ' +
'be null or undefined');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor', 'function');

if (superCtor.prototype === undefined)
throw new TypeError('The super constructor to "inherits" must ' +
'have a prototype');

throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor.prototype',
'function');
ctor.super_ = superCtor;
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
};
Expand Down
24 changes: 18 additions & 6 deletions test/parallel/test-util-inherits.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');
const inherits = require('util').inherits;
const errCheck =
/^TypeError: The super constructor to "inherits" must not be null or undefined$/;

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

// super constructor
function A() {
Expand Down Expand Up @@ -80,10 +82,20 @@ assert.strictEqual(e.constructor, E);
// should throw with invalid arguments
assert.throws(function() {
inherits(A, {});
}, /^TypeError: The super constructor to "inherits" must have a prototype$/);
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "superCtor.prototype" argument must be of type function'
})
);
assert.throws(function() {
inherits(A, null);
}, errCheck);
assert.throws(function() {
inherits(null, A);
}, /^TypeError: The constructor to "inherits" must not be null or undefined$/);
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "ctor" argument must be of type function'
})
);
14 changes: 12 additions & 2 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1028,11 +1028,21 @@ if (typeof Symbol !== 'undefined') {

assert.throws(() => {
util.inspect.defaultOptions = null;
}, /"options" must be an object/);
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be of type object'
})
);

assert.throws(() => {
util.inspect.defaultOptions = 'bad';
}, /"options" must be an object/);
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be of type object'
})
);
}

assert.doesNotThrow(() => util.inspect(process));