Components.utils.reportError

  • Revision slug: Components.utils.reportError
  • Revision title: Components.utils.reportError
  • Revision id: 94977
  • Created:
  • Creator: kitchin
  • Is current revision? No
  • Comment add note for firefox3 and "javascript.options.showInConsole"; 45 words added

Revision Content

 

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);
}

{{ Fx_minversion_note("3") }}

Examples

Usage in an exception handler:

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

Printing debugging messages to the Error Console:

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

{{ languages( { "fr": "fr/Components.utils.reportError", "ja": "ja/Components.utils.reportError", "pl": "pl/Components.utils.reportError" } ) }}

Revision Source

<p> </p>
<p><code>Components.utils.reportError</code> reports a JavaScript Error object to the <a href="/en/Error_Console" title="en/Error_Console">Error Console</a>, 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.</p>
<p>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 <code>Components.utils.reportError</code> to report debugging messages to the Error Console, just like <a href="/en/DOM/window.dump" title="en/DOM/window.dump">dump()</a> can be used to print messages to the native console.</p>
<p>However you should use <a href="/en/nsIConsoleService" title="en/nsIConsoleService">nsIConsoleService</a> for logging, as it logs the messages with normal severity, unlike <code>reportError</code>.</p>
<pre class="eval">function myDump(aMessage) {
  var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                 .getService(Components.interfaces.nsIConsoleService);
  consoleService.logStringMessage("My component: " + aMessage);
}
</pre>
<p>{{ Fx_minversion_note("3") }}</p>
<h3 name="Examples">Examples</h3>
<p>Usage in an exception handler:</p>
<pre class="eval">try {
  this.could.raise.an.exception;
} catch(e) {
  Components.utils.reportError(e); // report the error and continue execution
}
</pre>
<p>Printing debugging messages to the Error Console:</p>
<pre class="eval">Components.utils.reportError("init() called");
</pre>
<p>{{ languages( { "fr": "fr/Components.utils.reportError", "ja": "ja/Components.utils.reportError", "pl": "pl/Components.utils.reportError" } ) }}</p>
Revert to this revision