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

Exception message in json formatter #973

Merged
merged 1 commit into from
Nov 28, 2017

Conversation

innocentiv
Copy link
Contributor

Add exception.message to error_message if not already included in exception.stack

Fix #957

let message = exception.message || ''
let stack = exception.stack || ''
message = _.includes(stack, message) ? '' : message + '\n'
data.result.error_message = message + stack || exception

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.

Copy link
Contributor Author

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@finaruioana

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

@innocentiv
Copy link
Contributor Author

I have updated the pull request to force consistency over the type of error_message

@humphreyn
Copy link

Great work guys,
Sorry I couldn't be of more assistance. It's been mad at work and home:-)

let { message = '', stack = '' } = exception
message = _.includes(stack, message) ? '' : message + '\n'
data.result.error_message =
message + stack || JSON.stringify(exception)
Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me

@innocentiv
Copy link
Contributor Author

I have updated the pull request to add function stringifyException and unit test

@@ -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)
Copy link
Member

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.

Copy link
Contributor Author

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

  1. ErrorHelpers
    stringifyException
    return stack:
    AssertionError: expected '[object Object]stack' to equal 'stack'

  2. ErrorHelpers
    stringifyException
    return message and stack:
    AssertionError: expected 'errorstack' to equal 'error\nstack'

  3. ErrorHelpers
    stringifyException
    stringify an object:
    TypeError: stack.indexOf is not a function

Copy link
Member

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

@innocentiv
Copy link
Contributor Author

I have updated the pull request. Now there is a small error when message is an empty string.

  1. ErrorHelpers
    format
    return stack:
    AssertionError: expected '{"message":"","stack":"stack"}\nstack' to equal 'stack'

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.

@charlierudolph charlierudolph merged commit 83a4a2b into cucumber:master Nov 28, 2017
@aslakhellesoy
Copy link
Contributor

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:

  • ✅ Continue to use branches and pull requests. When someone on the core team approves a pull request (yours or someone else's), you're welcome to merge it yourself.
  • 💚 Commit to setting a good example by following and upholding our code of conduct in your interactions with other collaborators and users.
  • 💬 Join the community Slack channel to meet the rest of the team and make yourself at home.
  • ℹ️ Don't feel obliged to help, just do what you can if you have the time and the energy.
  • 🙋 Ask if you need anything. We're looking for feedback about how to make the project more welcoming, so please tell us!

On behalf of the Cucumber core team,
Aslak Hellesøy
Creator of Cucumber

@lock
Copy link

lock bot commented Nov 28, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants