-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Exception message in json formatter #973
Conversation
src/formatter/json_formatter.js
Outdated
let message = exception.message || '' | ||
let stack = exception.stack || '' | ||
message = _.includes(stack, message) ? '' : message + '\n' | ||
data.result.error_message = message + stack || exception |
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.
for consistency over the type of error_message, I would suggest to have a JSON.stringify(exception)
. It would be a rare exception, but at least we know error_message will be a string all the time.
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 omitted the JSON.stringify in the pull request because that would change the spec (JSON.stringify add escaped quotes to string object):
result: { duration: 1, exception: 'my error', status: Status.FAILED } |
error_message: 'my error', |
Adding a type check would enforce a string without changing the spec.
I wouldn't add more code if not really needed
data.result.error_message = message + stack || _.isString(exception) ? exception : JSON.stringify(exception)
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.
This may be a better solution, what do you think?
if(_.isString(exception)) {
data.result.error_message = exception
} else {
let {message = '', stack = ''} = exception
message = _.includes(stack, message) ? '' : message + '\n'
data.result.error_message = message + stack || JSON.stringify(exception)
}
I have updated the pull request to force consistency over the type of error_message |
Great work guys, |
src/formatter/json_formatter.js
Outdated
let { message = '', stack = '' } = exception | ||
message = _.includes(stack, message) ? '' : message + '\n' | ||
data.result.error_message = | ||
message + stack || JSON.stringify(exception) |
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.
This is hard for me to follow. Thoughts on breaking out a function stringifyException
and focusing on making it easy to understand:
function stringifyException(exception) {
// if is string
// return it
// else if has message or stack
// if stack includes message
// return stack
// else
// return message newline stack
// else
// return stringified exception
}
The function could then go in a util file and be unit tested with some sample exceptions from the different browsers and non-error objects
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.
You are right, move this code to a function would be a better solution.
Any suggestion on where to put this utils file?
What about using /src/formatter/helpers/error_helpers.js?
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.
Sounds good to me
I have updated the pull request to add function stringifyException and unit test |
src/formatter/json_formatter.js
Outdated
@@ -156,7 +161,7 @@ export default class JsonFormatter extends Formatter { | |||
data.result.duration = testStep.result.duration | |||
} | |||
if (status === Status.FAILED && exception) { | |||
data.result.error_message = exception.stack || exception | |||
data.result.error_message = stringifyException(exception) |
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.
Thanks for the great test cases but thinking more on this feature I think we should use node-assertion-error-formatter which is what we use when displaying errors in the terminal. It includes the logic for concatenating the message / stack and shows diffs as well. Sorry I didn't remember this sooner.
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.
node-assertion-error-formatter does not pass 3 of the tests case I wrote. If those error are acceptable I can modify for the fourth time the pull request :)
-
ErrorHelpers
stringifyException
return stack:
AssertionError: expected '[object Object]stack' to equal 'stack' -
ErrorHelpers
stringifyException
return message and stack:
AssertionError: expected 'errorstack' to equal 'error\nstack' -
ErrorHelpers
stringifyException
stringify an object:
TypeError: stack.indexOf is not a function
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 just released v2.0.1 of node-assertion-error-formatter
which should fix cases 2 and 3 (charlierudolph/node-assertion-error-formatter@ca21cfd). Please update the version in the package.json / yarn.lock.
I think test case number 1 needs to be updated so the message is an empty string instead of undefined. Testing out the error constructor in node 8, this was always the case.
If all your test cases pass, I think the tests / util file you added can be deleted as the test cases are now covered in node-assertion-error-formatter
. We can use node-assertion-error-formatter
directly in src/formatter/json_formatter.js
I have updated the pull request. Now there is a small error when message is an empty string.
This is not a major error and should be fixed in assertion-error-formatter. I think we can merge this pull request and update later this dependency. |
Hi @innocentiv, Thanks for your making your first contribution to Cucumber, and welcome to the Cucumber committers team! You can now push directly to this repo and all other repos under the cucumber organization! 🍾 In return for this generous offer we hope you will:
On behalf of the Cucumber core team, |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Add exception.message to error_message if not already included in exception.stack
Fix #957