Monday, March 31, 2014

Confusing Error

I've been hacking away at my extension again, based on the fact that the old version is completely broken by Chrome version 35.

One of the major operational pieces that I did not have in place is error reporting -- I relied on anecdotal error reports from users, which have typically been enough in the past.  However, with a major refactoring and rewrite, there are so many problems that can pop up, and I have no way to know if I'm causing bad regressions.

So I've been building version 1.0.31, with opt-out error reporting.  But when making the changes and firing it up for the first time, I kept seeing this error in the console:

Error in event handler for (function name here): TypeError: The first argument is the receiver and must be an object.

WTF?  It only seemed to come up when registering a callback, but I didn't change anything significant with any callbacks, I just added some error aggregation and reporting.  After struggling with finding the cause, I used the time-tested method of commenting out large blocks of code and then un-commenting until things work.  I eventually narrowed it down to this new function-class that I added:

function Error(code, message) {
  this.code = code;
  this.message = message;
}

Why would this class cause problems, it's as basic as it gets...?  And why would it cause this weird error? The only answer I could think of was that "Error" is a reserved name and declaring this function effectively overwrote it, causing all sorts of disaster.  So I renamed the class to "ErrorReport" and things worked again.

If you encounter this error, there's a chance somewhere that someone is inadvertently clobbering a "protected namespace" function...one of the wonderful joys of Javascript is the ability to shoot yourself in the foot in mysterious ways.

1 comment:

  1. Glenn...

    maybe Error gagged trying to use your message as a filename.,.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error

    ReplyDelete