Skip to content

Commit 1609899

Browse files
bidipynejasnell
authored andcommitted
errors,util: migrate to use internal/errors.js
PR-URL: #13293 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 84c066a commit 1609899

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

lib/util.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const uv = process.binding('uv');
2525
const Buffer = require('buffer').Buffer;
2626
const internalUtil = require('internal/util');
2727
const binding = process.binding('util');
28+
const errors = require('internal/errors');
2829

2930
const isError = internalUtil.isError;
3031

@@ -194,7 +195,7 @@ Object.defineProperty(inspect, 'defaultOptions', {
194195
},
195196
set: function(options) {
196197
if (options === null || typeof options !== 'object') {
197-
throw new TypeError('"options" must be an object');
198+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
198199
}
199200
Object.assign(inspectDefaultOptions, options);
200201
return inspectDefaultOptions;
@@ -946,17 +947,14 @@ exports.log = function() {
946947
exports.inherits = function(ctor, superCtor) {
947948

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

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

956955
if (superCtor.prototype === undefined)
957-
throw new TypeError('The super constructor to "inherits" must ' +
958-
'have a prototype');
959-
956+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor.prototype',
957+
'function');
960958
ctor.super_ = superCtor;
961959
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
962960
};

test/parallel/test-util-inherits.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const inherits = require('util').inherits;
6-
const errCheck =
7-
/^TypeError: The super constructor to "inherits" must not be null or undefined$/;
8-
6+
const errCheck = common.expectsError({
7+
code: 'ERR_INVALID_ARG_TYPE',
8+
type: TypeError,
9+
message: 'The "superCtor" argument must be of type function'
10+
});
911

1012
// super constructor
1113
function A() {
@@ -80,10 +82,20 @@ assert.strictEqual(e.constructor, E);
8082
// should throw with invalid arguments
8183
assert.throws(function() {
8284
inherits(A, {});
83-
}, /^TypeError: The super constructor to "inherits" must have a prototype$/);
85+
}, common.expectsError({
86+
code: 'ERR_INVALID_ARG_TYPE',
87+
type: TypeError,
88+
message: 'The "superCtor.prototype" argument must be of type function'
89+
})
90+
);
8491
assert.throws(function() {
8592
inherits(A, null);
8693
}, errCheck);
8794
assert.throws(function() {
8895
inherits(null, A);
89-
}, /^TypeError: The constructor to "inherits" must not be null or undefined$/);
96+
}, common.expectsError({
97+
code: 'ERR_INVALID_ARG_TYPE',
98+
type: TypeError,
99+
message: 'The "ctor" argument must be of type function'
100+
})
101+
);

test/parallel/test-util-inspect.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,11 +1028,21 @@ if (typeof Symbol !== 'undefined') {
10281028

10291029
assert.throws(() => {
10301030
util.inspect.defaultOptions = null;
1031-
}, /"options" must be an object/);
1031+
}, common.expectsError({
1032+
code: 'ERR_INVALID_ARG_TYPE',
1033+
type: TypeError,
1034+
message: 'The "options" argument must be of type object'
1035+
})
1036+
);
10321037

10331038
assert.throws(() => {
10341039
util.inspect.defaultOptions = 'bad';
1035-
}, /"options" must be an object/);
1040+
}, common.expectsError({
1041+
code: 'ERR_INVALID_ARG_TYPE',
1042+
type: TypeError,
1043+
message: 'The "options" argument must be of type object'
1044+
})
1045+
);
10361046
}
10371047

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

0 commit comments

Comments
 (0)