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

errors: remove needless lazyAssert #11891

Closed
wants to merge 1 commit into from

Conversation

DavidCai1111
Copy link
Member

@DavidCai1111 DavidCai1111 commented Mar 17, 2017

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
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows [commit guidelines][]
Affected core subsystem(s)

errors

@nodejs-github-bot nodejs-github-bot added the errors Issues and PRs related to JavaScript errors originated in Node.js core. label Mar 17, 2017
Copy link
Member

@TimothyGu TimothyGu left a 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

@jasnell
Copy link
Member

jasnell commented Mar 17, 2017

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.

@TimothyGu
Copy link
Member

@jasnell
Copy link
Member

jasnell commented Mar 22, 2017

Landed in a00c9e9

@jasnell jasnell closed this Mar 22, 2017
@Trott
Copy link
Member

Trott commented Mar 23, 2017

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)
$ 

@TimothyGu
Copy link
Member

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.

@DavidCai1111
Copy link
Member Author

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

@Trott
Copy link
Member

Trott commented Mar 24, 2017

@DavidCai1993 Seems good to me if everyone else is 👍 about it.

@DavidCai1111
Copy link
Member Author

@Trott OK, if it is also LGT everyone else, I' ll submit another PR to do it ASAP 😃

MylesBorins pushed a commit that referenced this pull request Mar 28, 2017
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>
@MylesBorins MylesBorins mentioned this pull request Mar 28, 2017
@italoacasas italoacasas mentioned this pull request Apr 10, 2017
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
errors Issues and PRs related to JavaScript errors originated in Node.js core.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants