javascript 将自定义错误处理程序设置为第三方插件或模块

发布于 2024-09-09 01:55:18 字数 1498 浏览 6 评论 0原文

我试图在我的核心库中为第 3 方插件/模块设置自定义错误处理程序,但不知何故, myHandler 不会提醒 e.message。

有人可以帮我吗?谢谢

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-09-16 01:55:18

上面代码中的 setErrorHandler 并没有在 Function 上设置错误处理程序。 JavaScript 不允许您更改 Function 对象内的调用代码。

相反,它创建了所调用函数的包装版本,然后返回它。

obj.setErrorHandler(myHandler);

无法工作,因为返回的包装函数被丢弃,没有分配给任何东西。

你可以说:

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

尽管我有点担心用不同的包装版本交换函数的后果。这不一定适用于所有情况,并且肯定会混淆第三方代码。至少,您需要确保不会两次包装函数,并在包装​​器中保留调用时 this 值:(

that.apply(this, a);

注意:您不需要手动转换将 arguments 传递给数组。将 arguments 对象直接传递给 apply 是有效的。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文