-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,15 +38,22 @@ const inspectDefaultOptions = Object.seal({ | |
breakLength: 60 | ||
}); | ||
|
||
const CIRCULAR_ERROR_MESSAGE = 'Converting circular structure to JSON'; | ||
|
||
var CIRCULAR_ERROR_MESSAGE; | ||
var Debug; | ||
|
||
function tryStringify(arg) { | ||
try { | ||
return JSON.stringify(arg); | ||
} catch (err) { | ||
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE) | ||
// Populate the circular error message lazily | ||
if (!CIRCULAR_ERROR_MESSAGE) { | ||
try { | ||
const a = {}; a.a = a; JSON.stringify(a); | ||
} catch (err) { | ||
CIRCULAR_ERROR_MESSAGE = err.message; | ||
} | ||
} | ||
if (err.name === 'TypeError' && err.message.match(CIRCULAR_ERROR_MESSAGE)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the changes to this line can be reverted now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. |
||
return '[Circular]'; | ||
throw err; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this the only error message comparison in lib?
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.
Yes, usually error message comparison is done in test for which we added
common.engineSpecificMessage
API. See this for example.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.
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: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.
I would love to use
common.engineSpecificMessage()
API for this case and that's why it is written. We are using it frequently intest
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 innode-chakracore
repo to have a check for engine, but I thought this was more appropriate fix.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.
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.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.
Okay. I understand the concerns. How about solving this problem broadly by having engine *specific string templates for various error messages?
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.
Currently the way it is achieved in
node-chakracore
isAdditional 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 .
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.
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.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.
Updated the PR with the change proposed by @cjihrig .
We can have separate discussion for error message centralization.