Node: Print better error messages on RequireObjectCoercible error

Created on 1 May 2017  Â·  11Comments  Â·  Source: nodejs/node

not sure how to explain this. my videomail.io app threw an uncaught error and had to debug deep to find out what this error message means:

Can't call method on [object Object]

really want to know what method is tried to be invocated here on what object? but seeing just [object Object] isn't very helpful. the responsible code line is this one:

// 7.2.1 RequireObjectCoercible(argument)
module.exports = function(it){
  if(it == undefined)throw TypeError("Can't call method on  " + it);
  return it;
};

as you can see, using nodejs 7.2.1 here and Ubuntu 16.10 on the server. but the server isn't relevant since this part of code has been browserified.

question

All 11 comments

What core are you running/reproduces the problem? Where is the snippet you posted from?

i am afraid, this is hard to reproduce. this is just an Uncaught TypeError error i have caught with the window.onerror event handler by random within the www.videomail.io app

the snippet itself is part of the stack trace this error was thrown with. here a gist of that error but not sure if it is helpful: https://gist.github.com/binarykitchen/30ece5f6a7adc02d482e3c4120fa0df7

with the window.onerror event handler

This sounds like it’s a frontend bug, not a Node bug, to me? This bug tracker is for problems with Node itself, so it might be a better idea to move this to https://github.com/nodejs/help or a Q&A site like StackOverflow unless you are sure that this is a problem with Node.

@addaleax understand your comment. but then, this part of nodejs code has been browserified for the browser, hence still relevant i think.

for example "Can't call method on " + it could be "Can't call method on " + it.toString() instead so that there is more information than just [object Object]'

If this is browserified code, I think you may have better luck opening an issue on whatever the repo is for the module being browserified. As Anna suggested, feel free to move this to https://github.com/nodejs/help, but I'm going to go ahead and close as this doesn't appear to be an issue in core. Thanks!

still no response from browserify

but still, i wonder why not to apply .toString() - this would help greatly and comes with no pitfalls. @addaleax

i could do a pr for that it this is ok?

@binarykitchen I still don’t really know where the snippet you posted is from, how this bug relates to Node (it still reads like a frontend issue from everything you’ve said), or what your expected behaviour is.

i could do a pr for that it this is ok?

Sure – that might help clear a few things up anyway

yeah, it's node, around these C++ lines (not sure which one of these)

https://github.com/nodejs/node/blob/98ddab411523f83eca9b5311529f8288379e550f/deps/v8/src/builtins/builtins-object.cc#L271
https://github.com/nodejs/node/blob/98ddab411523f83eca9b5311529f8288379e550f/deps/v8/src/builtins/builtins-string-gen.cc#L999

sorry, cannot do a pr since i am not skilled enough with C++ ... point is that the resulting JavaScript code should not be

throw TypeError("Can't call method on  " + it);

but something like

throw TypeError("Can't call method on  " + it.toString());

so that more information is printed and not [object Object]

Wouldn't the first implicitly call it.toString() though?

right. then something else than toString(). maybe util.inspect() or something better which can deal with objects too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dfahlander picture dfahlander  Â·  3Comments

ksushilmaurya picture ksushilmaurya  Â·  3Comments

fanjunzhi picture fanjunzhi  Â·  3Comments

mcollina picture mcollina  Â·  3Comments

seishun picture seishun  Â·  3Comments