queerviolet / bones

MIT License
36 stars 119 forks source link

Fix error sending to client #29

Closed glebec closed 7 years ago

glebec commented 7 years ago

Bones's custom logger middleware sends any captured errors directly to the client.

Unfortunately, there seems to be a bug — in at least some cases, this results in an empty JSON object showing up in the client side. We could omit the stack trace by doing a res.send(err.toString()), resulting in an HTML page with just the error message. If we also want the stack trace, we might have to do some more work. In any case though, it would be good to track down and understand precisely why res.send(err) results in an empty JSON response. I have a suspicion it may have to do with the default behavior for calling toJSON on an Error object, but that's just a hunch, I haven't looked into it yet.

Also, we may want to distinguish between production and development behavior here, if only for the sake of demonstrating good practices (and maybe init-time branching as a concept).

glebec commented 7 years ago

This seems to be because calling JSON.stringify skips non-enumerable properties, and an error's message, name, stack etc. are all non-enumerable.