Skip to content

Commit

Permalink
assert: use destructuring for errors
Browse files Browse the repository at this point in the history
Destructure the necessary Error classes from internal/errors.
This improves the readability of the error creation.

PR-URL: nodejs#18247
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR committed Jan 24, 2018
1 parent 72bb444 commit 8e6e1c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const {
isDeepEqual,
isDeepStrictEqual
} = require('internal/util/comparisons');
const errors = require('internal/errors');
const { AssertionError, TypeError } = require('internal/errors');
const { openSync, closeSync, readSync } = require('fs');
const { parseExpressionAt } = require('internal/deps/acorn/dist/acorn');
const { inspect } = require('util');
Expand Down Expand Up @@ -69,7 +69,7 @@ const NO_EXCEPTION_SENTINEL = {};
function innerFail(obj) {
if (obj.message instanceof Error) throw obj.message;

throw new errors.AssertionError(obj);
throw new AssertionError(obj);
}

function fail(actual, expected, message, operator, stackStartFn) {
Expand Down Expand Up @@ -99,7 +99,7 @@ assert.fail = fail;
// new assert.AssertionError({ message: message,
// actual: actual,
// expected: expected });
assert.AssertionError = errors.AssertionError;
assert.AssertionError = AssertionError;

function getBuffer(fd, assertLine) {
var lines = 0;
Expand Down Expand Up @@ -138,7 +138,7 @@ function innerOk(args, fn) {
var [value, message] = args;

if (args.length === 0)
throw new errors.TypeError('ERR_MISSING_ARGS', 'value');
throw new TypeError('ERR_MISSING_ARGS', 'value');

if (!value) {
if (message == null) {
Expand Down Expand Up @@ -346,8 +346,8 @@ function expectedException(actual, expected, msg) {
return expected.test(actual);
// assert.doesNotThrow does not accept objects.
if (arguments.length === 2) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'expected',
['Function', 'RegExp'], expected);
throw new TypeError('ERR_INVALID_ARG_TYPE', 'expected',
['Function', 'RegExp'], expected);
}
// The name and message could be non enumerable. Therefore test them
// explicitly.
Expand Down Expand Up @@ -384,8 +384,8 @@ function expectedException(actual, expected, msg) {

function getActual(block) {
if (typeof block !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
block);
throw new TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
block);
}
try {
block();
Expand All @@ -401,10 +401,10 @@ assert.throws = function throws(block, error, message) {

if (typeof error === 'string') {
if (arguments.length === 3)
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'error',
['Function', 'RegExp'],
error);
throw new TypeError('ERR_INVALID_ARG_TYPE',
'error',
['Function', 'RegExp'],
error);

message = error;
error = null;
Expand Down Expand Up @@ -465,7 +465,7 @@ assert.ifError = function ifError(err) {
message += inspect(err);
}

const newErr = new assert.AssertionError({
const newErr = new AssertionError({
actual: err,
expected: null,
operator: 'ifError',
Expand Down
2 changes: 1 addition & 1 deletion test/message/error_exit.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Exiting with code=1
assert.js:*
throw new errors.AssertionError(obj);
throw new AssertionError(obj);
^

AssertionError [ERR_ASSERTION]: 1 strictEqual 2
Expand Down

0 comments on commit 8e6e1c9

Please sign in to comment.