FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

errors: remove needless lazyAssert by DavidCai1111 · Pull Request #11891 · nodejs/node · GitHub

/ node Public

errors: remove needless lazyAssert - #11891

Closed
DavidCai1111 wants to merge 1 commit into
nodejs:masterfrom
DavidCai1111:errors/lazy-assert
Closed

errors: remove needless lazyAssert #11891
DavidCai1111 wants to merge 1 commit into
nodejs:masterfrom
DavidCai1111:errors/lazy-assert

Conversation

DavidCai1111 commented Mar 17, 2017
edited
Loading

Copy link
Copy Markdown
Member

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 added the errors Issues and PRs related to JavaScript errors originated in Node.js core. label Mar 17, 2017

TimothyGu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM if CI is green

jasnell commented Mar 17, 2017

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

jasnell commented Mar 22, 2017

Copy link
Copy Markdown
Member

Landed in a00c9e9

jasnell closed this Mar 22, 2017

Trott commented Mar 23, 2017
edited by gibfahn
Loading

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
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 commented Mar 24, 2017

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
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 mentioned this pull request Mar 28, 2017
italoacasas mentioned this pull request Apr 10, 2017
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

9 participants


Back | FazBrowse Home | New Git URL