Call the inspect() function if message is not set - #1848
Conversation
There was a problem hiding this comment.
Another option here would be to call util.inspect(err), but this wouldn't work outside of Node.
There was a problem hiding this comment.
The return value of an object's inspect function doesn't have to be a string. Would be worth calling toString to ensure that message is always a string.
From the docs: https://nodejs.org/api/util.html#util_util_inspect_object_options
You may also return another Object entirely, and the returned String will be formatted according to the returned Object. This is similar to how JSON.stringify() works:
var obj = { foo: 'this will not show up in the inspect() output' };
obj.inspect = function(depth) {
return { bar: 'baz' };
};
util.inspect(obj);
// "{ bar: 'baz' }"
Unfortunately some frameworks throw error objects which do not have a `message` property but do have an `inspect()` function. This means Mocha reports a test failure, but prints the empty string instead of any useful information about the error message. For an example, see versions of the Waterline ORM before v0.10.19: balderdashy/waterline@0965d132 If the `message` key is not set on an error object, attempt to print the same output as `console.log` in Node, by calling the object's `inspect()` function, if it exists. We could try to fallback to calling `util.inspect` on the `err` object and logging that, but I'm not sure the format would be appropriate.
933808d to
dae820e
Compare
|
I too have been experiencing this and would much prefer the proper solution here to the workaround I've been using. Are you folks solving this in a different way in your own setups? What do you think of this enhancement? |
|
Thanks for the PR! Aside from what I mentioned regarding non-string values returned by err.inspect(), it looks good :) |
|
Thanks for the code review @danielstjules! I started going down a path where I called the object's I am starting to think that I should have just called |
I think I'm less worried about that, and mostly just want to make sure we won't hit a path where mocha throws. For example, |
|
Ok! Let me try to write something up and then see if it makes sense. Kevin Burke On Fri, Sep 11, 2015 at 7:52 AM, Daniel St. Jules notifications@github.com
|
|
Oh, and I think we do get utils.inspect as part of the browser bundle. Lines 10786 to 10818 in c7a8fed |
|
Hmm I just tried |
I lied, sorry. Looks like utils is just getting picked up to support the browserify buffer module. So that would make sense :( Sorry |
|
Had to pick up some paid work - sorry! Will push something soon & tag you again when I do. |
|
Thanks @danielstjules! Sorry I couldn't push this over the finish line in time. |
|
Nah, don't worry about it - there was nothing more to do! Appreciate the help! |
Unfortunately some frameworks throw error objects which do not have a
messageproperty but do have an
inspect()function. This means Mocha reports a testfailure, but prints the empty string instead of any useful information about
the error message. For an example, see versions of the Waterline ORM before
v0.10.19: balderdashy/waterline@0965d132
If the
messagekey is not set on an error object, attempt to print the sameoutput as
console.login Node, by calling the object'sinspect()function,if it exists.
We could try to fallback to calling
util.inspecton theerrobject andlogging that, but it would break outside of Node and I'm not sure the format would be appropriate.