lib: throw a special error in internal/assert
Instead of using the public AssertionError, use a simplified error that describes potential causes of these assertions and suggests the user to open an issue. PR-URL: https://github.com/nodejs/node/pull/26635 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
237d9f9716
commit
b9f1e57201
@ -1148,6 +1148,12 @@ is set for the `Http2Stream`.
|
|||||||
`http2.connect()` was passed a URL that uses any protocol other than `http:` or
|
`http2.connect()` was passed a URL that uses any protocol other than `http:` or
|
||||||
`https:`.
|
`https:`.
|
||||||
|
|
||||||
|
<a id="ERR_INTERNAL_ASSERTION"></a>
|
||||||
|
### ERR_INTERNAL_ASSERTION
|
||||||
|
|
||||||
|
There was a bug in Node.js or incorrect usage of Node.js internals.
|
||||||
|
To fix the error, open an issue at https://github.com/nodejs/node/issues.
|
||||||
|
|
||||||
<a id="ERR_INCOMPATIBLE_OPTION_PAIR"></a>
|
<a id="ERR_INCOMPATIBLE_OPTION_PAIR"></a>
|
||||||
### ERR_INCOMPATIBLE_OPTION_PAIR
|
### ERR_INCOMPATIBLE_OPTION_PAIR
|
||||||
|
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
let error;
|
||||||
|
function lazyError() {
|
||||||
|
if (!error) {
|
||||||
|
error = require('internal/errors').codes.ERR_INTERNAL_ASSERTION;
|
||||||
|
}
|
||||||
|
return error;
|
||||||
|
}
|
||||||
function assert(value, message) {
|
function assert(value, message) {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
require('assert')(value, message);
|
const ERR_INTERNAL_ASSERTION = lazyError();
|
||||||
|
throw new ERR_INTERNAL_ASSERTION(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fail(message) {
|
function fail(message) {
|
||||||
require('assert').fail(message);
|
const ERR_INTERNAL_ASSERTION = lazyError();
|
||||||
|
throw new ERR_INTERNAL_ASSERTION(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.fail = fail;
|
assert.fail = fail;
|
||||||
|
@ -798,6 +798,13 @@ E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
|
|||||||
E('ERR_INSPECTOR_COMMAND', 'Inspector error %d: %s', Error);
|
E('ERR_INSPECTOR_COMMAND', 'Inspector error %d: %s', Error);
|
||||||
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
|
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
|
||||||
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
|
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
|
||||||
|
E('ERR_INTERNAL_ASSERTION', (message) => {
|
||||||
|
const suffix = 'This is caused by either a bug in Node.js ' +
|
||||||
|
'or incorrect usage of Node.js internals.\n' +
|
||||||
|
'Please open an issue with this stack trace at ' +
|
||||||
|
'https://github.com/nodejs/node/issues\n';
|
||||||
|
return message === undefined ? suffix : `${message}\n${suffix}`;
|
||||||
|
}, Error);
|
||||||
E('ERR_INVALID_ADDRESS_FAMILY', function(addressType, host, port) {
|
E('ERR_INVALID_ADDRESS_FAMILY', function(addressType, host, port) {
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
@ -626,6 +626,19 @@ function expectsError(fn, settings, exact) {
|
|||||||
return mustCall(innerFn, exact);
|
return mustCall(innerFn, exact);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const suffix = 'This is caused by either a bug in Node.js ' +
|
||||||
|
'or incorrect usage of Node.js internals.\n' +
|
||||||
|
'Please open an issue with this stack trace at ' +
|
||||||
|
'https://github.com/nodejs/node/issues\n';
|
||||||
|
|
||||||
|
function expectsInternalAssertion(fn, message) {
|
||||||
|
assert.throws(fn, {
|
||||||
|
message: `${message}\n${suffix}`,
|
||||||
|
name: 'Error',
|
||||||
|
code: 'ERR_INTERNAL_ASSERTION'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function skipIfInspectorDisabled() {
|
function skipIfInspectorDisabled() {
|
||||||
if (!process.features.inspector) {
|
if (!process.features.inspector) {
|
||||||
skip('V8 inspector is disabled');
|
skip('V8 inspector is disabled');
|
||||||
@ -729,6 +742,7 @@ module.exports = {
|
|||||||
enoughTestCpu,
|
enoughTestCpu,
|
||||||
enoughTestMem,
|
enoughTestMem,
|
||||||
expectsError,
|
expectsError,
|
||||||
|
expectsInternalAssertion,
|
||||||
expectWarning,
|
expectWarning,
|
||||||
getArrayBufferViews,
|
getArrayBufferViews,
|
||||||
getBufferSources,
|
getBufferSources,
|
||||||
|
7
test/message/internal_assert.js
Normal file
7
test/message/internal_assert.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
// Flags: --expose-internals
|
||||||
|
require('../common');
|
||||||
|
|
||||||
|
const assert = require('internal/assert');
|
||||||
|
assert(false);
|
15
test/message/internal_assert.out
Normal file
15
test/message/internal_assert.out
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
internal/assert.js:*
|
||||||
|
throw new ERR_INTERNAL_ASSERTION(message);
|
||||||
|
^
|
||||||
|
|
||||||
|
Error [ERR_INTERNAL_ASSERTION]: This is caused by either a bug in Node.js or incorrect usage of Node.js internals.
|
||||||
|
Please open an issue with this stack trace at https://github.com/nodejs/node/issues
|
||||||
|
|
||||||
|
at assert (internal/assert.js:*:*)
|
||||||
|
at * (*test*message*internal_assert.js:7:1)
|
||||||
|
at *
|
||||||
|
at *
|
||||||
|
at *
|
||||||
|
at *
|
||||||
|
at *
|
||||||
|
at *
|
7
test/message/internal_assert_fail.js
Normal file
7
test/message/internal_assert_fail.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
// Flags: --expose-internals
|
||||||
|
require('../common');
|
||||||
|
|
||||||
|
const assert = require('internal/assert');
|
||||||
|
assert.fail('Unreachable!');
|
16
test/message/internal_assert_fail.out
Normal file
16
test/message/internal_assert_fail.out
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
internal/assert.js:*
|
||||||
|
throw new ERR_INTERNAL_ASSERTION(message);
|
||||||
|
^
|
||||||
|
|
||||||
|
Error [ERR_INTERNAL_ASSERTION]: Unreachable!
|
||||||
|
This is caused by either a bug in Node.js or incorrect usage of Node.js internals.
|
||||||
|
Please open an issue with this stack trace at https://github.com/nodejs/node/issues
|
||||||
|
|
||||||
|
at Function.fail (internal/assert.js:*:*)
|
||||||
|
at * (*test*message*internal_assert_fail.js:7:8)
|
||||||
|
at *
|
||||||
|
at *
|
||||||
|
at *
|
||||||
|
at *
|
||||||
|
at *
|
||||||
|
at *
|
@ -1,17 +1,13 @@
|
|||||||
// Flags: --expose-internals
|
// Flags: --expose-internals
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
// This tests that the internal assert module works as expected.
|
||||||
|
// The failures are tested in test/message.
|
||||||
|
|
||||||
require('../common');
|
require('../common');
|
||||||
|
|
||||||
const assert = require('assert');
|
|
||||||
const internalAssert = require('internal/assert');
|
const internalAssert = require('internal/assert');
|
||||||
|
|
||||||
// Should not throw.
|
// Should not throw.
|
||||||
internalAssert(true);
|
internalAssert(true);
|
||||||
internalAssert(true, 'fhqwhgads');
|
internalAssert(true, 'fhqwhgads');
|
||||||
|
|
||||||
assert.throws(() => { internalAssert(false); }, assert.AssertionError);
|
|
||||||
assert.throws(() => { internalAssert(false, 'fhqwhgads'); },
|
|
||||||
{ code: 'ERR_ASSERTION', message: 'fhqwhgads' });
|
|
||||||
assert.throws(() => { internalAssert.fail('fhqwhgads'); },
|
|
||||||
{ code: 'ERR_ASSERTION', message: 'fhqwhgads' });
|
|
||||||
|
@ -50,12 +50,10 @@ errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`, Error);
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
assert.throws(
|
common.expectsInternalAssertion(
|
||||||
() => new errors.codes.TEST_ERROR_1(),
|
() => new errors.codes.TEST_ERROR_1(),
|
||||||
{
|
'Code: TEST_ERROR_1; The provided arguments ' +
|
||||||
message: 'Code: TEST_ERROR_1; The provided arguments ' +
|
'length (0) does not match the required ones (1).'
|
||||||
'length (0) does not match the required ones (1).'
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,12 +78,9 @@ common.expectsError(
|
|||||||
assert.throws(() => tls.createServer({ ticketKeys: Buffer.alloc(0) }),
|
assert.throws(() => tls.createServer({ ticketKeys: Buffer.alloc(0) }),
|
||||||
/TypeError: Ticket keys length must be 48 bytes/);
|
/TypeError: Ticket keys length must be 48 bytes/);
|
||||||
|
|
||||||
common.expectsError(
|
common.expectsInternalAssertion(
|
||||||
() => tls.createSecurePair({}),
|
() => tls.createSecurePair({}),
|
||||||
{
|
'context.context must be a NativeSecureContext'
|
||||||
code: 'ERR_ASSERTION',
|
|
||||||
message: 'context.context must be a NativeSecureContext'
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -115,14 +115,14 @@ tmpdir.refresh();
|
|||||||
// https://github.com/joyent/node/issues/6690
|
// https://github.com/joyent/node/issues/6690
|
||||||
{
|
{
|
||||||
let oldhandle;
|
let oldhandle;
|
||||||
assert.throws(() => {
|
common.expectsInternalAssertion(
|
||||||
const w = fs.watch(__filename, common.mustNotCall());
|
() => {
|
||||||
oldhandle = w._handle;
|
const w = fs.watch(__filename, common.mustNotCall());
|
||||||
w._handle = { close: w._handle.close };
|
oldhandle = w._handle;
|
||||||
w.close();
|
w._handle = { close: w._handle.close };
|
||||||
}, {
|
w.close();
|
||||||
message: 'handle must be a FSEvent',
|
},
|
||||||
code: 'ERR_ASSERTION'
|
'handle must be a FSEvent'
|
||||||
});
|
);
|
||||||
oldhandle.close(); // clean up
|
oldhandle.close(); // clean up
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user