Skip to content

Commit d3c2534

Browse files
BridgeARMylesBorins
authored andcommittedMar 20, 2018
assert: use destructuring for errors
Destructure the necessary Error classes from internal/errors. This improves the readability of the error creation. Backport-PR-URL: #19230 PR-URL: #18247 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 74f0d1a commit d3c2534

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed
 

‎lib/assert.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
const { isDeepEqual, isDeepStrictEqual } =
2424
require('internal/util/comparisons');
25-
const errors = require('internal/errors');
25+
const { AssertionError, TypeError } = require('internal/errors');
2626
const { inspect } = require('util');
2727

2828
const ERR_DIFF_DEACTIVATED = 0;
@@ -46,7 +46,7 @@ const NO_EXCEPTION_SENTINEL = {};
4646
function innerFail(obj) {
4747
if (obj.message instanceof Error) throw obj.message;
4848

49-
throw new errors.AssertionError(obj);
49+
throw new AssertionError(obj);
5050
}
5151

5252
function fail(actual, expected, message, operator, stackStartFn) {
@@ -76,7 +76,7 @@ assert.fail = fail;
7676
// new assert.AssertionError({ message: message,
7777
// actual: actual,
7878
// expected: expected });
79-
assert.AssertionError = errors.AssertionError;
79+
assert.AssertionError = AssertionError;
8080

8181

8282
// Pure assertion tests whether a value is truthy, as determined
@@ -220,8 +220,8 @@ function expectedException(actual, expected, msg) {
220220
return expected.test(actual);
221221
// assert.doesNotThrow does not accept objects.
222222
if (arguments.length === 2) {
223-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'expected',
224-
['Function', 'RegExp'], expected);
223+
throw new TypeError('ERR_INVALID_ARG_TYPE', 'expected',
224+
['Function', 'RegExp'], expected);
225225
}
226226
// The name and message could be non enumerable. Therefore test them
227227
// explicitly.
@@ -258,8 +258,8 @@ function expectedException(actual, expected, msg) {
258258

259259
function getActual(block) {
260260
if (typeof block !== 'function') {
261-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
262-
block);
261+
throw new TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
262+
block);
263263
}
264264
try {
265265
block();
@@ -275,10 +275,10 @@ assert.throws = function throws(block, error, message) {
275275

276276
if (typeof error === 'string') {
277277
if (arguments.length === 3)
278-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
279-
'error',
280-
['Function', 'RegExp'],
281-
error);
278+
throw new TypeError('ERR_INVALID_ARG_TYPE',
279+
'error',
280+
['Function', 'RegExp'],
281+
error);
282282

283283
message = error;
284284
error = null;

‎test/message/error_exit.out

+1-1
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 === 2

0 commit comments

Comments
 (0)
Please sign in to comment.