Skip to content

Commit d250e3d

Browse files
fhaldejasnell
authored andcommitted
errors: AssertionError moved to internal/error
AssertionError class is moved to interna/error in reference to the TODO in assert.js. This was suggested to get rid of the cyclic dependency between assert.js and internal/error.js PR-URL: #12906 Reviewed-By: James M Snell <[email protected]>
1 parent ce645e2 commit d250e3d

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

lib/assert.js

+9-34
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,6 @@ function lazyErrors() {
4040

4141
const assert = module.exports = ok;
4242

43-
// The AssertionError is defined in assert.
44-
// new assert.AssertionError({ message: message,
45-
// actual: actual,
46-
// expected: expected });
47-
48-
// TODO(jasnell): Consider moving AssertionError into internal/errors.js
49-
class AssertionError extends Error {
50-
constructor(options) {
51-
if (typeof options !== 'object' || options === null) {
52-
// Lazy because the errors module itself uses assertions, leading to
53-
// a circular dependency. This can be eliminated by moving this class
54-
// into internal/errors.js
55-
const errors = lazyErrors();
56-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
57-
}
58-
const message = options.message ||
59-
`${util.inspect(options.actual).slice(0, 128)} ` +
60-
`${options.operator} ` +
61-
util.inspect(options.expected).slice(0, 128);
62-
super(message);
63-
this.generatedMessage = !options.message;
64-
this.name = 'AssertionError [ERR_ASSERTION]';
65-
this.code = 'ERR_ASSERTION';
66-
this.actual = options.actual;
67-
this.expected = options.expected;
68-
this.operator = options.operator;
69-
var stackStartFunction = options.stackStartFunction || fail;
70-
Error.captureStackTrace(this, stackStartFunction);
71-
}
72-
}
73-
74-
assert.AssertionError = AssertionError;
75-
7643
// At present only the three keys mentioned above are used and
7744
// understood by the spec. Implementations or sub modules can pass
7845
// other keys to the AssertionError's constructor - they will be
@@ -89,7 +56,8 @@ function fail(actual, expected, message, operator, stackStartFunction) {
8956
message = actual;
9057
if (arguments.length === 2)
9158
operator = '!=';
92-
throw new AssertionError({
59+
const errors = lazyErrors();
60+
throw new errors.AssertionError({
9361
message: message,
9462
actual: actual,
9563
expected: expected,
@@ -101,6 +69,13 @@ function fail(actual, expected, message, operator, stackStartFunction) {
10169
// EXTENSION! allows for well behaved errors defined elsewhere.
10270
assert.fail = fail;
10371

72+
// The AssertionError is defined in internal/error.
73+
// new assert.AssertionError({ message: message,
74+
// actual: actual,
75+
// expected: expected });
76+
assert.AssertionError = lazyErrors().AssertionError;
77+
78+
10479
// Pure assertion tests whether a value is truthy, as determined
10580
// by !!guard.
10681
// assert.ok(guard, message_opt);

lib/internal/errors.js

+25
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ function makeNodeError(Base) {
4141
};
4242
}
4343

44+
class AssertionError extends Error {
45+
constructor(options) {
46+
if (typeof options !== 'object' || options === null) {
47+
throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
48+
}
49+
const util = lazyUtil();
50+
const assert = lazyAssert();
51+
const message = options.message ||
52+
`${util.inspect(options.actual).slice(0, 128)} ` +
53+
`${options.operator} ` +
54+
util.inspect(options.expected).slice(0, 128);
55+
56+
super(message);
57+
this.generatedMessage = !options.message;
58+
this.name = 'AssertionError [ERR_ASSERTION]';
59+
this.code = 'ERR_ASSERTION';
60+
this.actual = options.actual;
61+
this.expected = options.expected;
62+
this.operator = options.operator;
63+
const stackStartFunction = options.stackStartFunction || assert.fail;
64+
Error.captureStackTrace(this, stackStartFunction);
65+
}
66+
}
67+
4468
function message(key, args) {
4569
const assert = lazyAssert();
4670
assert.strictEqual(typeof key, 'string');
@@ -69,6 +93,7 @@ module.exports = exports = {
6993
Error: makeNodeError(Error),
7094
TypeError: makeNodeError(TypeError),
7195
RangeError: makeNodeError(RangeError),
96+
AssertionError,
7297
E // This is exported only to facilitate testing.
7398
};
7499

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 AssertionError({
3+
throw new errors.AssertionError({
44
^
55

66
AssertionError [ERR_ASSERTION]: 1 === 2

0 commit comments

Comments
 (0)