Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: AssertionError moved to internal/error #12906

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 9 additions & 34 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,6 @@ function lazyErrors() {

const assert = module.exports = ok;

// The AssertionError is defined in assert.
// new assert.AssertionError({ message: message,
// actual: actual,
// expected: expected });

// TODO(jasnell): Consider moving AssertionError into internal/errors.js
class AssertionError extends Error {
constructor(options) {
if (typeof options !== 'object' || options === null) {
// Lazy because the errors module itself uses assertions, leading to
// a circular dependency. This can be eliminated by moving this class
// into internal/errors.js
const errors = lazyErrors();
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
}
const message = options.message ||
`${util.inspect(options.actual).slice(0, 128)} ` +
`${options.operator} ` +
util.inspect(options.expected).slice(0, 128);
super(message);
this.generatedMessage = !options.message;
this.name = 'AssertionError [ERR_ASSERTION]';
this.code = 'ERR_ASSERTION';
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;
var stackStartFunction = options.stackStartFunction || fail;
Error.captureStackTrace(this, stackStartFunction);
}
}

assert.AssertionError = AssertionError;

// At present only the three keys mentioned above are used and
// understood by the spec. Implementations or sub modules can pass
// other keys to the AssertionError's constructor - they will be
Expand All @@ -89,7 +56,8 @@ function fail(actual, expected, message, operator, stackStartFunction) {
message = actual;
if (arguments.length === 2)
operator = '!=';
throw new AssertionError({
const errors = lazyErrors();
throw new errors.AssertionError({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This is anyway exported. Why not just throw new assert. AssertionError({...});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember, there was a problem caused by cyclic dependency. Something wasn't initialized causing few test cases to fail. Let me check it again!

message: message,
actual: actual,
expected: expected,
Expand All @@ -101,6 +69,13 @@ function fail(actual, expected, message, operator, stackStartFunction) {
// EXTENSION! allows for well behaved errors defined elsewhere.
assert.fail = fail;

// The AssertionError is defined in internal/error.
// new assert.AssertionError({ message: message,
// actual: actual,
// expected: expected });
assert.AssertionError = lazyErrors().AssertionError;


// Pure assertion tests whether a value is truthy, as determined
// by !!guard.
// assert.ok(guard, message_opt);
Expand Down
25 changes: 25 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ function makeNodeError(Base) {
};
}

class AssertionError extends Error {
constructor(options) {
if (typeof options !== 'object' || options === null) {
throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
}
const util = lazyUtil();
const assert = lazyAssert();
const message = options.message ||
`${util.inspect(options.actual).slice(0, 128)} ` +
`${options.operator} ` +
util.inspect(options.expected).slice(0, 128);

super(message);
this.generatedMessage = !options.message;
this.name = 'AssertionError [ERR_ASSERTION]';
this.code = 'ERR_ASSERTION';
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;
const stackStartFunction = options.stackStartFunction || assert.fail;
Error.captureStackTrace(this, stackStartFunction);
}
}

function message(key, args) {
const assert = lazyAssert();
assert.strictEqual(typeof key, 'string');
Expand Down Expand Up @@ -69,6 +93,7 @@ module.exports = exports = {
Error: makeNodeError(Error),
TypeError: makeNodeError(TypeError),
RangeError: makeNodeError(RangeError),
AssertionError,
E // This is exported only to facilitate testing.
};

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

AssertionError [ERR_ASSERTION]: 1 === 2
Expand Down