Components.utils.reportError 编辑

Components.utils.reportError reports a JavaScript Error object to the Error Console, and returns. It is meant for use by extension developers who have exception handler blocks which want to "eat" an exception, but still want to report it to the console.

It must be called with one parameter, usually an object which was caught by an exception handler. If it is not a JavaScript error object, the parameter is converted to a string and reported as a new error. This means you can use Components.utils.reportError to report debugging messages to the Error Console, just like dump() can be used to print messages to the native console.

However you should use nsIConsoleService for logging, as it logs the messages with normal severity, unlike reportError.

function myDump(aMessage) {
  var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                 .getService(Components.interfaces.nsIConsoleService);
  consoleService.logStringMessage("My component: " + aMessage);
}

Firefox 3: The Error Console will not display chrome errors by default. To show them, set the preference 'javascript.options.showInConsole' to true, or use nsConsoleService instead to show a message.

Firefox 4: The preference 'javascript.options.showInConsole' is set to true by default in this version.

Examples

Usage in an exception handler:

try {
  this.could.raise.an.exception;
} catch(e) {
  Components.utils.reportError(e); // report the error and continue execution
}

Sending debugging messages to the Error Console:

Components.utils.reportError("init() called");

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:100 次

字数:2223

最后编辑:7年前

编辑次数:0 次

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