14

Is there a way in javascript to make the runtime use a different object representation in the error messages.

A typical error message

Uncaught TypeError: Object [object Object] has no method 'toggle'

It would be helpful if we can give a better representation to the object than [object Object] .In other languages you can make it print a better representation of the object by overriding toString().

However it looks like overriding toString has no effect in this case.

Ahmed Aeon Axan
  • 2,139
  • 1
  • 17
  • 30
  • Do all the browsers do that or only IE? In Firefox and Chrome I usually get pretty good error messages. The line should be there so you can `console.log(theObject);` just before the error. – HMR Nov 14 '13 at 11:29
  • I copied the error message from chrome actually. – Ahmed Aeon Axan Nov 14 '13 at 17:45
  • 1
    Yes, that is not a very helpful message. Chrome can break on error and you'll be on the line that caused the error. Press F12, Sources tab, button in the bottom that looks like a stop sign with a pause icon in it. You can click it multiple times to: not pause at all on errors, pause on uncaught errors and all errors. – HMR Nov 15 '13 at 03:58

6 Answers6

4

I would use a try...catch and throw your own error messages:

var obj = {
  name: 'obj',
  fn: function () {
    return 'hallo';
  }
}


function hasMethod(obj, fn) {
    try {
        if (!obj[fn]) {
            var err = obj.name + ' does not have a method ' + fn;
            throw new Error(err)
        }
    } catch (e) {
        console.log(e)
    }
}

hasMethod(obj, 'moose'); // obj does not have a method moose

Fiddle

Andy
  • 61,948
  • 13
  • 68
  • 95
  • yes. throwing my own error messages is one way. I was looking for a solution without try/catches, So it works for error messages thrown anywhere in the code, ones I do not expect too – Ahmed Aeon Axan Nov 09 '13 at 16:40
  • This is the only solution I can think of. Otherwise we need a way to modify the `Error` class itself. – me_digvijay Nov 15 '13 at 07:38
1

The message is browser dependent. Without making any changes other than switching to chrome you get a nicer message that reports the type of object you were using.

evan
  • 12,307
  • 7
  • 37
  • 51
1

This is a limitation of the Object.prototype.toString method, which is what would seem to be responsible for creating the [object Object] notation you see in those TypeErrors. Though you can override the toString prototype for the Object global, the overridden prototype is not what ends up getting called by the native code in TypeErrors and the like.

Here's a bit more explanation for why that specific notation was chosen and here's why it can't be overridden. Effectively, that error message format is more a part of the Javascript compiler and/or implementation than a part of Javascript itself!

Community
  • 1
  • 1
Jeff Sisson
  • 1,616
  • 11
  • 22
0

If your aim is to convert plain javascript object to string then

JSON.stringify(object) 

works absolutely great. Documentation for it is here.

But I see "toggle" in the error message you mentioned, I am assuming it is general JQuery toggle which is performed on DOM objects. In this case JSON.stringify() does not work.

To Convert DOM object to string follow this stackoverflow post

Community
  • 1
  • 1
Sreedhar M B
  • 179
  • 4
  • 15
0

Perhaps you can also try Object.toSource() (i guess it works only in firefox)

everlasto
  • 4,890
  • 4
  • 24
  • 31
-3

Something like this should work:

Object.prototype.toString = function() {
  return '(OBJECT: ' + this.name + ')';
};

foo = { name: 'foo' };
bar = { name: 'bar' };
baz = {}

foo.thing(); // TypeError: Object (OBJECT: foo) has no method 'thing'
bar.thing(); // TypeError: Object (OBJECT: bar) has no method 'thing'
baz.thing(); // TypeError: Object (OBJECT: undefined) has no method 'thing'

(node js)

olleicua
  • 2,039
  • 2
  • 21
  • 33