| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
There was a problem hiding this comment.
LGTM if CI is green
Sorry, something went wrong.
|
We will need to make sure this works properly when the internal/errors module is used from within bootstrap_node.js but it should be fine. |
Sorry, something went wrong.
Sorry, something went wrong.
|
If I'm not mistaken, with this change, we can't convert lib/assert.js to use internal/errors.js, at least not trivially. 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 results in: $ ./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)
$ |
Sorry, something went wrong.
|
I guess we can convert the assert.ok usage in internal/error.js to a normal if statement, which would avoid using assert in that module. |
Sorry, something went wrong.
|
@Trott Oh, sorry for not taking the scene that using internal/errors.js in lib/assert.js into consideration. Seems @TimothyGu 's idea is a good solution? like: 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));
} |
Sorry, something went wrong.
|
@DavidCai1993 Seems good to me if everyone else is 👍 about it. |
Sorry, something went wrong.
|
@Trott OK, if it is also LGT everyone else, I' ll submit another PR to do it ASAP 😃 |
Sorry, something went wrong.
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>
| Back | FazBrowse Home | New Git URL |
Since now the lazyAssert() function will be called in E() and E() will be called suddenly at the end of the script, so there is no need to require the assert module lazily any more.
Checklist
Affected core subsystem(s)
errors