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

Inconsistent error property enumerability #49

Open
jaydenseric opened this issue Jun 6, 2018 · 6 comments
Open

Inconsistent error property enumerability #49

jaydenseric opened this issue Jun 6, 2018 · 6 comments
Assignees
Labels

Comments

@jaydenseric
Copy link

jaydenseric commented Jun 6, 2018

You would expect these usages to log identically:

import createError from 'http-errors'

console.log(
  JSON.stringify(createError('Message 1.')),
  JSON.stringify(createError(new Error('Message 2.')))
)

They actually output this:

{"message":"Message 1."} {"expose":false,"statusCode":500,"status":500}

For context, I am attempting to do snapshot testing of errors that have been created various ways and am having many surprises.

@jaydenseric
Copy link
Author

If it helps guide what the right behavior should be, the JSON representation of a normal Error instance is {}:

console.log(JSON.stringify(new Error('Message.')))

Consistency with native errors might make sense, although for the purposes of snapshot testing I wish all of the properties were enumerable.

@dougwilson
Copy link
Contributor

Ah, that's a good point. I can see either way, but it makes sense that it should be consistent.

@jaydenseric
Copy link
Author

Related past discussion: #20

@dougwilson dougwilson self-assigned this Jul 18, 2018
@dougwilson
Copy link
Contributor

So the issue is that the properties you are getting in the output are just the ones we are touching inside the factory and JSON.stringify ignores properties on the prototype.

All the properties we add are enumerable so it seems like the issue here is that Json.stringify doesn't print out properties on the prototype, hm... I'm not sure what a good solution would be.

Does anyone have some thoughts on this?

@jeanbaptiste-brasselet
Copy link

Actually for some reason message is now enumerable when for a classic error it is not. And statusCode is not enumerable.

This is really weird, and it seems to have been done because someone is trying to JSON.stringify it. But it should not be the problem of http-errors

@dougwilson
Copy link
Contributor

dougwilson commented Nov 15, 2021

Hi everyone, as an update, the 3.0 release will address this by setting all properties as non-enumerable, which matches how ECMA-262 works with errors: https://tc39.es/ecma262/#sec-error-message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants
@dougwilson @jaydenseric @jeanbaptiste-brasselet and others