-
Notifications
You must be signed in to change notification settings - Fork 30k
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
errors: remove needless lazyAssert #11891
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM if CI is green
We will need to make sure this works properly when the |
Landed in a00c9e95 |
If I'm not mistaken, with this change, we can't convert If I make this change: diff --git a/lib/assert.js b/lib/assert.js
index df575c0..9315783 100644
--- a/lib/assert.js
+++ b/lib/assert.js
@@ -25,6 +25,7 @@ const compare = process.binding('buffer').compare;
const util = require('util');
const objectToString = require('internal/util').objectToString;
const Buffer = require('buffer').Buffer;
+const errors = require('internal/errors');
// The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
@@ -391,7 +392,7 @@ function _throws(shouldThrow, block, expected, message) {
var actual;
if (typeof block !== 'function') {
- throw new TypeError('"block" argument must be a function');
+ throw new errors.TypeError('ERR_ARG_FUNCTION');
}
if (typeof expected === 'string') {
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 01e4e48..15198b4 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -80,4 +80,5 @@ module.exports = exports = {
//
// Note: Please try to keep these in alphabetical order
E('ERR_ASSERTION', (msg) => msg);
+E('ERR_ARG_FUNCTION', '"block" argument must be a function');
// Add new errors from here... ...and then compile, trying to run $ ./node
internal/errors.js:57
assert(messages.has(sym) === false, `Error symbol: ${sym} was already used.`);
^
TypeError: assert is not a function
at E (internal/errors.js:57:3)
at internal/errors.js:82:1
at NativeModule.compile (bootstrap_node.js:545:7)
at NativeModule.require (bootstrap_node.js:486:18)
at assert.js:28:16
at NativeModule.compile (bootstrap_node.js:545:7)
at NativeModule.require (bootstrap_node.js:486:18)
at timers.js:26:16
at NativeModule.compile (bootstrap_node.js:545:7)
at Function.NativeModule.require (bootstrap_node.js:486:18)
$ |
I guess we can convert the |
@Trott Oh, sorry for not taking the scene that using diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 01e4e482cb..363db9728b 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -6,7 +6,6 @@
// value statically and permanently identifies the error. While the error
// message may change, the code should not.
-const assert = require('assert');
const kCode = Symbol('code');
const messages = new Map();
@@ -36,10 +35,15 @@ function makeNodeError(Base) {
}
function message(key, args) {
- assert.strictEqual(typeof key, 'string');
+ if (typeof key !== 'string')
+ throw new TypeError('"key" argument must be a string');
+
const util = lazyUtil();
const msg = messages.get(key);
- assert(msg, `An invalid error message key was used: ${key}.`);
+
+ if (!msg)
+ throw new Error(`An invalid error message key was used: ${key}.`);
+
let fmt = util.format;
if (typeof msg === 'function') {
fmt = msg;
@@ -54,7 +58,9 @@ function message(key, args) {
// Utility function for registering the error codes. Only used here. Exported
// *only* to allow for testing.
function E(sym, val) {
- assert(messages.has(sym) === false, `Error symbol: ${sym} was already used.`);
+ if (messages.has(sym))
+ throw new Error(`Error symbol: ${sym} was already used.`);
+
messages.set(sym, typeof val === 'function' ? val : String(val));
} |
@DavidCai1993 Seems good to me if everyone else is 👍 about it. |
@Trott OK, if it is also LGT everyone else, I' ll submit another PR to do it ASAP 😃 |
PR-URL: #11891 Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Since now the
lazyAssert()
function will be called inE()
andE()
will be called suddenly at the end of the script, so there is no need to require theassert
module lazily any more.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
errors