views:

64

answers:

1

Hi, I am trying to set a custom error handler for 3rd party plugins/modules in my core library, but somehow, myHandler does not alert the e.message.

Can somebody help me please? thank you

Function.prototype.setErrorHandler = function(f) {
 if (!f) {
  throw new Error('No function provided.');
 }
 var that = this;
 var g = function() {
  try {
   var a = [];
   for(var i=0; i<arguments.length; i++) {
    a.push(arguments[i]);
   }
   that.apply(null,a);
  }
  catch(e) {
   return f(e);
  }
 };
 g.old = this;
 return g;
};


function myHandler(e) {
 alert(e.message)
};

// my Core library object
(function(){
 if (typeof window.Core === 'undefined') {
  var Core = window.Core = function() {
   this.addPlugin = function(namespace, obj){
    if (typeof this[namespace] === 'undefined') {
     if (typeof obj === 'function') {
      obj.setErrorHandler(myHandler);
     } else if (!!obj && typeof obj === 'object') {
      for (var o in obj) {
       if (obj.hasOwnProperty(o) && typeof obj[o] === 'function') {
        obj[o].setErrorHandler(myHandler);
       }
      }
     }

     this[namespace] = obj;

     return true;
    } else {
     alert("The namespace '" + namespace + "' is already taken...");
     //return false;
    }
   };
  };

  window.Core = new Core();
 }
})();

// test plugin
(function(){
 var myPlugin = {
  init: function() {},
  conf: function() {
   return this.foo.x; // error here
  }
 };

 Core.addPlugin("myPlugin", myPlugin);
})();

// test
Core.myPlugin.conf(); // supposed to alert(e.message) from myHandler()
A: 

setErrorHandler in the above code doesn't set an error handler on a Function, as such. JavaScript does not give you the ability to change the called code inside a Function object.

Instead it makes a wrapped version of the function it's called on, and returns it.

obj.setErrorHandler(myHandler);

Can't work as the returned wrapper function is thrown away, not assigned to anything.

You could say:

obj[o]= obj[o].setErrorHandler(myHandler);

though I'm a bit worried about the consequences of swapping out functions with different, wrapped versions. That won't necessarily work for all cases and could certainly confuse third-party code. At the least, you'd want to ensure you don't wrap functions twice, and also retain the call-time this value in the wrapper:

that.apply(this, a);

(Note: you don't need the manual conversion of arguments to an Array. It's valid to pass the arguments object directly to apply.)

bobince