From d3c2534bbeb12b813cecdd543e9da04dc9fb70d9 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 23 Jan 2018 00:11:22 +0100 Subject: [PATCH] assert: use destructuring for errors Destructure the necessary Error classes from internal/errors. This improves the readability of the error creation. Backport-PR-URL: https://github.com/nodejs/node/pull/19230 PR-URL: https://github.com/nodejs/node/pull/18247 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/assert.js | 22 +++++++++++----------- test/message/error_exit.out | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 48670de0c5458a..74757c7b297f73 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -22,7 +22,7 @@ const { isDeepEqual, isDeepStrictEqual } = require('internal/util/comparisons'); -const errors = require('internal/errors'); +const { AssertionError, TypeError } = require('internal/errors'); const { inspect } = require('util'); const ERR_DIFF_DEACTIVATED = 0; @@ -46,7 +46,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) { @@ -76,7 +76,7 @@ assert.fail = fail; // new assert.AssertionError({ message: message, // actual: actual, // expected: expected }); -assert.AssertionError = errors.AssertionError; +assert.AssertionError = AssertionError; // Pure assertion tests whether a value is truthy, as determined @@ -220,8 +220,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. @@ -258,8 +258,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(); @@ -275,10 +275,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; diff --git a/test/message/error_exit.out b/test/message/error_exit.out index c2dfc6a9f910ca..8f03f08a7e0392 100644 --- a/test/message/error_exit.out +++ b/test/message/error_exit.out @@ -1,6 +1,6 @@ Exiting with code=1 assert.js:* - throw new errors.AssertionError(obj); + throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: 1 === 2