创建JS库警告而不是错误

时间:2015-07-15 22:14:19

标签: javascript node.js google-chrome firefox mozilla

我正在使用NPM创建自己的JS库。我想给我的库的用户一个警告,但不是错误,但是,我希望我的用户有一个警告的堆栈跟踪,我在我的库中登录。因此,为了获得堆栈跟踪,我使用了新的Error()。stack。这个问题是(1)单词'错误'默认情况下出现在堆栈跟踪中;(2)对于Node和Chrome,堆栈跟踪是相同的,但对于Mozilla则不同。

所以我的问题是 - 创建具有堆栈跟踪的警告消息的最佳方法是什么,但这看起来不是错误? Chrome和Node.js是对齐的,我可以在调用toString()之前简单地删除堆栈数组的第一个元素。但问题是这对Mozilla来说不起作用。

无论如何,这是我目前在我的图书馆中应该发出警告时所做的事情:

 var err = new Error('looks like you have called IdempotentJSON.parse on an object that was already parsed');
 console.error(err.message.concat('\n'),err.stack);

这"记录警告"看起来像这样

Chrome中的

 looks like you have called IdempotentJSON.parse on an object that was already parsed
 Error: looks like you have called IdempotentJSON.parse on an object that was already parsed
    at IdempotentJSON.parse (http://localhost:3000/static/vendor/idempotent-json.js:11:23)
    at Backbone.Model.extend.persistModel.save.success (http://localhost:3000/static/app/js/models/userModel.js:88:56)
    at Object._.extend.save.options.success (http://localhost:3000/static/vendor/backbone.js:620:30)
    at jQuery.Callbacks.fire (http://localhost:3000/static/vendor/jquery.js:3099:30)
    at Object.jQuery.Callbacks.self.fireWith [as resolveWith] (http://localhost:3000/static/vendor/jquery.js:3211:7)
    at done (http://localhost:3000/static/vendor/jquery.js:8264:14)
    at XMLHttpRequest.jQuery.ajaxTransport.send.callback (http://localhost:3000/static/vendor/jquery.js:8605:9)
Mozilla Firefox中的

"looks like you have called IdempotentJSON.parse on an object that was already parsed
" "IdempotentJSON.prototype.parse@http://localhost:3000/static/vendor/idempotent-json.js:11:23
@http://localhost:3000/static/app/js/app.js:157:30
Backbone.batchSyncCollection/<@http://localhost:3000/static/app/js/app.js:141:16
jQuery.Callbacks/fire@http://localhost:3000/static/vendor/jquery.js:3099:10
jQuery.Callbacks/self.fireWith@http://localhost:3000/static/vendor/jquery.js:3211:7
done@http://localhost:3000/static/vendor/jquery.js:8264:5
.send/callback/<@http://localhost:3000/static/vendor/jquery.js:8605:1
Node.js中的

looks like you have called IdempotentJSON.parse on an object that was already parsed
 Error: looks like you have called IdempotentJSON.parse on an object that was already parsed
    at IdempotentJSON.parse (/Users/amills001c/WebstormProjects/first_comcast/node_modules/idempotent-json/index.js:9:19)
    at Object.<anonymous> (/Users/amills001c/WebstormProjects/first_comcast/IJSONexp/ijson.js:8:15)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

一个适用于Node.js和Chrome的快速解决方案是删除堆栈跟踪的第一行,说明&#34;错误:...&#34;。

但是这个电话无效:

err.stack.shift(); //error

显然堆栈不是JS中的数组?

无论如何,这对Mozilla来说都不会起作用。

我最好打赌另一个图书馆检查它所在的环境/浏览器?像这样:

http://www.eriwen.com/javascript/js-stack-trace/

它的2015年!顺便说一句,Mozilla,Chrome和Node在堆栈跟踪中都是正确的,我可以点击Chrome和Node中的堆栈跟踪,它会将我带到涉及的文件,似乎我无法在Firefox中执行此操作。

我想要的是这样的东西:

 Warning: looks like you have called IdempotentJSON.parse on an object that was already parsed
    at IdempotentJSON.parse (http://localhost:3000/static/vendor/idempotent-json.js:11:23)
    at Backbone.Model.extend.persistModel.save.success (http://localhost:3000/static/app/js/models/userModel.js:88:56)
    at Object._.extend.save.options.success (http://localhost:3000/static/vendor/backbone.js:620:30)
    at jQuery.Callbacks.fire (http://localhost:3000/static/vendor/jquery.js:3099:30)
    at Object.jQuery.Callbacks.self.fireWith [as resolveWith] (http://localhost:3000/static/vendor/jquery.js:3211:7)
    at done (http://localhost:3000/static/vendor/jquery.js:8264:14)
    at XMLHttpRequest.jQuery.ajaxTransport.send.callback (http://localhost:3000/static/vendor/jquery.js:8605:9)

1 个答案:

答案 0 :(得分:1)

查看console.trace的来源,您可以尝试使用chrome&amp;的node.js:

function warning(message) {
  var err = new Error;
  err.name = 'Warning';
  err.message = message; //or use util.format in nodejs
  Error.captureStackTrace(err, warning); // this is to prevent `warning` function being included in the stack.
  return err;
};
console.log(warning('message').stack);

在nodejs中,输出:

Warning: message
    at Object.<anonymous> (/home/ubuntu/workspace/warning.js:9:13)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

不幸的是,firefox没有captureStackTrace方法。因此,您必须直接调用上述内容而不将其包装在函数中。