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

assert: fix AssertException, assign error code #12651

Closed
wants to merge 1 commit 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
60 changes: 33 additions & 27 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ const { isSet, isMap } = process.binding('util');
const objectToString = require('internal/util').objectToString;
const Buffer = require('buffer').Buffer;

var errors;
function lazyErrors() {
if (!errors)
errors = require('internal/errors');
return errors;
}

// The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
// assert module must conform to the following interface.
Expand All @@ -38,34 +45,33 @@ const assert = module.exports = ok;
// actual: actual,
// expected: expected });

assert.AssertionError = function AssertionError(options) {
this.name = 'AssertionError';
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;
if (options.message) {
this.message = options.message;
this.generatedMessage = false;
} else {
this.message = getMessage(this);
this.generatedMessage = true;
}
var stackStartFunction = options.stackStartFunction || fail;
Error.captureStackTrace(this, stackStartFunction);
};

// assert.AssertionError instanceof Error
util.inherits(assert.AssertionError, Error);

function truncate(s, n) {
return s.slice(0, n);
// TODO(jasnell): Consider moving AssertionError into internal/errors.js
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I dislike TODO comments because they tend to confuse others rather than communicate clearly what's going on. See all the longstanding TODOs that we just can't seem to address and all the confusion it sows for people reading them in #4641 #4642 #4640

In this case, what are the considerations? I'd rather see this opened as a separate issue with full sentences and paragraphs explaining the issue.

On the other hand, bonus points for putting your name there so at least the reader knows who to ask.

You can totally ignore this nit, of course. But in the off-chance you agree, then please open an issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

I expected at least one person to ask about this one :-) ... if we think it's a good idea, then I can just add a commit to this PR that moves it. It would be a minimally invasive change and would allow us to simplify some of the code. I just didn't do it yet because I want to know if there are any objections before doing so.

Copy link
Contributor

Choose a reason for hiding this comment

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

TODO comments like this are still better than not having them, IMO.

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);
}
}

function getMessage(self) {
return truncate(util.inspect(self.actual), 128) + ' ' +
self.operator + ' ' +
truncate(util.inspect(self.expected), 128);
}
assert.AssertionError = AssertionError;

// At present only the three keys mentioned above are used and
// understood by the spec. Implementations or sub modules can pass
Expand All @@ -83,7 +89,7 @@ function fail(actual, expected, message, operator, stackStartFunction) {
message = actual;
if (arguments.length === 2)
operator = '!=';
throw new assert.AssertionError({
throw new AssertionError({
message: message,
actual: actual,
expected: expected,
Expand Down
6 changes: 3 additions & 3 deletions test/message/error_exit.out
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Exiting with code=1

assert.js:*
throw new assert.AssertionError({
throw new AssertionError({
^
AssertionError: 1 === 2

AssertionError [ERR_ASSERTION]: 1 === 2
at Object.<anonymous> (*test*message*error_exit.js:*:*)
at Module._compile (module.js:*:*)
at Object.Module._extensions..js (module.js:*:*)
Expand Down
7 changes: 5 additions & 2 deletions test/parallel/test-assert-checktag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const util = require('util');

Expand All @@ -13,7 +13,10 @@ function re(literals, ...values) {
result += str.replace(/[\\^$.*+?()[\]{}|=!<>:-]/g, '\\$&');
result += literals[i + 1];
}
return new RegExp('^AssertionError: ' + result + '$');
return common.expectsError({
code: 'ERR_ASSERTION',
message: new RegExp(`^${result}$`)
});
}

// Turn off no-restricted-properties because we are testing deepEqual!
Expand Down
13 changes: 9 additions & 4 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const util = require('util');

Expand All @@ -13,7 +13,10 @@ function re(literals, ...values) {
result += str.replace(/[\\^$.*+?()[\]{}|=!<>:-]/g, '\\$&');
result += literals[i + 1];
}
return new RegExp(`^AssertionError: ${result}$`);
return common.expectsError({
code: 'ERR_ASSERTION',
message: new RegExp(`^${result}$`)
});
}

// The following deepEqual tests might seem very weird.
Expand Down Expand Up @@ -112,8 +115,10 @@ for (const a of similar) {

assert.throws(
() => { assert.deepEqual(new Set([{a: 0}]), new Set([{a: 1}])); },
/^AssertionError: Set { { a: 0 } } deepEqual Set { { a: 1 } }$/
);
common.expectsError({
code: 'ERR_ASSERTION',
message: /^Set { { a: 0 } } deepEqual Set { { a: 1 } }$/
}));

function assertDeepAndStrictEqual(a, b) {
assert.deepEqual(a, b);
Expand Down
32 changes: 26 additions & 6 deletions test/parallel/test-assert-fail.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

// no args
assert.throws(
() => { assert.fail(); },
/^AssertionError: undefined undefined undefined$/
common.expectsError({
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'undefined undefined undefined'
})
);

// one arg = message
assert.throws(
() => { assert.fail('custom message'); },
/^AssertionError: custom message$/
common.expectsError({
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'custom message'
})
);

// two args only, operator defaults to '!='
assert.throws(
() => { assert.fail('first', 'second'); },
/^AssertionError: 'first' != 'second'$/
common.expectsError({
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: '\'first\' != \'second\''
})
);

// three args
assert.throws(
() => { assert.fail('ignored', 'ignored', 'another custom message'); },
/^AssertionError: another custom message$/
common.expectsError({
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'another custom message'
})
);

// no third arg (but a fourth arg)
assert.throws(
() => { assert.fail('first', 'second', undefined, 'operator'); },
/^AssertionError: 'first' operator 'second'$/
common.expectsError({
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: '\'first\' operator \'second\''
})
);
Loading