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

lib: Use regex to compare error message #11854

Closed
wants to merge 3 commits into from

Conversation

@kunalspathak
Copy link
Contributor

commented Mar 15, 2017

To make node Engine Agnostic, use better comparison method for error
message.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

lib

lib: Use regex to compare error message
To make node engine agnostic, use better comparison method for error
message.
@jasnell

This comment has been minimized.

Copy link
Member

commented Mar 15, 2017

lib/util.js Outdated

var Debug;

function tryStringify(arg) {
try {
return JSON.stringify(arg);
} catch (err) {
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)
if (err.name === 'TypeError' && err.message.match(CIRCULAR_ERROR_MESSAGE))

This comment has been minimized.

Copy link
@boingoing

boingoing Mar 15, 2017

Contributor

Is this the only error message comparison in lib?

This comment has been minimized.

Copy link
@kunalspathak

kunalspathak Mar 15, 2017

Author Contributor

Yes, usually error message comparison is done in test for which we added common.engineSpecificMessage API. See this for example.

This comment has been minimized.

Copy link
@cjihrig

cjihrig Mar 15, 2017

Contributor

I don't really like this particular change because it makes the test less specific, even if we are unlikely to get burned by it. I think the common.engineSpecificMessage API is a good solution in the long term. In the shorter term, you could generate this particular error dynamically like this:

try { const a = {}; a.a = a; JSON.stringify(a); } catch (err) { console.log(err.message); }

This comment has been minimized.

Copy link
@kunalspathak

kunalspathak Mar 15, 2017

Author Contributor

I would love to use common.engineSpecificMessage() API for this case and that's why it is written. We are using it frequently in test to get around similar issues. But this particular check is specific to v8 and is inside product code which I felt should be relaxed. Alternatively I can modify the test case in node-chakracore repo to have a check for engine, but I thought this was more appropriate fix.

This comment has been minimized.

Copy link
@mscdex

mscdex Mar 15, 2017

Contributor

I think between the current regexp and @cjihrig's suggestion, I would vote for the latter (followed by an assert to make sure the value is a non-empty string during startup).

The regexp might match a TypeError that includes the name of a property named 'circular', which would match the wrong error.

This comment has been minimized.

Copy link
@kunalspathak

kunalspathak Mar 15, 2017

Author Contributor

Okay. I understand the concerns. How about solving this problem broadly by having engine *specific string templates for various error messages?

// v8-error-msg.js
var errorMsg = [];
...
errorMsg[engineName]['SyntaxError_Unexpected_Id'] = "SyntaxError: Unexpected identifier";
...

We could update test infrastructure to fetch the appropriate error message based on template. Of-course some error message might need parameters like variable names, function names, etc. which we will have to add support for.

// test.js
assert.strictEquals(stdErr, common.engineSpecific('SyntaxError_Unexpected_Id'));

Currently the way it is achieved in node-chakracore is

assert.strictEquals(stdErr, common.engineSpecific({
    v8: "SyntaxError: Unexpected identifier",
    chakracore: "SyntaxError: Expected ';'"})
}));

Additional benefit we get is if engine changes the error message, there will be one common place where the messages will have to be changed. I know this is non-trivial work since it will involve going through every test case that relies on error message, make the entry of error message in central engine specific file and then update the test to fetch that error message, but wanted to get thoughts of community to make node test cases more engine neutral.

cc: @addaleax, @jasnell

*Roughly adopted from #4311 .

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 15, 2017

Member

Fwiw I like @cjihrig’s suggestion for this particular problem too, just generating the error dynamically (either lazily or when at the top level of util.js) seems okay to me.

This comment has been minimized.

Copy link
@kunalspathak

kunalspathak Mar 17, 2017

Author Contributor

Updated the PR with the change proposed by @cjihrig .

We can have separate discussion for error message centralization.

lib/util.js Outdated

var Debug;

function tryStringify(arg) {
try {
return JSON.stringify(arg);
} catch (err) {
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)
if (err.name === 'TypeError' && err.message.match(CIRCULAR_ERROR_MESSAGE))

This comment has been minimized.

Copy link
@cjihrig

cjihrig Mar 15, 2017

Contributor

I don't really like this particular change because it makes the test less specific, even if we are unlikely to get burned by it. I think the common.engineSpecificMessage API is a good solution in the long term. In the shorter term, you could generate this particular error dynamically like this:

try { const a = {}; a.a = a; JSON.stringify(a); } catch (err) { console.log(err.message); }
lib: Addressed review comments
Lazily populate the `circular reference` error message thrown
by `JSON.stringify()` which can be used to compare the error
message thrown.
lib/util.js Outdated
CIRCULAR_ERROR_MESSAGE = err.message;
}
}
if (err.name === 'TypeError' && err.message.match(CIRCULAR_ERROR_MESSAGE))

This comment has been minimized.

Copy link
@mscdex

mscdex Mar 17, 2017

Contributor

I think the changes to this line can be reverted now.

This comment has been minimized.

Copy link
@kunalspathak

kunalspathak Mar 17, 2017

Author Contributor

Sure.

@mscdex

This comment has been minimized.

Copy link
Contributor

commented Mar 19, 2017

jasnell added a commit that referenced this pull request Mar 22, 2017
lib: Use regex to compare error message
To make node engine agnostic, use better comparison method for error
message.

Lazily populate the `circular reference` error message thrown
by `JSON.stringify()` which can be used to compare the error
message thrown.

PR-URL: #11854
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
@jasnell

This comment has been minimized.

Copy link
Member

commented Mar 22, 2017

Landed in 4eb194a

@jasnell jasnell closed this Mar 22, 2017

kunalspathak added a commit to kunalspathak/node-chakracore that referenced this pull request Mar 23, 2017
chakrashim,test: Revert previous changes for chakracore
Reverted nodejs@374a1d8 because
nodejs/node#11854 is present in upstream.

Updated v8-version
kunalspathak added a commit to kunalspathak/node-chakracore that referenced this pull request Mar 24, 2017
chakrashim,test: Revert previous changes for chakracore
Reverted nodejs@374a1d8 because
nodejs/node#11854 is present in upstream.

Updated v8-version

PR-URL: nodejs#198
Signed-off-by: Hitesh Kanwathirtha <hiteshk@microsoft.com>
@MylesBorins

This comment has been minimized.

Copy link
Member

commented Mar 28, 2017

This will need to be manually backported to v7.x

@jasnell jasnell referenced this pull request Apr 4, 2017
@gibfahn gibfahn referenced this pull request Jun 15, 2017
2 of 3 tasks complete
@gibfahn

This comment has been minimized.

Copy link
Member

commented Jun 17, 2017

Should this be backported to v6.x-staging? If yes please follow the guide and raise a backport PR, if no let me know or add the dont-land-on label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
10 participants
You can’t perform that action at this time.