Skip to content

Commit 56403f7

Browse files
committed
assert: use destructuring for errors
Destructure the necessary Error classes from internal/errors. This improves the readability of the error creation.
1 parent 479b1d1 commit 56403f7

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/assert.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const {
2525
isDeepEqual,
2626
isDeepStrictEqual
2727
} = require('internal/util/comparisons');
28-
const errors = require('internal/errors');
28+
const { AssertionError, TypeError } = require('internal/errors');
2929
const { openSync, closeSync, readSync } = require('fs');
3030
const { parseExpressionAt } = require('internal/deps/acorn/dist/acorn');
3131
const { inspect } = require('util');
@@ -65,7 +65,7 @@ const NO_EXCEPTION_SENTINEL = {};
6565
function innerFail(obj) {
6666
if (obj.message instanceof Error) throw obj.message;
6767

68-
throw new errors.AssertionError(obj);
68+
throw new AssertionError(obj);
6969
}
7070

7171
function fail(actual, expected, message, operator, stackStartFn) {
@@ -95,7 +95,7 @@ assert.fail = fail;
9595
// new assert.AssertionError({ message: message,
9696
// actual: actual,
9797
// expected: expected });
98-
assert.AssertionError = errors.AssertionError;
98+
assert.AssertionError = AssertionError;
9999

100100
function getBuffer(fd, assertLine) {
101101
var lines = 0;
@@ -134,7 +134,7 @@ function innerOk(args, fn) {
134134
var [value, message] = args;
135135

136136
if (args.length === 0)
137-
throw new errors.TypeError('ERR_MISSING_ARGS', 'value');
137+
throw new TypeError('ERR_MISSING_ARGS', 'value');
138138

139139
if (!value) {
140140
if (message == null) {
@@ -338,8 +338,8 @@ function expectedException(actual, expected, msg) {
338338
return expected.test(actual);
339339
// assert.doesNotThrow does not accept objects.
340340
if (arguments.length === 2) {
341-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'expected',
342-
['Function', 'RegExp'], expected);
341+
throw new TypeError('ERR_INVALID_ARG_TYPE', 'expected',
342+
['Function', 'RegExp'], expected);
343343
}
344344
// The name and message could be non enumerable. Therefore test them
345345
// explicitly.
@@ -376,8 +376,8 @@ function expectedException(actual, expected, msg) {
376376

377377
function getActual(block) {
378378
if (typeof block !== 'function') {
379-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
380-
block);
379+
throw new TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
380+
block);
381381
}
382382
try {
383383
block();
@@ -393,10 +393,10 @@ assert.throws = function throws(block, error, message) {
393393

394394
if (typeof error === 'string') {
395395
if (arguments.length === 3)
396-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
397-
'error',
398-
['Function', 'RegExp'],
399-
error);
396+
throw new TypeError('ERR_INVALID_ARG_TYPE',
397+
'error',
398+
['Function', 'RegExp'],
399+
error);
400400

401401
message = error;
402402
error = null;
@@ -457,7 +457,7 @@ assert.ifError = function ifError(err) {
457457
message += inspect(err);
458458
}
459459

460-
const newErr = new assert.AssertionError({
460+
const newErr = new AssertionError({
461461
actual: err,
462462
expected: null,
463463
operator: 'ifError',

test/message/error_exit.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Exiting with code=1
22
assert.js:*
3-
throw new errors.AssertionError(obj);
3+
throw new AssertionError(obj);
44
^
55

66
AssertionError [ERR_ASSERTION]: 1 strictEqual 2

0 commit comments

Comments
 (0)